handle graphql exists type

This commit is contained in:
Elliot DeNolf
2020-10-16 21:42:55 -04:00
parent 0758f01120
commit 215df7ff9e
2 changed files with 91 additions and 11 deletions

View File

@@ -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,
},
};
}, {}),
});
};