mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 15:44:59 +00:00
docs: add .js ext to graphql paths, fix code snip-
et formatting Signed-off-by: Robert Piosik <robertpiosik@gmail.com>
This commit is contained in:
parent
9414eb717b
commit
8675effa21
@ -50,7 +50,7 @@ You can also enable the Apollo server tracing feature, which is supported by the
|
||||
|
||||
You can edit these configurations by creating following file.
|
||||
|
||||
**Path —** `./extensions/graphql/config/settings.json`.
|
||||
**Path —** `./extensions/graphql/config/settings.json`
|
||||
|
||||
```json
|
||||
{
|
||||
@ -271,7 +271,7 @@ If you use a local plugin, the controller methods of your plugin are not created
|
||||
|
||||
If you've generated an API called `Restaurant` using the CLI `strapi generate:api restaurant` or the administration panel, your model looks like this:
|
||||
|
||||
**Path —** `./api/restaurant/models/Restaurant.settings.json`.
|
||||
**Path —** `./api/restaurant/models/Restaurant.settings.json`
|
||||
|
||||
```json
|
||||
{
|
||||
@ -478,9 +478,9 @@ Result
|
||||
|
||||
## Customise the GraphQL schema
|
||||
|
||||
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 `./extensions/*/config/schema.graphql`.
|
||||
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.js` file. This file has to be placed into the config folder of each API `./api/*/config/schema.graphql.js` or plugin `./extensions/*/config/schema.graphql.js`.
|
||||
|
||||
**Structure —** `schema.graphql`.
|
||||
**Structure —** `schema.graphql.js`.
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
@ -505,11 +505,11 @@ module.exports = {
|
||||
|
||||
Let say we are using the same previous `Restaurant` model.
|
||||
|
||||
**Path —** `./api/restaurant/config/schema.graphql`.
|
||||
**Path —** `./api/restaurant/config/schema.graphql.js`
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
definition:`
|
||||
definition: `
|
||||
enum RestaurantStatusInput {
|
||||
work
|
||||
open
|
||||
@ -530,37 +530,38 @@ module.exports = {
|
||||
},
|
||||
restaurants: {
|
||||
description: 'Return a list of restaurants', // Add a description to the query.
|
||||
deprecated: 'This query should not be used anymore. Please consider using restaurantsByChef instead.'
|
||||
deprecated:
|
||||
'This query should not be used anymore. Please consider using restaurantsByChef instead.',
|
||||
},
|
||||
restaurantsByChef: {
|
||||
description: 'Return the restaurants open by the chef',
|
||||
resolver: 'application::restaurant.restaurant.findByChef'
|
||||
resolver: 'application::restaurant.restaurant.findByChef',
|
||||
},
|
||||
restaurantsByCategories: {
|
||||
description: 'Return the restaurants open by the category',
|
||||
resolverOf: 'application::restaurant.restaurant.findByCategories', // Will apply the same policy on the custom resolver as the controller's action `findByCategories`.
|
||||
resolver: (obj, options, ctx) => {
|
||||
resolver: async (obj, options, ctx) => {
|
||||
// ctx is the context of the Koa request.
|
||||
await strapi.controllers.restaurants.findByCategories(ctx);
|
||||
|
||||
return ctx.body.restaurants || `There is no restaurant.`;
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
Mutation: {
|
||||
attachRestaurantToChef: {
|
||||
description: 'Attach a restaurant to an chef',
|
||||
policies: ['plugins::users-permissions.isAuthenticated', 'isOwner'],
|
||||
resolver: 'application::restaurant.restaurant.attachToChef'
|
||||
}
|
||||
}
|
||||
}
|
||||
resolver: 'application::restaurant.restaurant.attachToChef',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### Define a new type
|
||||
|
||||
Edit the `definition` attribute in one of the `schema.graphql` files of your project by using the GraphQL Type language string.
|
||||
Edit the `definition` attribute in one of the `schema.graphql.js` files of your project by using the GraphQL Type language string.
|
||||
|
||||
::: tip
|
||||
The easiest way is to create a new model using the CLI `strapi generate:model category --api restaurant`, so you don't need to customise anything.
|
||||
@ -641,7 +642,7 @@ module.exports = {
|
||||
|
||||
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.
|
||||
|
||||
**Path —** `./api/restaurant/models/Restaurant.settings.json`.
|
||||
**Path —** `./api/restaurant/models/Restaurant.settings.json`
|
||||
|
||||
```json
|
||||
{
|
||||
@ -670,13 +671,13 @@ One of the most powerful features of GraphQL is the auto-documentation of the sc
|
||||
}
|
||||
```
|
||||
|
||||
It might happen that you want to add a description to a query or deprecate it. To do that, you need to use the `schema.graphql` file.
|
||||
It might happen that you want to add a description to a query or deprecate it. To do that, you need to use the `schema.graphql.js` file.
|
||||
|
||||
::: warning
|
||||
The `schema.graphql` file has to be placed into the config folder of each API `./api/*/config/schema.graphql` or plugin `./extensions/*/config/schema.graphql`.
|
||||
The `schema.graphql.js` file has to be placed into the config folder of each API `./api/*/config/schema.graphql.js` or plugin `./extensions/*/config/schema.graphql.js`.
|
||||
:::
|
||||
|
||||
**Path —** `./api/restaurant/config/schema.graphql`.
|
||||
**Path —** `./api/restaurant/config/schema.graphql.js`
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
@ -778,27 +779,29 @@ module.exports = {
|
||||
resolver: (obj, options, { context }) => {
|
||||
// You can return a raw JSON object or a promise.
|
||||
|
||||
return [{
|
||||
name: 'My first blog restaurant',
|
||||
description: 'Whatever you want...'
|
||||
}];
|
||||
}
|
||||
}
|
||||
return [
|
||||
{
|
||||
name: 'My first blog restaurant',
|
||||
description: 'Whatever you want...',
|
||||
},
|
||||
];
|
||||
},
|
||||
},
|
||||
},
|
||||
Mutation: {
|
||||
updateRestaurant: {
|
||||
description: 'Update an existing restaurant',
|
||||
resolver: (obj, options, { context }) => {
|
||||
resolver: async (obj, options, { context }) => {
|
||||
// The `where` and `data` parameters passed as arguments
|
||||
// of the GraphQL mutation are available via the `context` object.
|
||||
const where = context.params;
|
||||
const data = context.request.body;
|
||||
|
||||
return await strapi.api.restaurant.services.restaurant.addRestaurant(data, where);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
@ -818,47 +821,49 @@ module.exports = {
|
||||
resolver: (obj, options, context) => {
|
||||
// You can return a raw JSON object or a promise.
|
||||
|
||||
return [{
|
||||
name: 'My first blog restaurant',
|
||||
description: 'Whatever you want...'
|
||||
}];
|
||||
}
|
||||
}
|
||||
return [
|
||||
{
|
||||
name: 'My first blog restaurant',
|
||||
description: 'Whatever you want...',
|
||||
},
|
||||
];
|
||||
},
|
||||
},
|
||||
},
|
||||
Mutation: {
|
||||
updateRestaurant: {
|
||||
description: 'Update an existing restaurant',
|
||||
resolverOf: 'application::restaurant.restaurant.update', // Will apply the same policy on the custom resolver than the controller's action `update` located in `Restaurant.js`.
|
||||
resolver: (obj, options, { context }) => {
|
||||
resolver: async (obj, options, { context }) => {
|
||||
const where = context.params;
|
||||
const data = context.request.body;
|
||||
|
||||
return await strapi.api.restaurant.services.restaurant.addRestaurant(data, where);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### Disable a query or a type
|
||||
|
||||
To do that, we need to use the `schema.graphql` like below:
|
||||
To do that, we need to use the `schema.graphql.js` like below:
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
type: {
|
||||
Restaurant: false // The Restaurant type won't be "queriable" or "mutable".
|
||||
}
|
||||
Restaurant: false, // The Restaurant type won't be "queriable" or "mutable".
|
||||
},
|
||||
resolver: {
|
||||
Query: {
|
||||
restaurants: false // The `restaurants` query will no longer be in the GraphQL schema.
|
||||
restaurants: false, // The `restaurants` query will no longer be in the GraphQL schema.
|
||||
},
|
||||
Mutation: {
|
||||
createRestaurant: false,
|
||||
deletePOst: false
|
||||
}
|
||||
}
|
||||
deletePOst: false,
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
@ -873,7 +878,7 @@ The type name is the global ID of the model. You can find the global ID of a mod
|
||||
We recommend putting 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.
|
||||
|
||||
::: tip
|
||||
It's not a bad practice to put the description and deprecated attribute in the `schema.graphql`, though.
|
||||
It's not a bad practice to put the description and deprecated attribute in the `schema.graphql.js`, though.
|
||||
:::
|
||||
|
||||
**Why are the "createdAt" and "updatedAt" field added to my type?**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user