Custom Queries
This page provides practical examples of common GraphQL queries for the Intuition API.
Best Practicesβ
- Use Fragments: Create reusable fragments for common fields
- Optimize Queries: Only request the fields you need
- Handle Errors: Always implement proper error handling
- Cache Strategically: Use React Query's caching capabilities
- Type Safety: Leverage generated types for better development experience
Basic Queriesβ
Fetching a Single Atomβ
Use the primary key lookup for fetching a single atom by its term_id:
query GetAtom($termId: String!) {
atom(term_id: $termId) {
term_id
label
image
type
created_at
creator {
id
label
}
value {
thing { name description url }
person { name description url }
organization { name description url }
}
}
}
Fetching Triplesβ
Query triples with Hasura's where filtering:
query GetTriples($subjectId: String, $predicateLabel: String, $limit: Int = 20) {
triples(
where: {
subject_id: { _eq: $subjectId }
predicate: { label: { _ilike: $predicateLabel } }
}
order_by: { created_at: desc }
limit: $limit
) {
term_id
subject {
term_id
label
}
predicate {
term_id
label
}
object {
term_id
label
}
created_at
}
}
Fetching Signalsβ
Query signal events for a specific atom or account:
query GetSignals($accountId: String!, $limit: Int = 20) {
signals(
where: { account_id: { _eq: $accountId } }
order_by: { created_at: desc }
limit: $limit
) {
id
delta
atom_id
triple_id
created_at
account {
label
}
}
}
Advanced Queriesβ
Searchβ
Use the search_term database function for text search:
query SearchAtoms($query: String!, $limit: Int = 20) {
search_term(args: { query: $query }, limit: $limit) {
term_id
label
image
type
creator {
id
label
}
}
}
Paginationβ
Hasura uses offset-based pagination with limit and offset:
query GetAtomsPaginated($limit: Int!, $offset: Int!) {
atoms_aggregate {
aggregate {
count
}
}
atoms(
limit: $limit
offset: $offset
order_by: { created_at: desc }
) {
term_id
label
image
type
created_at
}
}
React Query Hooksβ
Using Generated Hooksβ
import { useGetAtomQuery } from '@0xintuition/graphql'
function AtomViewer({ atomId }: { atomId: string }) {
const { data, isLoading, error } = useGetAtomQuery({
variables: { termId: atomId }
})
if (isLoading) return <div>Loading...</div>
if (error) return <div>Error: {error.message}</div>
return (
<div>
<h1>{data?.atom?.label}</h1>
<img src={data?.atom?.image} alt={data?.atom?.label} />
<p>Type: {data?.atom?.type}</p>
</div>
)
}
Custom Hooksβ
import { useQuery } from '@tanstack/react-query'
import { GraphQLClient } from 'graphql-request'
import { API_URL_PROD } from '@0xintuition/graphql'
const client = new GraphQLClient(API_URL_PROD)
function useCustomAtomQuery(termId: string) {
return useQuery({
queryKey: ['atom', termId],
queryFn: () => client.request(`
query GetAtom($termId: String!) {
atom(term_id: $termId) {
term_id
label
image
type
}
}
`, { termId }),
enabled: !!termId
})
}