mirror of
https://github.com/strapi/strapi.git
synced 2025-08-13 03:07:32 +00:00
Merge pull request #4008 from donmasakayan/graphql_policies_fix-v3
Graphql policies fix
This commit is contained in:
commit
f59b4a5c7b
@ -492,7 +492,7 @@ module.exports = {
|
|||||||
Query: {
|
Query: {
|
||||||
post: {
|
post: {
|
||||||
description: 'Return a single post',
|
description: 'Return a single post',
|
||||||
policy: ['plugins.users-permissions.isAuthenticated', 'isOwner'], // Apply the 'isAuthenticated' policy of the `Users & Permissions` plugin, then the 'isOwner' policy before executing the resolver.
|
policies: ['plugins.users-permissions.isAuthenticated', 'isOwner'], // Apply the 'isAuthenticated' policy of the `Users & Permissions` plugin, then the 'isOwner' policy before executing the resolver.
|
||||||
},
|
},
|
||||||
posts: {
|
posts: {
|
||||||
description: 'Return a list of posts', // Add a description to the query.
|
description: 'Return a list of posts', // Add a description to the query.
|
||||||
@ -504,7 +504,7 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
postsByTags: {
|
postsByTags: {
|
||||||
description: 'Return the posts published by the author',
|
description: 'Return the posts published by the author',
|
||||||
resolverOf: 'Post.findByTags', // Will apply the same policy on the custom resolver than the controller's action `findByTags`.
|
resolverOf: 'Post.findByTags', // Will apply the same policy on the custom resolver as the controller's action `findByTags`.
|
||||||
resolver: (obj, options, ctx) => {
|
resolver: (obj, options, ctx) => {
|
||||||
// ctx is the context of the Koa request.
|
// ctx is the context of the Koa request.
|
||||||
await strapi.controllers.posts.findByTags(ctx);
|
await strapi.controllers.posts.findByTags(ctx);
|
||||||
@ -516,7 +516,7 @@ module.exports = {
|
|||||||
Mutation: {
|
Mutation: {
|
||||||
attachPostToAuthor: {
|
attachPostToAuthor: {
|
||||||
description: 'Attach a post to an author',
|
description: 'Attach a post to an author',
|
||||||
policy: ['plugins.users-permissions.isAuthenticated', 'isOwner'],
|
policies: ['plugins.users-permissions.isAuthenticated', 'isOwner'],
|
||||||
resolver: 'Post.attachToAuthor'
|
resolver: 'Post.attachToAuthor'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -677,7 +677,7 @@ module.exports = {
|
|||||||
Query: {
|
Query: {
|
||||||
posts: {
|
posts: {
|
||||||
description: 'Return a list of posts',
|
description: 'Return a list of posts',
|
||||||
policy: [
|
policies: [
|
||||||
'plugins.users-permissions.isAuthenticated',
|
'plugins.users-permissions.isAuthenticated',
|
||||||
'isOwner',
|
'isOwner',
|
||||||
'global.logging',
|
'global.logging',
|
||||||
@ -687,7 +687,10 @@ module.exports = {
|
|||||||
Mutation: {
|
Mutation: {
|
||||||
createPost: {
|
createPost: {
|
||||||
description: 'Create a new post',
|
description: 'Create a new post',
|
||||||
policy: ['plugins.users-permissions.isAuthenticated', 'global.logging'],
|
policies: [
|
||||||
|
'plugins.users-permissions.isAuthenticated',
|
||||||
|
'global.logging',
|
||||||
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -782,7 +785,7 @@ module.exports = {
|
|||||||
Query: {
|
Query: {
|
||||||
posts: {
|
posts: {
|
||||||
description: 'Return a list of posts by author',
|
description: 'Return a list of posts by author',
|
||||||
resolverOf: 'Post.find', // Will apply the same policy on the custom resolver than the controller's action `find` located in `Post.js`.
|
resolverOf: 'Post.find', // Will apply the same policy on the custom resolver as the controller's action `find` located in `Post.js`.
|
||||||
resolver: (obj, options, context) => {
|
resolver: (obj, options, context) => {
|
||||||
// You can return a raw JSON object or a promise.
|
// You can return a raw JSON object or a promise.
|
||||||
|
|
||||||
|
@ -19,11 +19,15 @@ module.exports = {
|
|||||||
* @return Promise or Error.
|
* @return Promise or Error.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
composeMutationResolver: function(_schema, plugin, name, action) {
|
composeMutationResolver: function({ _schema, plugin, name, action }) {
|
||||||
// Extract custom resolver or type description.
|
// Extract custom resolver or type description.
|
||||||
const { resolver: handler = {} } = _schema;
|
const { resolver: handler = {} } = _schema;
|
||||||
|
|
||||||
const queryName = `${action}${_.capitalize(name)}`;
|
let queryName = `${action}${_.capitalize(name)}`;
|
||||||
|
|
||||||
|
if (_.has(handler, `Mutation.${action}`)) {
|
||||||
|
queryName = action;
|
||||||
|
}
|
||||||
|
|
||||||
// Retrieve policies.
|
// Retrieve policies.
|
||||||
const policies = _.get(handler, `Mutation.${queryName}.policies`, []);
|
const policies = _.get(handler, `Mutation.${queryName}.policies`, []);
|
||||||
@ -155,7 +159,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strapi.plugins['users-permissions']) {
|
if (strapi.plugins['users-permissions']) {
|
||||||
policies.push('plugins.users-permissions.permissions');
|
policies.unshift('plugins.users-permissions.permissions');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate policies.
|
// Populate policies.
|
||||||
|
@ -69,7 +69,7 @@ module.exports = {
|
|||||||
* @return Promise or Error.
|
* @return Promise or Error.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
composeQueryResolver: function(_schema, plugin, name, isSingular) {
|
composeQueryResolver: function({ _schema, plugin, name, isSingular }) {
|
||||||
const params = {
|
const params = {
|
||||||
model: name,
|
model: name,
|
||||||
};
|
};
|
||||||
@ -236,7 +236,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strapi.plugins['users-permissions']) {
|
if (strapi.plugins['users-permissions']) {
|
||||||
policies.push('plugins.users-permissions.permissions');
|
policies.unshift('plugins.users-permissions.permissions');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate policies.
|
// Populate policies.
|
||||||
|
@ -326,11 +326,21 @@ const buildShadowCRUD = (models, plugin) => {
|
|||||||
const queries = {
|
const queries = {
|
||||||
singular:
|
singular:
|
||||||
_.get(resolver, `Query.${singularName}`) !== false
|
_.get(resolver, `Query.${singularName}`) !== false
|
||||||
? Query.composeQueryResolver(_schema, plugin, name, true)
|
? Query.composeQueryResolver({
|
||||||
|
_schema,
|
||||||
|
plugin,
|
||||||
|
name,
|
||||||
|
isSingular: true,
|
||||||
|
})
|
||||||
: null,
|
: null,
|
||||||
plural:
|
plural:
|
||||||
_.get(resolver, `Query.${pluralName}`) !== false
|
_.get(resolver, `Query.${pluralName}`) !== false
|
||||||
? Query.composeQueryResolver(_schema, plugin, name, false)
|
? Query.composeQueryResolver({
|
||||||
|
_schema,
|
||||||
|
plugin,
|
||||||
|
name,
|
||||||
|
isSingular: false,
|
||||||
|
})
|
||||||
: null,
|
: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -376,15 +386,30 @@ const buildShadowCRUD = (models, plugin) => {
|
|||||||
const mutations = {
|
const mutations = {
|
||||||
create:
|
create:
|
||||||
_.get(resolver, `Mutation.create${capitalizedName}`) !== false
|
_.get(resolver, `Mutation.create${capitalizedName}`) !== false
|
||||||
? Mutation.composeMutationResolver(_schema, plugin, name, 'create')
|
? Mutation.composeMutationResolver({
|
||||||
|
_schema,
|
||||||
|
plugin,
|
||||||
|
name,
|
||||||
|
action: 'create',
|
||||||
|
})
|
||||||
: null,
|
: null,
|
||||||
update:
|
update:
|
||||||
_.get(resolver, `Mutation.update${capitalizedName}`) !== false
|
_.get(resolver, `Mutation.update${capitalizedName}`) !== false
|
||||||
? Mutation.composeMutationResolver(_schema, plugin, name, 'update')
|
? Mutation.composeMutationResolver({
|
||||||
|
_schema,
|
||||||
|
plugin,
|
||||||
|
name,
|
||||||
|
action: 'update',
|
||||||
|
})
|
||||||
: null,
|
: null,
|
||||||
delete:
|
delete:
|
||||||
_.get(resolver, `Mutation.delete${capitalizedName}`) !== false
|
_.get(resolver, `Mutation.delete${capitalizedName}`) !== false
|
||||||
? Mutation.composeMutationResolver(_schema, plugin, name, 'delete')
|
? Mutation.composeMutationResolver({
|
||||||
|
_schema,
|
||||||
|
plugin,
|
||||||
|
name,
|
||||||
|
action: 'delete',
|
||||||
|
})
|
||||||
: null,
|
: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -228,22 +228,22 @@ const schemaBuilder = {
|
|||||||
const [name, action] = acc[type][resolver].split('.');
|
const [name, action] = acc[type][resolver].split('.');
|
||||||
const normalizedName = _.toLower(name);
|
const normalizedName = _.toLower(name);
|
||||||
|
|
||||||
acc[type][resolver] = Mutation.composeMutationResolver(
|
acc[type][resolver] = Mutation.composeMutationResolver({
|
||||||
strapi.plugins.graphql.config._schema.graphql,
|
_schema: strapi.plugins.graphql.config._schema.graphql,
|
||||||
plugin,
|
plugin,
|
||||||
normalizedName,
|
name: normalizedName,
|
||||||
action
|
action,
|
||||||
);
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'Query':
|
case 'Query':
|
||||||
default:
|
default:
|
||||||
acc[type][resolver] = Query.composeQueryResolver(
|
acc[type][resolver] = Query.composeQueryResolver({
|
||||||
strapi.plugins.graphql.config._schema.graphql,
|
_schema: strapi.plugins.graphql.config._schema.graphql,
|
||||||
plugin,
|
plugin,
|
||||||
resolver,
|
name: resolver,
|
||||||
'force' // Avoid singular/pluralize and force query name.
|
isSingular: 'force', // Avoid singular/pluralize and force query name.
|
||||||
);
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user