WIP query input types

This commit is contained in:
James
2020-04-03 20:11:52 -04:00
parent 7342861fcb
commit c47cef4ac3
6 changed files with 288 additions and 14 deletions

View File

@@ -10,18 +10,8 @@ const {
} = require('graphql');
const formatName = require('./formatName');
const getTypeWithNullable = (field, type) => {
if (field.required && !field.localized) {
return new GraphQLNonNull(type);
}
return type;
};
const combineParentName = (parent, name) => {
return formatName(`${parent ? `${parent}_` : ''}${name}`);
};
const combineParentName = require('./combineParentName');
const getTypeWithNullable = require('./combineParentName');
const buildObjectType = (config, { name, fields, parent }) => {
const getLocalizedType = (field, type) => {

View File

@@ -0,0 +1,244 @@
const {
GraphQLString,
GraphQLFloat,
GraphQLBoolean,
GraphQLList,
GraphQLInterfaceType,
GraphQLEnumType,
GraphQLObjectType,
} = require('graphql');
const formatName = require('./formatName');
const combineParentName = require('./combineParentName');
const getTypeWithOperators = require('./getTypeWithOperators');
const buildQueryInputObjectType = ({ name, fields, parent }) => {
const fieldToSchemaMap = {
number: (field) => {
const type = GraphQLFloat;
return {
type: getTypeWithOperators(
field,
type,
parent,
['equals', 'gte', 'gt', 'lte', 'lt', 'ne'],
),
};
},
text: (field) => {
const type = GraphQLString;
return {
type: getTypeWithOperators(
field,
type,
parent,
['equals', 'like', 'ne'],
),
};
},
email: (field) => {
const type = GraphQLString;
return {
type: getTypeWithOperators(
field,
type,
parent,
['equals', 'like', 'ne'],
),
};
},
textarea: (field) => {
const type = GraphQLString;
return {
type: getTypeWithOperators(
field,
type,
parent,
['equals', 'like', 'ne'],
),
};
},
WYSIWYG: (field) => {
const type = GraphQLString;
return {
type: getTypeWithOperators(
field,
type,
parent,
['equals', 'like', 'ne'],
),
};
},
code: (field) => {
const type = GraphQLString;
return {
type: getTypeWithOperators(
field,
type,
parent,
['equals', 'like', 'ne'],
),
};
},
date: (field) => {
const type = GraphQLString;
return {
type: getTypeWithOperators(
field,
type,
parent,
['equals', 'like', 'ne'],
),
};
},
upload: () => {
const type = GraphQLString;
return {
type,
};
},
checkbox: field => ({
type: getTypeWithOperators(
field,
GraphQLBoolean,
parent,
['equals', 'ne'],
),
}),
relationship: (field) => {
const type = GraphQLString;
const typeWithLocale = getLocalizedType(
field,
getTypeWithNullable(field, type),
);
return {
type: field.hasMany ? new GraphQLList(typeWithLocale) : typeWithLocale,
};
},
repeater: (field) => {
const fullName = combineParentName(parent, field.label);
const type = buildObjectType(config, {
name: fullName,
fields: field.fields,
parent: fullName,
});
const typeWithNullable = new GraphQLList(getTypeWithNullable(field, type));
return {
type: getLocalizedType(
field,
typeWithNullable,
),
};
},
group: (field) => {
const fullName = combineParentName(parent, field.label);
const type = buildObjectType(config, {
name: fullName,
fields: field.fields,
parent: fullName,
});
return {
type,
};
},
select: (field) => {
const fullName = combineParentName(parent, field.label);
const type = new GraphQLEnumType({
name: fullName,
values: field.options.reduce((values, option) => {
if (typeof option === 'object' && option.value) {
return {
...values,
[formatName(option.label)]: {
value: option.value,
},
};
}
if (typeof option === 'string') {
return {
...values,
[option]: {
value: option,
},
};
}
return values;
}, {}),
});
const typeWithList = field.hasMany ? new GraphQLList(type) : type;
const typeWithNullable = getTypeWithNullable(field, typeWithList);
return {
type: getLocalizedType(
field,
typeWithNullable,
),
};
},
flexible: (field) => {
const blockTypes = field.blocks.reduce((blocks, block) => {
const formattedBlockName = formatName(block.labels.singular);
const fullName = `${combineParentName(parent, field.label)}_${formattedBlockName}`;
return {
...blocks,
[block.slug]: buildObjectType(config, {
name: fullName,
fields: block.fields,
parent: fullName,
}),
};
}, {});
return {
type: getLocalizedType(
field,
new GraphQLList(new GraphQLInterfaceType({
name: combineParentName(parent, field.label),
fields: {
blockType: {
type: new GraphQLEnumType({
name: `${combineParentName(parent, field.label)}_BlockType`,
values: field.blocks.reduce((values, block) => {
return {
...values,
[block.slug]: {
value: block.slug,
},
};
}, {}),
}),
},
blockName: { type: GraphQLString },
},
resolveType(value) {
return blockTypes[value.blockType];
},
})),
),
};
},
};
return new GraphQLObjectType({
name,
fields: fields.reduce((schema, field) => {
const fieldName = formatName(field.label);
return {
...schema,
[fieldName]: fieldToSchemaMap[field.type](field),
};
}, {}),
});
};
module.exports = buildQueryInputObjectType;

View File

@@ -0,0 +1,7 @@
const formatName = require('./formatName');
const combineParentName = (parent, name) => {
return formatName(`${parent ? `${parent}_` : ''}${name}`);
};
module.exports = combineParentName;

View File

@@ -0,0 +1,11 @@
const { GraphQLNonNull } = require('graphql');
const getTypeWithNullable = (field, type) => {
if (field.required && !field.localized) {
return new GraphQLNonNull(type);
}
return type;
};
module.exports = getTypeWithNullable;

View File

@@ -0,0 +1,17 @@
const { GraphQLObjectType } = require('graphql');
const combineParentName = require('./combineParentName');
const getTypeWithOperators = (field, type, parent, operators) => {
const fullName = combineParentName(parent, field.label);
return new GraphQLObjectType({
name: fullName,
fields: operators.reduce((fields, operator) => {
return {
...fields,
[operator]: { type },
},
}, {}),
});
}
module.exports = getTypeWithOperators;

View File

@@ -1,7 +1,7 @@
const graphQLHTTP = require('express-graphql');
const { GraphQLObjectType, GraphQLString, GraphQLSchema } = require('graphql');
const buildType = require('./buildObjectType');
const loadPolicy = require('../express/middleware/loadPolicy');
const buildInputObjectType = require('./buildInputObjectType');
const Query = {
name: 'Query',
@@ -46,10 +46,15 @@ function init() {
parent: label,
});
collection.graphQLInputType = buildInputObjectType({
name: label,
fields: collection.config.fields,
});
Query.fields[label] = {
type: collection.graphQLType,
args: {
id: { type: GraphQLString },
data: collection.graphQLInputType,
},
resolve: async (_, { id }, context) => {
return {