Merge pull request #3372 from grom194/fix/destroy_delete_request

Remove graphql special condition for delete request
This commit is contained in:
Alexandre BODIN 2019-06-04 17:00:45 +02:00 committed by GitHub
commit d012aa4e9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,17 +43,24 @@ module.exports = {
const resolver = _.get(handler, `Mutation.${queryName}.resolver`);
if (_.isString(resolver) || _.isPlainObject(resolver)) {
const { handler = resolver } = _.isPlainObject(resolver) ? resolver : {};
const { handler = resolver } = _.isPlainObject(resolver)
? resolver
: {};
// Retrieve the controller's action to be executed.
const [name, action] = handler.split('.');
const controller = plugin
? _.get(strapi.plugins, `${plugin}.controllers.${_.toLower(name)}.${action}`)
? _.get(
strapi.plugins,
`${plugin}.controllers.${_.toLower(name)}.${action}`
)
: _.get(strapi.controllers, `${_.toLower(name)}.${action}`);
if (!controller) {
return new Error(`Cannot find the controller's action ${name}.${action}`);
return new Error(
`Cannot find the controller's action ${name}.${action}`
);
}
// We're going to return a controller instead.
@ -81,13 +88,17 @@ module.exports = {
// We're going to return a controller instead.
isController = true;
const controllers = plugin ? strapi.plugins[plugin].controllers : strapi.controllers;
const controllers = plugin
? strapi.plugins[plugin].controllers
: strapi.controllers;
// Try to find the controller that should be related to this model.
const controller = _.get(controllers, `${name}.${action}`);
if (!controller) {
return new Error(`Cannot find the controller's action ${name}.${action}`);
return new Error(
`Cannot find the controller's action ${name}.${action}`
);
}
// Push global policy to make sure the permissions will work as expected.
@ -96,7 +107,7 @@ module.exports = {
policyUtils.globalPolicy(
undefined,
{
handler: `${name}.${action === 'delete' ? 'destroy' : action}`,
handler: `${name}.${action}`,
},
undefined,
plugin
@ -121,11 +132,16 @@ module.exports = {
const [name, action] = resolverOf.split('.');
const controller = plugin
? _.get(strapi.plugins, `${plugin}.controllers.${_.toLower(name)}.${action}`)
? _.get(
strapi.plugins,
`${plugin}.controllers.${_.toLower(name)}.${action}`
)
: _.get(strapi.controllers, `${_.toLower(name)}.${action}`);
if (!controller) {
return new Error(`Cannot find the controller's action ${name}.${action}`);
return new Error(
`Cannot find the controller's action ${name}.${action}`
);
}
policiesFn[0] = policyUtils.globalPolicy(
@ -144,7 +160,13 @@ module.exports = {
// Populate policies.
policies.forEach(policy =>
policyUtils.get(policy, plugin, policiesFn, `GraphQL query "${queryName}"`, name)
policyUtils.get(
policy,
plugin,
policiesFn,
`GraphQL query "${queryName}"`,
name
)
);
return async (obj, options, { context }) => {
@ -159,7 +181,10 @@ module.exports = {
const policy = await strapi.koaMiddlewares.compose(policiesFn)(ctx);
// Policy doesn't always return errors but they update the current context.
if (_.isError(ctx.request.graphql) || _.get(ctx.request.graphql, 'isBoom')) {
if (
_.isError(ctx.request.graphql) ||
_.get(ctx.request.graphql, 'isBoom')
) {
return ctx.request.graphql;
}
@ -172,7 +197,8 @@ module.exports = {
if (_.isFunction(resolver)) {
context.params = Query.convertToParams(
options.input.where || {},
(plugin ? strapi.plugins[plugin].models[name] : strapi.models[name]).primaryKey
(plugin ? strapi.plugins[plugin].models[name] : strapi.models[name])
.primaryKey
);
context.request.body = options.input.data || {};