diff --git a/src/graphql/init.js b/src/graphql/init.js index 7bb0e7df6d..3e83534fd1 100644 --- a/src/graphql/init.js +++ b/src/graphql/init.js @@ -30,6 +30,7 @@ function init() { types: { LocaleStringType: getLocaleStringType(this.config.localization), LocaleFloatType: getLocaleFloatType(this.config.localization), + blockTypes: {}, }, }; diff --git a/src/graphql/schema/getBuildObjectType.js b/src/graphql/schema/getBuildObjectType.js index 49fc431bd2..6460fb0d4d 100644 --- a/src/graphql/schema/getBuildObjectType.js +++ b/src/graphql/schema/getBuildObjectType.js @@ -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]; - }, - })), + }), + ), ), }; }, diff --git a/src/graphql/utilities/formatName.js b/src/graphql/utilities/formatName.js index ef54a8add9..71d3672116 100644 --- a/src/graphql/utilities/formatName.js +++ b/src/graphql/utilities/formatName.js @@ -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; diff --git a/src/graphql/utilities/uppercase.js b/src/graphql/utilities/uppercase.js new file mode 100644 index 0000000000..767653c579 --- /dev/null +++ b/src/graphql/utilities/uppercase.js @@ -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;