From f0e8df63c56dfd6fa73d664bc43b35838f818030 Mon Sep 17 00:00:00 2001 From: Jelmer Visser <39594925+JelmerV-WFC@users.noreply.github.com> Date: Tue, 13 Aug 2019 10:55:12 +0200 Subject: [PATCH] Fix custom mutation returning null and not executing policies (#3735) Fix custom mutation returning null and not executing policies --- .../services/Mutation.js | 46 ++++++++++++++----- .../strapi-plugin-graphql/services/Schema.js | 9 +++- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/packages/strapi-plugin-graphql/services/Mutation.js b/packages/strapi-plugin-graphql/services/Mutation.js index fec5c97b82..a9ca8c5990 100644 --- a/packages/strapi-plugin-graphql/services/Mutation.js +++ b/packages/strapi-plugin-graphql/services/Mutation.js @@ -195,27 +195,49 @@ module.exports = { // Resolver can be a function. Be also a native resolver or a controller's action. if (_.isFunction(resolver)) { - context.params = Query.convertToParams( - options.input.where || {}, - (plugin ? strapi.plugins[plugin].models[name] : strapi.models[name]) - .primaryKey - ); - context.request.body = options.input.data || {}; + const normalizedName = _.toLower(name); + + let primaryKey; + + if (plugin) { + primaryKey = strapi.plugins[plugin].models[normalizedName].primaryKey; + } else { + primaryKey = strapi.models[normalizedName].primaryKey; + } + + if (options.input && options.input.where) { + context.params = Query.convertToParams( + options.input.where || {}, + primaryKey + ); + } else { + context.params = {}; + } + + if (options.input && options.input.data) { + context.request.body = options.input.data || {}; + } else { + context.request.body = options; + } if (isController) { const values = await resolver.call(null, context); if (ctx.body) { - return { - [pluralize.singular(name)]: ctx.body, - }; + return options.input + ? { + [pluralize.singular(normalizedName)]: ctx.body, + } + : ctx.body; } const body = values && values.toJSON ? values.toJSON() : values; - return { - [pluralize.singular(name)]: body, - }; + return options.input + ? { + [pluralize.singular(normalizedName)]: body, + } + : body; } return resolver.call(null, obj, options, context); diff --git a/packages/strapi-plugin-graphql/services/Schema.js b/packages/strapi-plugin-graphql/services/Schema.js index efe896cc5c..78af4039db 100644 --- a/packages/strapi-plugin-graphql/services/Schema.js +++ b/packages/strapi-plugin-graphql/services/Schema.js @@ -205,14 +205,19 @@ const schemaBuilder = { : {}; switch (type) { - case 'Mutation': + case 'Mutation': { // TODO: Verify this... + const [name, action] = acc[type][resolver].split('.'); + const normalizedName = _.toLower(name); + acc[type][resolver] = Mutation.composeMutationResolver( strapi.plugins.graphql.config._schema.graphql, plugin, - resolver + normalizedName, + action ); break; + } case 'Query': default: acc[type][resolver] = Query.composeQueryResolver(