Skip to main content

Basic Queries

GraphQL queries in Directus are automatically generated based on your collections. For each collection, several query types are available.

Query a Collection

Retrieve multiple items from a collection:

Query by ID

Retrieve a single item by its primary key:

Query System Collections

Use the /graphql/system endpoint to query system collections:
System collection names in the /graphql/system endpoint don’t include the directus_ prefix.

Filtering

Directus provides powerful filtering capabilities through GraphQL query arguments.

Basic Filters

Filter Operators

Directus supports a comprehensive set of filter operators:

Logical Operators

Combine multiple filters with logical operators:
Available logical operators:
  • _and: All conditions must be true
  • _or: At least one condition must be true

Nested Filters

Filter on related items:

Sorting

Sort results using the sort argument:
  • Prefix with - for descending order
  • No prefix for ascending order
  • Multiple sort fields are evaluated in order

Pagination

Control how many items are returned:

Limit and Offset

Page-Based Pagination

Searching

Perform full-text search across multiple fields:
The search parameter performs a case-insensitive search across all string fields in the collection.

Relationships

Directus automatically resolves relationships in GraphQL.

Many-to-One (M2O)

One-to-Many (O2M)

Many-to-Many (M2M)

Apply filters to related items:

Aggregation

Query aggregated data using the _aggregated suffix:

Grouping

Group aggregations by field values:

Available Aggregate Functions

  • count: Count of items
  • avg: Average value (numeric fields)
  • sum: Sum of values (numeric fields)
  • min: Minimum value
  • max: Maximum value
  • avgDistinct: Average of distinct values
  • sumDistinct: Sum of distinct values
  • countDistinct: Count of distinct values

Versioning

Query content versions using the _by_version suffix:
The _by_version queries are only available on the /graphql endpoint for item collections, not on /graphql/system.

Deep Filtering

Filter parent items based on related item properties:

Using Variables

Make queries reusable with variables:
Variables payload:

Fragments

Define reusable field selections:

Aliases

Query the same collection multiple times with different arguments:

Singleton Collections

For singleton collections, query without the _by_id suffix:

Example: Complex Query

Here’s a comprehensive example combining multiple features:
Variables:

Next Steps

GraphQL Mutations

Learn how to create, update, and delete data

GraphQL Subscriptions

Get real-time updates with subscriptions