handle graphql exists type
This commit is contained in:
@@ -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