mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 08:19:07 +00:00
* 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:
parent
631969d4d7
commit
11a128ead2
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user