Fix custom mutation returning null and not executing policies (#3735)

Fix custom mutation returning null and not executing policies
This commit is contained in:
Jelmer Visser 2019-08-13 10:55:12 +02:00 committed by Alexandre BODIN
parent a2770d7c7f
commit f0e8df63c5
2 changed files with 41 additions and 14 deletions

View File

@ -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);

View File

@ -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(