WIP - replaces graphql-playground-express-middleware with react counterpart
This commit is contained in:
@@ -1,52 +1,85 @@
|
||||
const { GraphQLScalarType } = require('graphql');
|
||||
const { Kind, print } = require('graphql/language');
|
||||
const combineParentName = require('../utilities/combineParentName');
|
||||
|
||||
function parseObject(typeName, ast, variables) {
|
||||
const value = Object.create(null);
|
||||
ast.fields.forEach((field) => {
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
value[field.name.value] = parseLiteral(typeName, field.value, variables);
|
||||
});
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function parseLiteral(typeName, ast, variables) {
|
||||
switch (ast.kind) {
|
||||
case Kind.STRING:
|
||||
case Kind.BOOLEAN:
|
||||
return ast.value;
|
||||
case Kind.INT:
|
||||
case Kind.FLOAT:
|
||||
return parseFloat(ast.value);
|
||||
case Kind.OBJECT:
|
||||
return parseObject(typeName, ast, variables);
|
||||
case Kind.LIST:
|
||||
return ast.values.map(n => parseLiteral(typeName, n, variables));
|
||||
case Kind.NULL:
|
||||
return null;
|
||||
case Kind.VARIABLE:
|
||||
return variables ? variables[ast.name.value] : undefined;
|
||||
default:
|
||||
throw new TypeError(`${typeName} cannot represent value: ${print(ast)}`);
|
||||
}
|
||||
}
|
||||
|
||||
function buildBlockInputType(name, blocks, parentName) {
|
||||
const fullName = combineParentName(parentName, name);
|
||||
|
||||
const validate = (value) => {
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((blockValue) => {
|
||||
const blockToValidate = blocks.find(block => block.slug === blockValue.blockType);
|
||||
const blockToValidate = blocks.find(block => block.slug === value.blockType);
|
||||
|
||||
if (!blockToValidate) {
|
||||
throw new Error(`${fullName} tried to set a block type of ${blockValue}, but no corresponding block was found.`);
|
||||
}
|
||||
|
||||
const allRequiredPaths = blockToValidate.fields.reduce((paths, field) => {
|
||||
if (field.required && !field.localized) {
|
||||
return [
|
||||
...paths,
|
||||
{
|
||||
name: field.name,
|
||||
type: field.type,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
return paths;
|
||||
});
|
||||
|
||||
allRequiredPaths.forEach((path) => {
|
||||
if (!value[path]) {
|
||||
throw new Error(`${fullName} is missing the required path ${path}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
if (!blockToValidate) {
|
||||
throw new Error(`${fullName} tried to set a block type of ${value.blockType}, but no corresponding block was found.`);
|
||||
}
|
||||
|
||||
const allRequiredPaths = blockToValidate.fields.reduce((paths, field) => {
|
||||
if (field.required && !field.localized) {
|
||||
return [
|
||||
...paths,
|
||||
{
|
||||
name: field.name,
|
||||
type: field.type,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
return paths;
|
||||
});
|
||||
|
||||
allRequiredPaths.forEach((path) => {
|
||||
if (!value[path]) {
|
||||
throw new Error(`${fullName} is missing the required path ${path}`);
|
||||
}
|
||||
});
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
const LocaleCustomType = new GraphQLScalarType({
|
||||
const BlockInputType = new GraphQLScalarType({
|
||||
name: `${fullName}BlockInputType`,
|
||||
description: `Validates ${fullName} values.`,
|
||||
serialize: validate,
|
||||
parseValue: validate,
|
||||
parseLiteral: ast => ast.value,
|
||||
serialize: value => value,
|
||||
parseValue: value => value,
|
||||
parseLiteral: (ast, variables) => {
|
||||
const parsedObject = parseLiteral('JSON', ast, variables);
|
||||
const validatedObject = validate(parsedObject);
|
||||
|
||||
return validatedObject;
|
||||
},
|
||||
});
|
||||
|
||||
return LocaleCustomType;
|
||||
return BlockInputType;
|
||||
}
|
||||
|
||||
module.exports = buildBlockInputType;
|
||||
|
||||
@@ -8,6 +8,7 @@ const {
|
||||
GraphQLEnumType,
|
||||
GraphQLInputObjectType,
|
||||
} = require('graphql');
|
||||
const { GraphQLJSON } = require('graphql-type-json');
|
||||
|
||||
const withNullableType = require('./withNullableType');
|
||||
const formatName = require('../utilities/formatName');
|
||||
@@ -26,7 +27,7 @@ function buildMutationInputType(name, fields, parentName) {
|
||||
checkbox: () => ({ type: GraphQLBoolean }),
|
||||
select: (field) => {
|
||||
let type = new GraphQLEnumType({
|
||||
name: `${combineParentName(parentName, field.name)}_Input`,
|
||||
name: `${combineParentName(parentName, field.name)}_MutationInput`,
|
||||
values: field.options.reduce((values, option) => {
|
||||
if (typeof option === 'object' && option.value) {
|
||||
return {
|
||||
@@ -69,8 +70,8 @@ function buildMutationInputType(name, fields, parentName) {
|
||||
return { type };
|
||||
},
|
||||
flexible: (field) => {
|
||||
const blockInputType = this.buildBlockInputType(field.name, field.blocks, parentName);
|
||||
return { type: new GraphQLList(blockInputType) };
|
||||
// const blockInputType = this.buildBlockInputType(field.name, field.blocks, parentName);
|
||||
return { type: GraphQLJSON };
|
||||
},
|
||||
};
|
||||
|
||||
@@ -92,7 +93,7 @@ function buildMutationInputType(name, fields, parentName) {
|
||||
const fieldName = formatName(name);
|
||||
|
||||
return new GraphQLInputObjectType({
|
||||
name: `create${fieldName}Input`,
|
||||
name: `mutation${fieldName}Input`,
|
||||
fields: {
|
||||
...fieldTypes,
|
||||
},
|
||||
|
||||
@@ -41,7 +41,7 @@ function registerCollections() {
|
||||
collection.graphQL.mutationInputType = this.buildMutationInputType(
|
||||
singularLabel,
|
||||
fields,
|
||||
`create${singularLabel}`,
|
||||
singularLabel,
|
||||
);
|
||||
|
||||
this.Query.fields[singularLabel] = {
|
||||
|
||||
Reference in New Issue
Block a user