mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 23:57:32 +00:00
Update GraphQL documentation and fix context request
This commit is contained in:
parent
3daf7523c8
commit
69714842a3
@ -181,9 +181,7 @@ module.exports = {
|
||||
},
|
||||
postsByAuthor: {
|
||||
description: 'Return the posts published by the author',
|
||||
resolver: (obj, options, context) => {
|
||||
return strapi.api.post.services.post.fetchAll({ where: { author: obj.id }, limit: obj.limit || 20 });
|
||||
}
|
||||
resolver: 'Post.findByAuthor'
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -242,17 +240,7 @@ module.exports = {
|
||||
Query: {
|
||||
person: {
|
||||
description: 'Return a single person',
|
||||
resolver: (obj, options, context) => {
|
||||
// This is where you can the function that will resolve the query
|
||||
|
||||
return {
|
||||
id: 1,
|
||||
firstname: 'John',
|
||||
lastname: 'Doe',
|
||||
age: 21,
|
||||
children: []
|
||||
}
|
||||
}
|
||||
resolver: 'Person.findOne'
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -330,6 +318,51 @@ In this example, the policy `isAuthenticated` located in `./plugins/users-permis
|
||||
|
||||
> Note: There is no custom resolver in that case, so it will execute the default resolver provided by the "Shadow CRUD" feature.
|
||||
|
||||
### Link a query to a controller action.
|
||||
|
||||
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.
|
||||
|
||||
## FAQ
|
||||
|
||||
**How are the types name defined?**
|
||||
|
||||
@ -50,4 +50,4 @@
|
||||
"npm": ">= 3.0.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
@ -203,7 +203,28 @@ module.exports = {
|
||||
_.get(handler, `Query.${pluralize.singular(name)}.resolver`):
|
||||
_.get(handler, `Query.${pluralize.plural(name)}.resolver`);
|
||||
|
||||
if (resolver) {
|
||||
if (_.isString(resolver)) {
|
||||
// Retrieve the controller's action to be executed.
|
||||
const [ name, action ] = resolver.split('.');
|
||||
|
||||
const controller = _.get(strapi.controllers, `${_.toLower(name)}.${action}`);
|
||||
|
||||
if (!controller) {
|
||||
return new Error(`Cannot find the controller's action ${name}.${action}`);
|
||||
}
|
||||
|
||||
// We're going to return a controller instead.
|
||||
isController = true;
|
||||
|
||||
return async (ctx, next) => {
|
||||
ctx.query = this.convertToParams(options);
|
||||
ctx.params = obj;
|
||||
|
||||
// Return the controller.
|
||||
return controller(ctx, next);
|
||||
}
|
||||
} else {
|
||||
// Function.
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@ -261,9 +282,9 @@ module.exports = {
|
||||
|
||||
// Hack to be able to handle permissions for each query.
|
||||
const ctx = Object.assign(_.clone(context), {
|
||||
request: {
|
||||
request: Object.assign(_.clone(context.request), {
|
||||
graphql: null
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// Execute policies stack.
|
||||
|
||||
@ -103,8 +103,6 @@ module.exports = {
|
||||
plugin
|
||||
};
|
||||
|
||||
console.log("CTX", ctx.request.route);
|
||||
|
||||
await next();
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user