> ⚠️ This feature requires the GraphQL plugin (not installed by default).
## Usage
To get started with GraphQL in your app, please install the plugin first. To do that, open your terminal and run the following command:
```
strapi install graphql
```
Then, start your app and open your browser at [http://localhost:1337/graphiql](http://localhost:1337/graphiql). You should see the interface (GraphiQL) that will help you to write GraphQL query to explore your data.
### Query API
In the section, we assume that the "Shadow CRUD" feature is enabled. For each model, the plugin auto-generates queries which just fit to your needs.
##### Fetch a single entry
-`id`: String
```
query {
user(id: "5aafe871ad624b7380d7a224") {
username
email
}
}
```
##### Fetch multiple entries
```
query {
users {
username
email
}
}
```
**Filters**
You can also apply different parameters to the query to make more complex queries.
-`limit` (integer): Define the number of returned entries.
-`start` (integer): Define the amount of entries to skip.
-`sort` (string): Define how the data should be sorted.
-`where` (object): Define the filters to apply in the query.
-`<field>`: Equals.
-`<field>_ne`: Not equals.
-`<field>_lt`: Lower than.
-`<field>_lte`: Lower than or equal to.
-`<field>_gt`: Greater than.
-`<field>_gte`: Lower than or equal to.
-`<field>_contains`: Contains.
Return the second decade of users which have an email that contains `@strapi.io` ordered by username.
Return the users which have been created after the March, 19th 2018 4:21 pm.
```
query {
users(where: {
createdAt_gt: "2018-03-19 16:21:07.161Z"
}) {
username
email
}
}
```
## Shadow CRUD
To simplify and automate the build of the GraphQL schema, we introduced the "Shadow CRUD" feature. It automatically generates the type definition, queries and resolvers based on your models. The feature also lets you make complex query with many arguments such as `limit`, `sort`, `start` and `where`.
#### Example
If you've generated an API called `Post` using the CLI `strapi generate:api post` or the administration panel, your model looks like this:
If you want to define a new scalar, input or enum types, this section is for you. To do so, you will have to create a `schema.graphql` file. This file has to be placed into the config folder of each API `./api/*/config/schema.graphql` or plugin `./plugins/*/config/schema.graphql`.
**Structure —** `schema.graphql`.
```
module.exports = {
definition: ``,
query: ``,
type: {},
resolver: {
Query: {}
}
};
```
-`definition` (string): let's you define new type, input, etc.
-`query` (string): where you add custom query.
-`type` (object): allows you to add description, deprecated field or disable the "Shadow CRUD" feature on a specific type.
-`resolver` (object):
-`Query` (object): let's you define custom resolver, policies for a query.
#### Example
Let say we are using the same previous `Post` model.
policy: ['plugins.users-permissions.isAuthenticated', 'isOwner'], // Apply the 'isAuthenticated' policy of the `Users & Permissions` plugin, then the 'isOwner' policy before executing the resolver.
},
posts: {
description: 'Return a list of posts', // Add a description to the query.
deprecated: 'This query should not be used anymore. Please consider using postsByAuthor instead.'
},
postsByAuthor: {
description: 'Return the posts published by the author',
One of the most powerful features of GraphQL is the auto-documentation of the schema. The GraphQL plugin allows you to add a description to a type, a field and a query. You can also deprecate a field or a query.
"deprecated": "We are not using the title anymore, it is auto-generated thanks to our powerful AI"
},
"content": {
"type": "text",
"description": "The content of the post."
},
"published": {
"type": "boolean",
"description": "Is the post published or not. Yes = true."
}
}
}
```
It might happens that you want to add a description to a query or deprecate it. To do that, you need to use the `schema.graphql` file.
> Remember: The `schema.graphql` file has to be placed into the config folder of each API `./api/*/config/schema.graphql` or plugin `./plugins/*/config/schema.graphql`.
**Path —** `./api/post/config/schema.graphql`.
```
module.exports = {
resolver: {
Query: {
posts: {
description: 'Return a list of posts', // Add a description to the query.
deprecated: 'This query should not be used anymore. Please consider using postsByAuthor instead.' // Deprecate the query and explain the reason why.
}
}
}
};
```
### Add/execute a policy before a resolver
Sometimes a query needs to be only accessible to authenticated user. To handle this, Strapi provides a solid policy system. A policy is a function executed before the final action (the resolver). You can define an array of policy that will be executed in order.
In this example, the policy `isAuthenticated` located in `./plugins/users-permissions/config/policies/isAuthenticated.js` will be executed first. Then, the `isOwner` policy located in the `Post` API `./api/post/config/policies/isOwner.js`. Next, it will execute the `logging` policy located in `./config/policies/logging.js`. Finally, the resolver will be executed.
> Note: There is no custom resolver in that case, so it will execute the default resolver provided by the "Shadow CRUD" feature.
By default, the plugin will execute the actions located in the controllers that has been generated via the Content-Type Builder plugin or the CLI. For example, the query `posts` is going to execute the logic inside the `find` action in the `Post.js` controller. It might happens that you want to execute another action or a custom logic for one of your query.
```
module.exports = {
resolver: {
Query: {
posts: {
description: 'Return a list of posts by author',
resolver: 'Post.findByAuthor'
}
}
}
};
```
In this example, it will execute the `findByAuthor` action of the `Post` controller. It also means that the resolver will apply on the `posts` query the permissions defined on the `findByAuthor` action (through the administration panel).
> Note: The `obj` parameter is available via `ctx.params` and the `options` are available via `ctx.query` in the controller's action.
### Define a custom resolver
```
module.exports = {
resolver: {
Query: {
posts: {
description: 'Return a list of posts by author',
resolver: (obj, options, context) => {
// You can return a raw JSON object or a promise.
return [{
title: 'My first blog post',
content: 'Whatever you want...'
}];
}
}
}
}
};
```
You can also execute a custom logic like above. However, the roles and permissions layers won't work.
The type name is the global ID of the model. You can find the global ID of a model like that `strapi.models[xxx].globalId` or `strapi.plugins[xxx].models[yyy].globalId`.
**Where should I put the field description and deprecated reason?**
We recommend to put the field description and deprecated reason in the model. Right now, the GraphQL plugin is the only which uses these fields. Another plugin could use this description in the future as well. However, sometimes you don't have the choice, especially when you're defining a custom type.
> Note: It's not a bad practice to put the description and deprecated attribute in the `schema.graphql`, though.
**Why are the "createdAt" and "updatedAt" field added to my type?**
The plugin detects if the `timestamps` option is set to `true` in the model. By default, when you generate an API this option is checked. Set it to `false` in your model to remove these fields.