diff --git a/packages/strapi-plugin-graphql/services/__tests__/resolvers-builder.test.js b/packages/strapi-plugin-graphql/services/__tests__/resolvers-builder.test.js new file mode 100644 index 0000000000..4f79625848 --- /dev/null +++ b/packages/strapi-plugin-graphql/services/__tests__/resolvers-builder.test.js @@ -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'); + }); + }); +}); diff --git a/packages/strapi-plugin-graphql/services/resolvers-builder.js b/packages/strapi-plugin-graphql/services/resolvers-builder.js index f7924d4abb..aaf431f6d1 100644 --- a/packages/strapi-plugin-graphql/services/resolvers-builder.js +++ b/packages/strapi-plugin-graphql/services/resolvers-builder.js @@ -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;