Fix #10125 - Accept falsy values returned by controllers in GraphQL queries and mutations (#10145)

* Fix #10125 - accept falsy values returned by controllers

* Add test for falsy ctx.body on built graphql resolver

* Use test instead of it in unit tests
This commit is contained in:
Guilherme Pacheco 2021-04-26 11:46:37 -03:00 committed by GitHub
parent 631969d4d7
commit 11a128ead2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 124 additions and 2 deletions

View File

@ -0,0 +1,122 @@
'use strict';
const { buildMutation, buildQuery } = require('../resolvers-builder');
global.strapi = {
plugins: {
graphql: {
config: {},
},
},
api: {
'my-api': {
controllers: {
'my-controller': {},
},
},
},
};
const graphqlContext = {
context: {
req: {},
res: {},
app: {
createContext(request, response) {
return { request, response };
},
},
},
};
describe('Resolvers builder', () => {
describe('buildMutation', () => {
test("Returns ctx.body if it's not falsy and the resolver is a string", async () => {
expect.assertions(1);
strapi.api['my-api'].controllers['my-controller'].myAction = async ctx => {
ctx.body = 1;
};
const resolver = buildMutation('mutation', {
resolver: 'application::my-api.my-controller.myAction',
});
const result = await resolver(null, {}, graphqlContext);
expect(result).toBe(1);
});
test("Returns ctx.body if it's not undefined and the resolver is a string", async () => {
expect.assertions(1);
strapi.api['my-api'].controllers['my-controller'].myAction = async ctx => {
ctx.body = 0;
};
const resolver = buildMutation('mutation', {
resolver: 'application::my-api.my-controller.myAction',
});
const result = await resolver(null, {}, graphqlContext);
expect(result).toBe(0);
});
test('Returns the action result if ctx.body is undefined and the resolver is a string', async () => {
expect.assertions(1);
strapi.api['my-api'].controllers['my-controller'].myAction = async () => 'result';
const resolver = buildMutation('mutation', {
resolver: 'application::my-api.my-controller.myAction',
});
const result = await resolver(null, {}, graphqlContext);
expect(result).toBe('result');
});
});
describe('buildQuery', () => {
test("Returns ctx.body if it's not falsy and the resolver is a string", async () => {
expect.assertions(1);
strapi.api['my-api'].controllers['my-controller'].myAction = async ctx => {
ctx.body = 1;
};
const resolver = buildQuery('mutation', {
resolver: 'application::my-api.my-controller.myAction',
});
const result = await resolver(null, {}, graphqlContext);
expect(result).toBe(1);
});
test("Returns ctx.body if it's not undefined and the resolver is a string", async () => {
expect.assertions(1);
strapi.api['my-api'].controllers['my-controller'].myAction = async ctx => {
ctx.body = 0;
};
const resolver = buildQuery('mutation', {
resolver: 'application::my-api.my-controller.myAction',
});
const result = await resolver(null, {}, graphqlContext);
expect(result).toBe(0);
});
test('Returns the action result if ctx.body is undefined and the resolver is a string', async () => {
expect.assertions(1);
strapi.api['my-api'].controllers['my-controller'].myAction = async () => 'result';
const resolver = buildQuery('mutation', {
resolver: 'application::my-api.my-controller.myAction',
});
const result = await resolver(null, {}, graphqlContext);
expect(result).toBe('result');
});
});
});

View File

@ -48,7 +48,7 @@ const buildMutation = (mutationName, config) => {
await policiesMiddleware(ctx);
const values = await action(ctx);
const result = ctx.body || values;
const result = ctx.body !== undefined ? ctx.body : values;
if (_.isError(result)) {
throw result;
@ -114,7 +114,7 @@ const buildQuery = (queryName, config) => {
await policiesMiddleware(ctx);
const values = await action(ctx);
const result = ctx.body || values;
const result = ctx.body !== undefined ? ctx.body : values;
if (_.isError(result)) {
throw result;