progress to Relationship

This commit is contained in:
James
2020-04-06 22:46:02 -04:00
parent 39a0f9c659
commit 057bb631c8
4 changed files with 116 additions and 48 deletions

View File

@@ -21,7 +21,7 @@ class GraphQL {
this.init = this.init.bind(this);
this.registerUser = this.registerUser.bind(this);
this.registerCollections = this.registerCollections.bind(this);
this.addBlockType = this.addBlockType.bind(this);
this.buildBlockTypeIfMissing = this.buildBlockTypeIfMissing.bind(this);
this.config = config;
this.collections = collections;
@@ -81,11 +81,32 @@ class GraphQL {
}
registerCollections() {
Object.keys(this.collections).forEach((collectionKey) => {
const collection = this.collections[collectionKey];
Object.keys(this.collections).forEach((slug) => {
const {
config: {
labels: {
singular,
},
fields,
},
} = this.collections[slug];
const singularLabel = formatName(singular);
this.collections[slug].graphQLType = this.buildObjectType(
singularLabel,
fields,
singularLabel,
getFindByID(this.collections[slug]),
);
});
Object.keys(this.collections).forEach((collectionSlug) => {
const collection = this.collections[collectionSlug];
const {
config: {
slug,
fields,
labels: {
singular,
@@ -97,8 +118,6 @@ class GraphQL {
const singularLabel = formatName(singular);
const pluralLabel = formatName(plural);
collection.graphQLType = this.buildObjectType(singularLabel, fields, singularLabel);
collection.graphQLWhereInputType = buildWhereInputType({
name: singularLabel,
fields,
@@ -106,7 +125,7 @@ class GraphQL {
});
this.Query.fields[singularLabel] = {
type: collection.graphQLType,
type: this.collections[slug].graphQLType,
args: {
id: { type: GraphQLString },
},
@@ -140,8 +159,32 @@ class GraphQL {
});
}
addBlockType(blockType, slug) {
this.types.blockTypes[slug] = blockType;
buildBlockTypeIfMissing(block) {
const {
slug,
labels: {
singular,
},
} = block;
if (!this.types.blockTypes[slug]) {
const formattedBlockName = formatName(singular);
this.types.blockTypes[slug] = this.buildObjectType(
formattedBlockName,
[
...block.fields,
{
name: 'blockName',
type: 'text',
},
{
name: 'blockType',
type: 'text',
},
],
formattedBlockName,
);
}
}
}

View File

@@ -33,7 +33,7 @@ function getBuildObjectType(context) {
return type;
};
const buildObjectType = (name, fields, parent) => {
const buildObjectType = (name, fields, parent, resolver) => {
const fieldToSchemaMap = {
number: (field) => {
return {
@@ -139,10 +139,31 @@ function getBuildObjectType(context) {
};
},
relationship: (field) => {
const type = GraphQLString;
const { relationTo, label } = field;
let relationshipType;
if (Array.isArray(relationTo)) {
const types = relationTo.map((relation) => {
return context.collections[relation].graphQLType;
});
relationshipType = new GraphQLUnionType({
name: combineParentName(parent, label),
types,
resolveType(data) {
return context.types.blockTypes[data.blockType];
},
});
} else {
relationshipType = context.collections[relationTo].graphQLType;
}
// eslint-disable-next-line no-use-before-define
relationshipType = relationshipType || blockType;
const typeWithLocale = withLocalizedType(
field,
withNullableType(field, type),
withNullableType(field, relationshipType),
);
return {
@@ -170,29 +191,9 @@ function getBuildObjectType(context) {
};
},
flexible: (field) => {
const blockTypeOptions = field.blocks.map(block => block.slug);
field.blocks.forEach((block) => {
if (context.types.blockTypes[block.slug] === undefined) {
const formattedBlockName = formatName(block.labels.singular);
context.addBlockType(buildObjectType(
formattedBlockName,
[
...block.fields,
{
name: 'blockName',
type: 'text',
},
{
name: 'blockType',
type: 'select',
options: blockTypeOptions,
},
],
formattedBlockName,
), block.slug);
}
const types = field.blocks.map((block) => {
context.buildBlockTypeIfMissing(block);
return context.types.blockTypes[block.slug];
});
return {
@@ -201,7 +202,7 @@ function getBuildObjectType(context) {
new GraphQLList(
new GraphQLUnionType({
name: combineParentName(parent, field.label),
types: field.blocks.map(blockType => context.types.blockTypes[blockType.slug]),
types,
resolveType(data) {
return context.types.blockTypes[data.blockType];
},
@@ -212,9 +213,9 @@ function getBuildObjectType(context) {
},
};
return new GraphQLObjectType({
const objectSchema = {
name,
fields: fields.reduce((schema, field) => {
fields: () => fields.reduce((schema, field) => {
const fieldSchema = fieldToSchemaMap[field.type];
if (fieldSchema) {
return {
@@ -225,7 +226,15 @@ function getBuildObjectType(context) {
return schema;
}, {}),
});
};
if (resolver) {
objectSchema.resolve = resolver;
}
const blockType = new GraphQLObjectType(objectSchema);
return blockType;
};
return buildObjectType;