mirror of
https://github.com/strapi/strapi.git
synced 2025-12-15 17:22:57 +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 });
|
||||
|
||||
await policiesMiddleware(ctx);
|
||||
graphqlContext.context = ctx;
|
||||
|
||||
return resolver(root, options, graphqlContext);
|
||||
};
|
||||
}
|
||||
@ -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 || {});
|
||||
@ -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);
|
||||
};
|
||||
}
|
||||
@ -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.');
|
||||
}
|
||||
|
||||
@ -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.
|
||||
@ -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 => {
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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 = {};
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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: {
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user