mirror of
https://github.com/strapi/strapi.git
synced 2025-12-16 09:45:08 +00:00
Fix context issue in custom GraphQL query and mutation (#5532)
* 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>
This commit is contained in:
parent
dae9cfa415
commit
40ea493b26
@ -27,6 +27,8 @@ const buildMutation = (mutationName, config) => {
|
|||||||
const ctx = buildMutationContext({ options, graphqlContext });
|
const ctx = buildMutationContext({ options, graphqlContext });
|
||||||
|
|
||||||
await policiesMiddleware(ctx);
|
await policiesMiddleware(ctx);
|
||||||
|
graphqlContext.context = ctx;
|
||||||
|
|
||||||
return resolver(root, options, graphqlContext);
|
return resolver(root, options, graphqlContext);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -52,10 +54,7 @@ const buildMutation = (mutationName, config) => {
|
|||||||
const buildMutationContext = ({ options, graphqlContext }) => {
|
const buildMutationContext = ({ options, graphqlContext }) => {
|
||||||
const { context } = graphqlContext;
|
const { context } = graphqlContext;
|
||||||
|
|
||||||
const ctx = context.app.createContext(
|
const ctx = context.app.createContext(_.clone(context.req), _.clone(context.res));
|
||||||
_.clone(context.req),
|
|
||||||
_.clone(context.res)
|
|
||||||
);
|
|
||||||
|
|
||||||
if (options.input && options.input.where) {
|
if (options.input && options.input.where) {
|
||||||
ctx.params = convertToParams(options.input.where || {});
|
ctx.params = convertToParams(options.input.where || {});
|
||||||
@ -89,6 +88,8 @@ const buildQuery = (queryName, config) => {
|
|||||||
const { ctx, opts } = buildQueryContext({ options, graphqlContext });
|
const { ctx, opts } = buildQueryContext({ options, graphqlContext });
|
||||||
|
|
||||||
await policiesMiddleware(ctx);
|
await policiesMiddleware(ctx);
|
||||||
|
graphqlContext.context = ctx;
|
||||||
|
|
||||||
return resolver(root, opts, graphqlContext);
|
return resolver(root, opts, graphqlContext);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -119,10 +120,7 @@ const validateResolverOption = config => {
|
|||||||
throw new Error(`Missing "resolverOf" option with custom resolver.`);
|
throw new Error(`Missing "resolverOf" option with custom resolver.`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (!_.isUndefined(policies) && (!Array.isArray(policies) || !_.every(policies, _.isString))) {
|
||||||
!_.isUndefined(policies) &&
|
|
||||||
(!Array.isArray(policies) || !_.every(policies, _.isString))
|
|
||||||
) {
|
|
||||||
throw new Error('Policies option must be an array of string.');
|
throw new Error('Policies option must be an array of string.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,10 +131,7 @@ const buildQueryContext = ({ options, graphqlContext }) => {
|
|||||||
const { context } = graphqlContext;
|
const { context } = graphqlContext;
|
||||||
const _options = _.cloneDeep(options);
|
const _options = _.cloneDeep(options);
|
||||||
|
|
||||||
const ctx = context.app.createContext(
|
const ctx = context.app.createContext(_.clone(context.req), _.clone(context.res));
|
||||||
_.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
|
// 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.
|
// cause a lost of the Object prototype.
|
||||||
@ -173,20 +168,10 @@ const getActionFn = details => {
|
|||||||
const { controller, action, plugin, api } = details;
|
const { controller, action, plugin, api } = details;
|
||||||
|
|
||||||
if (plugin) {
|
if (plugin) {
|
||||||
return _.get(strapi.plugins, [
|
return _.get(strapi.plugins, [_.toLower(plugin), 'controllers', _.toLower(controller), action]);
|
||||||
_.toLower(plugin),
|
|
||||||
'controllers',
|
|
||||||
_.toLower(controller),
|
|
||||||
action,
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return _.get(strapi.api, [
|
return _.get(strapi.api, [_.toLower(api), 'controllers', _.toLower(controller), action]);
|
||||||
_.toLower(api),
|
|
||||||
'controllers',
|
|
||||||
_.toLower(controller),
|
|
||||||
action,
|
|
||||||
]);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const getActionDetails = resolver => {
|
const getActionDetails = resolver => {
|
||||||
@ -234,9 +219,7 @@ const getPolicies = config => {
|
|||||||
|
|
||||||
const policyFns = [];
|
const policyFns = [];
|
||||||
|
|
||||||
const { controller, action, plugin: pathPlugin } = isResolvablePath(
|
const { controller, action, plugin: pathPlugin } = isResolvablePath(resolverOf)
|
||||||
resolverOf
|
|
||||||
)
|
|
||||||
? getActionDetails(resolverOf)
|
? getActionDetails(resolverOf)
|
||||||
: getActionDetails(resolver);
|
: getActionDetails(resolver);
|
||||||
|
|
||||||
|
|||||||
@ -1,10 +1,7 @@
|
|||||||
// Helpers.
|
// Helpers.
|
||||||
const { registerAndLogin } = require('../../../test/helpers/auth');
|
const { registerAndLogin } = require('../../../test/helpers/auth');
|
||||||
|
|
||||||
const {
|
const { createAuthRequest, createRequest } = require('../../../test/helpers/request');
|
||||||
createAuthRequest,
|
|
||||||
createRequest,
|
|
||||||
} = require('../../../test/helpers/request');
|
|
||||||
|
|
||||||
let authReq;
|
let authReq;
|
||||||
const data = {};
|
const data = {};
|
||||||
@ -24,11 +21,7 @@ describe('Test Graphql user service', () => {
|
|||||||
body: {
|
body: {
|
||||||
query: /* GraphQL */ `
|
query: /* GraphQL */ `
|
||||||
mutation {
|
mutation {
|
||||||
createUser(
|
createUser(input: { data: { username: "test", email: "test", password: "test" } }) {
|
||||||
input: {
|
|
||||||
data: { username: "test", email: "test", password: "test" }
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
user {
|
user {
|
||||||
id
|
id
|
||||||
username
|
username
|
||||||
@ -60,13 +53,7 @@ describe('Test Graphql user service', () => {
|
|||||||
query: /* GraphQL */ `
|
query: /* GraphQL */ `
|
||||||
mutation {
|
mutation {
|
||||||
createUser(
|
createUser(
|
||||||
input: {
|
input: { data: { username: "test", email: "test@strapi.io", password: "test" } }
|
||||||
data: {
|
|
||||||
username: "test"
|
|
||||||
email: "test@strapi.io"
|
|
||||||
password: "test"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
) {
|
) {
|
||||||
user {
|
user {
|
||||||
id
|
id
|
||||||
@ -78,7 +65,7 @@ describe('Test Graphql user service', () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(res.statusCode).toBe(201);
|
expect(res.statusCode).toBe(200);
|
||||||
expect(res.body).toMatchObject({
|
expect(res.body).toMatchObject({
|
||||||
data: {
|
data: {
|
||||||
createUser: {
|
createUser: {
|
||||||
@ -139,9 +126,7 @@ describe('Test Graphql user service', () => {
|
|||||||
body: {
|
body: {
|
||||||
query: /* GraphQL */ `
|
query: /* GraphQL */ `
|
||||||
mutation updateUser($id: ID!) {
|
mutation updateUser($id: ID!) {
|
||||||
updateUser(
|
updateUser(input: { where: { id: $id }, data: { username: "newUsername" } }) {
|
||||||
input: { where: { id: $id }, data: { username: "newUsername" } }
|
|
||||||
) {
|
|
||||||
user {
|
user {
|
||||||
id
|
id
|
||||||
username
|
username
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user