From 215df7ff9eedb2c2d78dff38d3d449b5cb21e01f Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Fri, 16 Oct 2020 21:42:55 -0400 Subject: [PATCH] handle graphql exists type --- .../graphql/resolvers/resolvers.spec.js | 73 +++++++++++++++++++ src/graphql/schema/withOperators.js | 29 +++++--- 2 files changed, 91 insertions(+), 11 deletions(-) diff --git a/src/collections/graphql/resolvers/resolvers.spec.js b/src/collections/graphql/resolvers/resolvers.spec.js index c812cfd96f..f73c494d12 100644 --- a/src/collections/graphql/resolvers/resolvers.spec.js +++ b/src/collections/graphql/resolvers/resolvers.spec.js @@ -97,6 +97,79 @@ describe('GrahpQL Resolvers', () => { expect(retrievedId).toStrictEqual(id); }); + + it('should query exists - true', async () => { + const title = 'gql read'; + const description = 'description'; + const summary = 'summary'; + + // language=graphQL + const query = `mutation { + createLocalizedPost(data: {title: "${title}", description: "${description}", summary: "${summary}", priority: 10}) { + id + title + description + priority + createdAt + updatedAt + } + }`; + + const response = await client.request(query); + + const { id } = response.createLocalizedPost; + // language=graphQL + const readQuery = `query { + LocalizedPosts(where: { summary: { exists: true }}) { + docs { + id + description + summary + } + } +}`; + const readResponse = await client.request(readQuery); + const retrievedId = readResponse.LocalizedPosts.docs[0].id; + + expect(readResponse.LocalizedPosts.docs).toHaveLength(1); + expect(retrievedId).toStrictEqual(id); + }); + + it('should query exists - false', async () => { + const title = 'gql read'; + const description = 'description'; + + // language=graphQL + const query = `mutation { + createLocalizedPost(data: {title: "${title}", description: "${description}", priority: 10}) { + id + title + description + priority + createdAt + updatedAt + } + }`; + + const response = await client.request(query); + + const { id } = response.createLocalizedPost; + // language=graphQL + const readQuery = `query { + LocalizedPosts(where: { summary: { exists: false }}) { + docs { + id + summary + } + } +}`; + const readResponse = await client.request(readQuery); + const retrievedDoc = readResponse.LocalizedPosts.docs[0]; + + expect(readResponse.LocalizedPosts.docs.length).toBeGreaterThan(0); + expect(retrievedDoc.id).toStrictEqual(id); + expect(retrievedDoc.summary).toBeNull(); + }); }); describe('Update', () => { diff --git a/src/graphql/schema/withOperators.js b/src/graphql/schema/withOperators.js index 6ee8fba886..b1d3b750ee 100644 --- a/src/graphql/schema/withOperators.js +++ b/src/graphql/schema/withOperators.js @@ -1,23 +1,30 @@ -const { GraphQLList, GraphQLInputObjectType } = require('graphql'); +const { GraphQLList, GraphQLInputObjectType, GraphQLBoolean } = require('graphql'); const combineParentName = require('../utilities/combineParentName'); const withOperators = (field, type, parent, operators) => { const name = `${combineParentName(parent, field.name)}_operator`; const listOperators = ['in', 'not_in', 'all']; - if (!field.required) { - console.log('name', field.name); - operators.push('exists'); - } + if (!field.required) operators.push('exists'); return new GraphQLInputObjectType({ name, - fields: operators.reduce((fields, operator) => ({ - ...fields, - [operator]: { - type: listOperators.indexOf(operator) > -1 ? new GraphQLList(type) : type, - }, - }), {}), + fields: operators.reduce((fields, operator) => { + let gqlType; + if (listOperators.indexOf(operator) > -1) { + gqlType = new GraphQLList(type); + } else if (operator === 'exists') { + gqlType = GraphQLBoolean; + } else { + gqlType = type; + } + return { + ...fields, + [operator]: { + type: gqlType, + }, + }; + }, {}), }); };