mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
* items semi complete
* updated items page to use snippet toggler and migrated endpoint docs to use it
* updated files page to use snippet toggler and migrated REST and GraphQL endpoint docs to it
* updated activity page to use snippet toggler and migrated REST and GraphQL endpoint doc to it
* updated collections page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* updated dashboards page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated extensions page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated fields page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated flows page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated folders page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated notifications page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated operations page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated panels page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated permissions page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated presets page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated relations page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated revisions page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated roles page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Made headlines consistant with the rest of the doc pages
* Updated server page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated settings page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated shares page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated translations page to use snippet togglers and migrated REST endpoint docs to them
* Updated users page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated utilities page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated webhooks page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated authentication page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* Updated Global Parameters page to use snippet togglers where there are adjacent REST and GraphQL Examples
* Added SDK code snippets to items page and made generic variables consistant
* Added SDK code snippets to files page and made generic variables consistant
* Few lang changes for files page
* Added SDK code snippets to activity page and made generic variables consistant
* Added SDK code snippets to collections page and made generic variables consistant
* Added SDK code snippets to dashboards page and made generic variables consistant
* removed query word from query parameter objects
* Added SDK code snippets to fields page and made generic variables consistant
* SnippetToggler border
* Used dynamic border color for snippettoggler heading
* Spacing top and bottom of snippet toggler in docs
* Removed extra HRs
* Remove manual TOC in query reference
* Small code styling change in items page
* Updated users page to use snippet togglers and migrated REST and GraphQL endpoint docs to them
* dashboards fixed up property names
* Small copy update on extensions page
* Updated keys in delete mult notifications REST
* Updated keys in operations
* Update keys in panel delete many
* Update keys in permissions
* Added quotes around generic example ID strings
* Added code formatting to final example in share public info
* Format files
* Refined sidebar
* Insert newline before ending template tags
* Fixed extra closing tags, causing an error, un users ref
* Text Formatting Users
* Put GQL related notes inside toggler
* Added SDK code snippets to flows page and made generic variables consistant
* Added SDK code snippets to folder page and made generic variables consistant
* fixing whitepsace for flows and folders page
* Consistent newlines in SnippetToggler usages
* Run prettier
* Fix 'alwaysDark' definition
* Home page snippet toggler style fixes
* Fix snippet toggler lang hover color in light mode
* Introduce different code theme for light mode
* Added SDK code snippets to notifications page and made generic variables consistant
* Switch to 'material-theme-lighter'
* Format file
* Fix tip
* Fix tip in sdk ref
* Consistent spacing for custom containers
* Added SDK code snippets to operations page and made generic variables consistant
* Lint & format code blocks
* Lint & format operations
* Added SDK code snippets to panels page and made generic variables consistant
* Added SDK code snippets to permissions page and made generic variables consistant
* Added SDK code snippets to presets page and made generic variables consistant
* Added SDK code snippets to relations page and made generic variables consistant
* Added SDK code snippets to revisions page and made generic variables consistant
* Added SDK code snippets to roles page and made generic variables consistant
* Added SDK code snippets to server page and made generic variables consistant
* Added SDK code snippets to settings page and made generic variables consistant
* app_url -> directus_project_url
* Omitted auth details in delete multiple files
* Added quotes to values in roles
* Upload a file snippets
* Pluralization for upload/import files
* More files functions typos
* Added SDK code snippets to shares page (still missing createShare(s) as endpoint not functioning currently) and made generic variables consistant
* Added SDK code snippets to translations page (missing delete endponts because not working) and made generic variables consistant
* Added SDK code snippets to users page and made generic variables consistant
* Added SDK code snippets to webhooks page and made generic variables consistant
* Added SDK code snippets to utilites page (except cleaning cache, will be tested and added in later commit) and made generic variables consistant
* Added SDK code snippets to auth page (not login, refresh, and logout though due to errors)
* Added SDK code snippets for utilsExport and clearCache
* added github username be7DOTis to contributors
* Omit auth commands in updateComment
* utilsImport
* rename app_url generic value
* changed instances of updated*operation* to update*Operation*
* missed some 'updated' changse
* Added SDK Snippets to Query Parameters page
* Add section on file security
* added create(s)Shares SDK snippet to shares page
* added console.log to create snippets
* Added delete(s)Webhook SDK snippet to webhooks page
* Added SDK snippets to extensions page
* Added create/updateSingleton section to items page
* Links in files security
* Added SDK Snippets to Schema page
* Added GQL Generic examples to snippet togglers and removed snippet toggler from Login Using SSO Providers
* Added create(s)Presets SDK Snippets to presets page
* replaced fields query in generics snippets for a more generic
* replaced fields query in generics snippets for a more generic
* Use storage value only if valid choice
* Sync snippet togglers across page
* Update docs/reference/system/activity.md
* Update docs/reference/system/activity.md
* Update docs/reference/system/extensions.md
* Update docs/reference/system/revisions.md
* Update docs/reference/system/settings.md
* Update docs/reference/system/revisions.md
* Update docs/reference/system/settings.md
* Update docs/reference/system/activity.md
* Update docs/reference/system/roles.md
* Update docs/reference/system/roles.md
* Update docs/reference/system/roles.md
* Update docs/reference/system/roles.md
* Update docs/reference/system/schema.md
* Update docs/reference/system/server.md
* Update docs/reference/system/shares.md
* Replace all directus_project_url placeholders
* Revert "Sync snippet togglers across page"
This reverts commit 8b36f0d778.
* Update docs/reference/system/shares.md
* Update docs/reference/system/webhooks.md
* Clarify singleton section
* Consistent newlines between SnippetToggler templates
* Format files
* Remove console.log(result) statements from snippet
* Add examples for shares & users
Co-authored-by: Brainslug <tim@brainslug.nl>
* Fix hash GraphQL example
* Clarify update singleton section
* Add auth examples
Co-authored-by: Brainslug <tim@brainslug.nl>
* Final run on consistent newlines between SnippetToggler
* Switch to github themes
* The "Last One"
Co-authored-by: Brainslug <tim@brainslug.nl>
* The "Big One"
* Fix dead links
---------
Co-authored-by: Bevis Halsey-Perry <hi@be7.is>
Co-authored-by: Kevin Lewis <kvn@lws.io>
Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>
Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
Co-authored-by: Brainslug <tim@brainslug.nl>
867 lines
18 KiB
Markdown
867 lines
18 KiB
Markdown
---
|
||
description: REST and GraphQL API documentation to run queries in Directus.
|
||
readTime: 9 min read
|
||
pageClass: page-reference
|
||
---
|
||
|
||
# Global Query Parameters
|
||
|
||
> Most Directus API Endpoint operations can be manipulated with the following parameters. It is important to understand
|
||
> them to get the most out of the platform.
|
||
|
||
## Fields
|
||
|
||
Choose the fields that are returned in the current dataset. This parameter supports dot notation to request nested
|
||
relational fields. You can also use a wildcard (\*) to include all fields at a specific depth.
|
||
|
||
### Examples
|
||
|
||
Get all top-level fields\
|
||
`*`
|
||
|
||
Get all top-level fields and all second-level relational fields\
|
||
`*.*`
|
||
|
||
::: tip Performance & Size
|
||
|
||
While the fields wildcard is very useful for debugging purposes, we recommend only requesting _specific_ fields for
|
||
production use. By only requesting the fields you really need, you can speed up the request, and reduce the overall
|
||
output size.
|
||
|
||
:::
|
||
|
||
Get all top-level fields and second-level relational fields within images\
|
||
`*,images.*`
|
||
|
||
Get only the first_name and last_name fields\
|
||
`first_name,last_name`
|
||
|
||
Get all top-level and second-level relational fields, and third-level fields within images.thumbnails\
|
||
`*.*,images.thumbnails.*`
|
||
|
||
### Many-To-Any (Union Types)
|
||
|
||
Seeing that Many-to-Any (M2A) fields have nested data from multiple collections, it's not always safe / wanted to fetch
|
||
the same field from every related collection. In M2A fields, you can use the following syntax to specify what fields to
|
||
fetch from which related nested collection type:\
|
||
`?fields=<m2a-field>:<collection-scope>.<field>`.
|
||
|
||
Lets say we have a collection `pages` with a many-to-any field called `sections` that points to `headings`,
|
||
`paragraphs`, and `videos`. We only want to fetch `title` and `level` from `headings`, `body` from `paragraphs` and
|
||
`source` from `videos`. We can achieve that by using:
|
||
|
||
```
|
||
sections.item:headings.title
|
||
sections.item:headings.level
|
||
sections.item:paragraphs.body
|
||
sections.item:videos.source
|
||
```
|
||
|
||
In GraphQL, this can be achieved using Union Types.
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
```
|
||
?fields=title,body,featured_image.*
|
||
|
||
// or
|
||
|
||
?fields[]=title
|
||
&fields[]=body
|
||
&fields[]=featured_image.*
|
||
```
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
` // Natively supported in GraphQL`
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, readItems } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://directus.example.com').with(rest());
|
||
|
||
const result = await client.request(
|
||
readItems('articles', {
|
||
fields: ['title', 'date_created', { authors: ['name'] }],
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Filter
|
||
|
||
Used to search items in a collection that matches the filter's conditions. The filter param follows
|
||
[the Filter Rules spec](/reference/filter-rules), which includes additional information on logical operators (AND/OR),
|
||
nested relational filtering, and dynamic variables.
|
||
|
||
### Examples
|
||
|
||
Retrieve all items where `first_name` equals "Rijk"
|
||
|
||
```json
|
||
{
|
||
"first_name": {
|
||
"_eq": "Rijk"
|
||
}
|
||
}
|
||
```
|
||
|
||
Retrieve all items in one of the following categories: "vegetables", "fruit"
|
||
|
||
```json
|
||
{
|
||
"categories": {
|
||
"_in": ["vegetables", "fruit"]
|
||
}
|
||
}
|
||
```
|
||
|
||
Retrieve all items that are published between two dates
|
||
|
||
```json
|
||
{
|
||
"date_published": {
|
||
"_between": ["2021-01-24", "2021-02-23"]
|
||
}
|
||
}
|
||
```
|
||
|
||
Retrieve all items where the author's "vip" flag is true
|
||
|
||
```json
|
||
{
|
||
"author": {
|
||
"vip": {
|
||
"_eq": true
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
::: tip Nested Filters
|
||
|
||
The above example will filter the _top level_ items based on a condition _in_ the related item. If you're looking to
|
||
filter the related items themselves, take a look at [the `deep` parameter](#deep)!
|
||
|
||
:::
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
```
|
||
?filter[first_name][_eq]=Rijk
|
||
|
||
// or
|
||
|
||
?filter={ "first_name": { "_eq": "Rijk" }}
|
||
```
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
```graphql
|
||
query {
|
||
users(filter: { first_name: { _eq: "Rijk" } }) {
|
||
id
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, readItems } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://directus.example.com').with(rest());
|
||
|
||
const result = await client.request(
|
||
readItems('articles', {
|
||
filter: {
|
||
status: {
|
||
_eq: 'draft',
|
||
},
|
||
},
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
::: tip Filtering M2A fields
|
||
|
||
Because attribute names in GraphQL cannot contain the `:` character, you will need to replace it with a double
|
||
underscore. For example, instead of using `sections.item:heading` in your filter, you will need to use
|
||
`sections.item__heading` (see the full example below).
|
||
|
||
```graphql
|
||
query {
|
||
articles(
|
||
filter: {
|
||
sections: {
|
||
item__headings: {
|
||
# Instead of: item:headings
|
||
title: { _eq: "Section 1" }
|
||
}
|
||
}
|
||
}
|
||
) {
|
||
id
|
||
}
|
||
}
|
||
```
|
||
|
||
:::
|
||
|
||
## Search
|
||
|
||
The search parameter allows you to perform a search on all string and text type fields within a collection. It's an easy
|
||
way to search for an item without creating complex field filters – though it is far less optimized. It only searches the
|
||
root item's fields, related item fields are not included.
|
||
|
||
### Example
|
||
|
||
Find all items that mention Directus\
|
||
`Directus`
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
`?search=Directus`
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
```graphql
|
||
query {
|
||
articles(search: "Directus") {
|
||
id
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, readItems } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://directus.example.com').with(rest());
|
||
|
||
const result = await client.request(
|
||
readItems('articles', {
|
||
search: 'foobar',
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Sort
|
||
|
||
What field(s) to sort by. Sorting defaults to ascending, but a minus sign (`-`) can be used to reverse this to
|
||
descending order. Fields are prioritized by the order in the parameter. The dot-notation has to be used when sorting
|
||
with values of nested fields.
|
||
|
||
### Examples
|
||
|
||
Sort by creation date descending\
|
||
`-date_created`
|
||
|
||
Sort by a "sort" field, followed by publish date descending\
|
||
`sort, -publish_date`
|
||
|
||
Sort by a "sort" field, followed by a nested author's name\
|
||
`sort, -author.name`
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
```
|
||
?sort=sort,-date_created,author.name
|
||
|
||
// or
|
||
|
||
?sort[]=sort
|
||
&sort[]=-date_created
|
||
&sort[]=-author.name
|
||
```
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
```graphql
|
||
query {
|
||
articles(sort: ["sort", "-date_created", "author.name"]) {
|
||
id
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, readItems } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://directus.example.com').with(rest());
|
||
|
||
const result = await client.request(
|
||
readItems('articles', {
|
||
sort: '-date_created', //Sort by creation date descending
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Limit
|
||
|
||
Set the maximum number of items that will be returned. The default limit is set to `100`.
|
||
|
||
### Examples
|
||
|
||
Get the first 200 items\
|
||
`200`
|
||
|
||
Get all items\
|
||
`-1`
|
||
|
||
::: warning All Items
|
||
|
||
Depending on the size of your collection, fetching unlimited data may result in degraded performance or timeouts, use
|
||
with caution.
|
||
|
||
:::
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
`?limit=200`
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
```graphql
|
||
query {
|
||
articles(limit: 200) {
|
||
id
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, readItems } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://directus.example.com').with(rest());
|
||
|
||
const result = await client.request(
|
||
readItems('articles', {
|
||
limit: 3,
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Offset
|
||
|
||
Skip the first `n` items in the response. Can be used for pagination.
|
||
|
||
### Examples
|
||
|
||
Get items 101—200\
|
||
`100`
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
`?offset=100`
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
```graphql
|
||
query {
|
||
articles(offset: 100) {
|
||
id
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, readItems } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://directus.example.com').with(rest());
|
||
|
||
const result = await client.request(
|
||
readItems('articles', {
|
||
offset: 5,
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Page
|
||
|
||
An alternative to `offset`. Page is a way to set `offset` under the hood by calculating `limit * page`. Page is
|
||
1-indexed.
|
||
|
||
### Examples
|
||
|
||
Get items 1-100\
|
||
`1`
|
||
|
||
Get items 101-200\
|
||
`2`
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
`?page=2`
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
```graphql
|
||
query {
|
||
articles(page: 2) {
|
||
id
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, readItems } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://directus.example.com').with(rest());
|
||
|
||
const result = await client.request(
|
||
readItems('articles', {
|
||
page: 1,
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Aggregation & Grouping
|
||
|
||
Aggregate functions allow you to perform calculations on a set of values, returning a single result.
|
||
|
||
The following aggregation functions are available in Directus:
|
||
|
||
| Name | Description |
|
||
| --------------- | ------------------------------------------------------------- |
|
||
| `count` | Counts how many items there are |
|
||
| `countDistinct` | Counts how many unique items there are |
|
||
| `sum` | Adds together the values in the given field |
|
||
| `sumDistinct` | Adds together the unique values in the given field |
|
||
| `avg` | Get the average value of the given field |
|
||
| `avgDistinct` | Get the average value of the unique values in the given field |
|
||
| `min` | Return the lowest value in the field |
|
||
| `max` | Return the highest value in the field |
|
||
| `countAll` | Equivalent to `?aggregate[count]=*` (GraphQL only) |
|
||
|
||
### Grouping
|
||
|
||
By default, the above aggregation functions run on the whole dataset. To allow for more flexible reporting, you can
|
||
combine the above aggregation with grouping. Grouping allows for running the aggregation functions based on a shared
|
||
value. This allows for things like _"Average rating per month"_ or _"Total sales of items in the jeans category"_.
|
||
|
||
The `groupBy` query allows for grouping on multiple fields simultaneously. Combined with the [Functions](#functions),
|
||
this allows for aggregate reporting per year-month-date.
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
```
|
||
?aggregate[avg]=cost
|
||
&groupBy[]=author
|
||
&groupBy[]=year(publish_date)
|
||
```
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
```graphql
|
||
query {
|
||
articles_aggregated(groupBy: ["author", "year(publish_date)"]) {
|
||
group
|
||
sum {
|
||
revenue
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, aggregate } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://directus.example.com').with(rest());
|
||
|
||
const result = await client.request(
|
||
aggregate('articles', {
|
||
aggregate: { count: '*' },
|
||
groupBy: 'authors',
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Deep
|
||
|
||
Deep allows you to set any of the other query parameters on a nested relational dataset.
|
||
|
||
### Examples
|
||
|
||
Limit the nested related articles to 3
|
||
|
||
```json
|
||
{
|
||
"related_articles": {
|
||
"_limit": 3
|
||
}
|
||
}
|
||
```
|
||
|
||
Only get 3 related articles, with only the top rated comment nested
|
||
|
||
```json
|
||
{
|
||
"related_articles": {
|
||
"_limit": 3,
|
||
"comments": {
|
||
"_sort": "rating",
|
||
"_limit": 1
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
```
|
||
?deep[translations][_filter][languages_code][_eq]=en-US
|
||
|
||
// or
|
||
|
||
?deep={ "translations": { "_filter": { "languages_code": { "_eq": "en-US" }}}}
|
||
```
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
` // Natively supported in GraphQL`
|
||
|
||
```graphql
|
||
query {
|
||
members {
|
||
favorite_games(filter: { name: { _eq: "Mariokart 8" } }) {
|
||
id
|
||
featured_image {
|
||
filename_disk
|
||
}
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, readItems } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://phzn-malleable.directus.app').with(staticToken()).with(rest());
|
||
|
||
const result = await client.request(
|
||
readItems('articles', {
|
||
filter: {
|
||
authors: {
|
||
name: {
|
||
_eq: 'John',
|
||
},
|
||
},
|
||
},
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Aliases
|
||
|
||
Aliases allow you rename fields on the fly, and request the same nested data set multiple times using different filters.
|
||
|
||
::: warning Nested fields
|
||
|
||
It is only possible to alias same level fields.\
|
||
Alias for nested fields, f.e. `field.nested`, will not work.
|
||
|
||
:::
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
```
|
||
?alias[all_translations]=translations
|
||
&alias[dutch_translations]=translations
|
||
&deep[dutch_translations][_filter][code][_eq]=nl-NL
|
||
```
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
_Natively supported in GraphQL:_
|
||
|
||
```graphql
|
||
query {
|
||
articles {
|
||
dutch_translations: translations(filter: { code: { _eq: "nl-NL" } }) {
|
||
id
|
||
}
|
||
|
||
all_translations: translations {
|
||
id
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, readItems } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://phzn-malleable.directus.app').with(staticToken()).with(rest());
|
||
|
||
const result = await client.request(
|
||
readItems('articles', {
|
||
alias: {
|
||
all_translations: 'translations',
|
||
dutch_translations: 'translations',
|
||
},
|
||
deep: {
|
||
dutch_translations: {
|
||
_filter: {
|
||
code: {
|
||
_eq: 'nl-NL',
|
||
},
|
||
},
|
||
},
|
||
},
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Export
|
||
|
||
Save the current API response to a file.
|
||
|
||
Saves the API response to a file. Accepts one of `csv`, `json`, `xml`, `yaml`.
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
```
|
||
?export=csv
|
||
?export=json
|
||
?export=xml
|
||
?export=yaml
|
||
```
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
`// Not Applicable`
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
// Not Applicable
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Functions
|
||
|
||
Functions allow for "live" modification of values stored in a field. Functions can be used in any query parameter you'd
|
||
normally supply a field key, including fields, aggregation, and filter.
|
||
|
||
Functions can be used by wrapping the field key in a JavaScript like syntax, for example:
|
||
|
||
`timestamp` -> `year(timestamp)`
|
||
|
||
### DateTime Functions
|
||
|
||
| Filter | Description |
|
||
| --------- | -------------------------------------------------------- |
|
||
| `year` | Extract the year from a datetime/date/timestamp field |
|
||
| `month` | Extract the month from a datetime/date/timestamp field |
|
||
| `week` | Extract the week from a datetime/date/timestamp field |
|
||
| `day` | Extract the day from a datetime/date/timestamp field |
|
||
| `weekday` | Extract the weekday from a datetime/date/timestamp field |
|
||
| `hour` | Extract the hour from a datetime/date/timestamp field |
|
||
| `minute` | Extract the minute from a datetime/date/timestamp field |
|
||
| `second` | Extract the second from a datetime/date/timestamp field |
|
||
|
||
### Array Functions
|
||
|
||
| Filter | Description |
|
||
| ------- | ----------------------------------------------------------------- |
|
||
| `count` | Extract the number of items from a JSON array or relational field |
|
||
|
||
::: warning GraphQL
|
||
|
||
Names aren't allowed to include any special characters in GraphQL, preventing the `()` syntax from being used.
|
||
|
||
As an alternative, the above functions can be used by appending `_func` at the end of the field name, and using the
|
||
function name as the nested field (see the example that follows).
|
||
|
||
:::
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
```
|
||
?fields=id,title,weekday(date_published)
|
||
&filter[year(date_published)][_eq]=2021
|
||
```
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
```graphql
|
||
query {
|
||
articles(filter: { date_published_func: { year: { _eq: 2021 } } }) {
|
||
id
|
||
title
|
||
date_published_func {
|
||
weekday
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
import { createDirectus } from '@directus/sdk';
|
||
import { rest, readItems } from '@directus/sdk/rest';
|
||
|
||
const client = createDirectus('https://directus.example.com').with(rest());
|
||
|
||
const result = await client.request(
|
||
readItems('articles', {
|
||
fields: ['month(date_created)'],
|
||
})
|
||
);
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|
||
|
||
## Metadata
|
||
|
||
Metadata allows you to retrieve some additional information about the items in the collection you're fetching. `*` can
|
||
be used as a wildcard to retrieve all metadata.
|
||
|
||
### Total Count
|
||
|
||
Returns the total item count of the collection you're querying.
|
||
|
||
### Filter Count
|
||
|
||
Returns the item count of the collection you're querying, taking the current filter/search parameters into account.
|
||
|
||
::: warning GraphQL
|
||
|
||
GraphQL does not have meta fields like the REST API.
|
||
As an alternative, you can retrieve the count using Aggregation.
|
||
|
||
For more details, see: [Aggregation & Grouping](#aggregation-grouping)
|
||
|
||
:::
|
||
|
||
<SnippetToggler :choices="['REST', 'GraphQL', 'SDK']" label="API">
|
||
<template #rest>
|
||
|
||
```
|
||
?meta=total_count
|
||
|
||
?meta=filter_count
|
||
|
||
?meta=*
|
||
```
|
||
|
||
</template>
|
||
<template #graphql>
|
||
|
||
```graphql
|
||
query {
|
||
articles_aggregated {
|
||
count {
|
||
id
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|
||
</template>
|
||
<template #sdk>
|
||
|
||
```js
|
||
// Not applicable, use aggregate()
|
||
```
|
||
|
||
</template>
|
||
</SnippetToggler>
|