Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context issue in custom GraphQL query and mutation #5532

Merged
merged 4 commits into from
Mar 20, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 10 additions & 27 deletions packages/strapi-plugin-graphql/services/resolvers-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const buildMutation = (mutationName, config) => {
const ctx = buildMutationContext({ options, graphqlContext });

await policiesMiddleware(ctx);
graphqlContext.context = ctx;

return resolver(root, options, graphqlContext);
};
}
Expand All @@ -52,10 +54,7 @@ const buildMutation = (mutationName, config) => {
const buildMutationContext = ({ options, graphqlContext }) => {
const { context } = graphqlContext;

const ctx = context.app.createContext(
_.clone(context.req),
_.clone(context.res)
);
const ctx = context.app.createContext(_.clone(context.req), _.clone(context.res));

if (options.input && options.input.where) {
ctx.params = convertToParams(options.input.where || {});
Expand Down Expand Up @@ -89,6 +88,8 @@ const buildQuery = (queryName, config) => {
const { ctx, opts } = buildQueryContext({ options, graphqlContext });

await policiesMiddleware(ctx);
graphqlContext.context = ctx;

return resolver(root, opts, graphqlContext);
};
}
Expand Down Expand Up @@ -119,10 +120,7 @@ const validateResolverOption = config => {
throw new Error(`Missing "resolverOf" option with custom resolver.`);
}

if (
!_.isUndefined(policies) &&
(!Array.isArray(policies) || !_.every(policies, _.isString))
) {
if (!_.isUndefined(policies) && (!Array.isArray(policies) || !_.every(policies, _.isString))) {
throw new Error('Policies option must be an array of string.');
}

Expand All @@ -133,10 +131,7 @@ const buildQueryContext = ({ options, graphqlContext }) => {
const { context } = graphqlContext;
const _options = _.cloneDeep(options);

const ctx = context.app.createContext(
_.clone(context.req),
_.clone(context.res)
);
const ctx = context.app.createContext(_.clone(context.req), _.clone(context.res));

// Note: we've to used the Object.defineProperties to reset the prototype. It seems that the cloning the context
// cause a lost of the Object prototype.
Expand Down Expand Up @@ -173,20 +168,10 @@ const getActionFn = details => {
const { controller, action, plugin, api } = details;

if (plugin) {
return _.get(strapi.plugins, [
_.toLower(plugin),
'controllers',
_.toLower(controller),
action,
]);
return _.get(strapi.plugins, [_.toLower(plugin), 'controllers', _.toLower(controller), action]);
}

return _.get(strapi.api, [
_.toLower(api),
'controllers',
_.toLower(controller),
action,
]);
return _.get(strapi.api, [_.toLower(api), 'controllers', _.toLower(controller), action]);
};

const getActionDetails = resolver => {
Expand Down Expand Up @@ -234,9 +219,7 @@ const getPolicies = config => {

const policyFns = [];

const { controller, action, plugin: pathPlugin } = isResolvablePath(
resolverOf
)
const { controller, action, plugin: pathPlugin } = isResolvablePath(resolverOf)
? getActionDetails(resolverOf)
: getActionDetails(resolver);

Expand Down