Skip to content

Commit

Permalink
Fix context issue in custom GraphQL query and mutation (#5532)
Browse files Browse the repository at this point in the history
* fix context issue in custom query and mutation

Signed-off-by: harimkims <harimkims@gmail.com>

* merge only state-related context

Signed-off-by: harimkims <harimkims@gmail.com>

* roll back the code, fix the test instead

Signed-off-by: harimkims <harimkims@gmail.com>
  • Loading branch information
iicdii committed Mar 20, 2020
1 parent dae9cfa commit 40ea493
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 47 deletions.
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
25 changes: 5 additions & 20 deletions packages/strapi-plugin-users-permissions/test/graphql.test.e2e.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Helpers.
const { registerAndLogin } = require('../../../test/helpers/auth');

const {
createAuthRequest,
createRequest,
} = require('../../../test/helpers/request');
const { createAuthRequest, createRequest } = require('../../../test/helpers/request');

let authReq;
const data = {};
Expand All @@ -24,11 +21,7 @@ describe('Test Graphql user service', () => {
body: {
query: /* GraphQL */ `
mutation {
createUser(
input: {
data: { username: "test", email: "test", password: "test" }
}
) {
createUser(input: { data: { username: "test", email: "test", password: "test" } }) {
user {
id
username
Expand Down Expand Up @@ -60,13 +53,7 @@ describe('Test Graphql user service', () => {
query: /* GraphQL */ `
mutation {
createUser(
input: {
data: {
username: "test"
email: "test@strapi.io"
password: "test"
}
}
input: { data: { username: "test", email: "test@strapi.io", password: "test" } }
) {
user {
id
Expand All @@ -78,7 +65,7 @@ describe('Test Graphql user service', () => {
},
});

expect(res.statusCode).toBe(201);
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
data: {
createUser: {
Expand Down Expand Up @@ -139,9 +126,7 @@ describe('Test Graphql user service', () => {
body: {
query: /* GraphQL */ `
mutation updateUser($id: ID!) {
updateUser(
input: { where: { id: $id }, data: { username: "newUsername" } }
) {
updateUser(input: { where: { id: $id }, data: { username: "newUsername" } }) {
user {
id
username
Expand Down

0 comments on commit 40ea493

Please sign in to comment.