handle graphql exists type
This commit is contained in:
@@ -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', () => {
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
}, {}),
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user