ensures flexible content block types are global

This commit is contained in:
James
2020-04-05 22:59:04 -04:00
parent 855ae9dea7
commit c6d649310f
4 changed files with 50 additions and 31 deletions

View File

@@ -30,6 +30,7 @@ function init() {
types: {
LocaleStringType: getLocaleStringType(this.config.localization),
LocaleFloatType: getLocaleFloatType(this.config.localization),
blockTypes: {},
},
};

View File

@@ -5,7 +5,7 @@ const {
GraphQLNonNull,
GraphQLList,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLEnumType,
} = require('graphql');
@@ -170,41 +170,43 @@ function getBuildObjectType(config, graphQL) {
};
},
flexible: (field) => {
const blockTypes = field.blocks.reduce((blocks, block) => {
const formattedBlockName = formatName(block.labels.singular);
const fullName = `${combineParentName(parent, field.label)}_${formattedBlockName}`;
const blockTypeOptions = field.blocks.map(block => block.slug);
return {
...blocks,
[block.slug]: buildObjectType(fullName, block.fields, fullName),
};
}, {});
field.blocks.forEach((block) => {
if (graphQL.types.blockTypes[block.slug] === undefined) {
const formattedBlockName = formatName(block.labels.singular);
graphQL.types.blockTypes[block.slug] = buildObjectType(
formattedBlockName,
[
...block.fields,
{
name: 'blockName',
type: 'text',
},
{
name: 'blockType',
type: 'select',
options: blockTypeOptions,
},
],
formattedBlockName,
);
}
});
return {
type: withLocalizedType(
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,
},
};
}, {}),
}),
new GraphQLList(
new GraphQLUnionType({
name: combineParentName(parent, field.label),
types: field.blocks.map(blockType => graphQL.types.blockTypes[blockType.slug]),
resolveType(data) {
return graphQL.types.blockTypes[data.blockType];
},
blockName: { type: GraphQLString },
},
resolveType(value) {
return blockTypes[value.blockType];
},
})),
}),
),
),
};
},

View File

@@ -1,5 +1,10 @@
const uppercase = require('./uppercase');
const formatName = (string) => {
return string.replace(/-|\/|\s/g, '_');
let formatted = string.replace(/-|\//g, '_');
formatted = uppercase(formatted);
formatted = formatted.replace(/ /g, '');
return formatted;
};
module.exports = formatName;

View File

@@ -0,0 +1,11 @@
function uppercase(str) {
const array1 = str.split(' ');
const newarray1 = [];
for (let x = 0; x < array1.length; x++) {
newarray1.push(array1[x].charAt(0).toUpperCase() + array1[x].slice(1));
}
return newarray1.join(' ');
}
module.exports = uppercase;