---
title: Custom Queries
description: Common GraphQL query examples and patterns for the Intuition API
url: https://docs.intuition.systems/docs/v1/graphql-api/custom-queries
markdown_url: https://docs.intuition.systems/docs/v1/graphql-api/custom-queries.md
version: V1
llms_index: https://docs.intuition.systems/docs/v1/llms.txt
---

This page provides practical examples of common GraphQL queries for the Intuition API.

## Best Practices

1. **Use Fragments**: Create reusable fragments for common fields
2. **Optimize Queries**: Only request the fields you need
3. **Handle Errors**: Always implement proper error handling
4. **Cache Strategically**: Use React Query's caching capabilities
5. **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`:

```graphql
query GetAtom($term_id: String!) {
  atom(term_id: $term_id) {
    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:

```graphql
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:

```graphql
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:

```graphql
query SearchAtoms($query: String!, $limit: Int = 20) {
  search_term(args: { query: $query }, limit: $limit) {
    id
    type
    atom {
      label
      image
      creator {
        id
        label
      }
    }
  }
}
```

### Pagination

Hasura uses offset-based pagination with `limit` and `offset`:

```graphql
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

```tsx
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

```tsx
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($term_id: String!) {
        atom(term_id: $term_id) {
          term_id
          label
          image
          type
        }
      }
    `, { term_id: termId }),
    enabled: !!termId
  })
}
```
