gatsby-source-prismic
Version:
Gatsby source plugin for building websites using Prismic as a data source
76 lines (75 loc) • 3.02 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const fieldModelsRecordToGraphQLType = require("./fieldModelsRecordToGraphQLType.cjs");
const pascalCase = require("./pascalCase.cjs");
const sharedSliceModelToGraphQLType = (args) => {
let variationTypeNames = [];
for (const variation of args.model.variations) {
const type2 = args.gatsbyNodeArgs.schema.buildObjectType({
name: pascalCase.pascalCase("Prismic", args.pluginOptions.typePrefix, args.model.id, "Slice", variation.id),
description: args.model.description,
fields: {
id: {
type: "ID!",
resolve: (source) => {
return source.id || args.gatsbyNodeArgs.createNodeId(args.gatsbyNodeArgs.createContentDigest(source));
}
},
slice_type: {
type: "String!"
},
slice_label: {
type: "String"
},
version: {
type: "String!"
},
variation: {
type: "String!"
}
},
interfaces: ["PrismicSlice", "PrismicSharedSlice"]
});
if (variation.primary && Object.keys(variation.primary).length > 0) {
const primaryType = fieldModelsRecordToGraphQLType.fieldModelsRecordToGraphQLType({
...args,
path: [args.model.id, variation.id, "primary"],
models: variation.primary
});
primaryType.config.name = pascalCase.pascalCase("Prismic", args.pluginOptions.typePrefix, args.model.id, "Slice", variation.id, "Primary");
args.gatsbyNodeArgs.actions.createTypes(primaryType);
if (type2.config.fields) {
type2.config.fields.primary = {
type: `${primaryType.config.name}!`
};
}
}
if (variation.items && Object.keys(variation.items).length > 0) {
const itemType = fieldModelsRecordToGraphQLType.fieldModelsRecordToGraphQLType({
...args,
path: [args.model.id, variation.id, "items"],
models: variation.items
});
itemType.config.name = pascalCase.pascalCase("Prismic", args.pluginOptions.typePrefix, args.model.id, "Slice", variation.id, "Item");
args.gatsbyNodeArgs.actions.createTypes(itemType);
if (type2.config.fields) {
type2.config.fields.items = {
type: `[${itemType.config.name}!]!`
};
}
}
args.gatsbyNodeArgs.actions.createTypes(type2);
variationTypeNames = [...variationTypeNames, type2.config.name];
}
const type = args.gatsbyNodeArgs.schema.buildUnionType({
name: pascalCase.pascalCase("Prismic", args.pluginOptions.typePrefix, args.model.id, "Slice"),
types: variationTypeNames,
description: args.model.description,
resolveType: (source) => {
return pascalCase.pascalCase("Prismic", args.pluginOptions.typePrefix, source.slice_type, "Slice", source.variation);
}
});
return type;
};
exports.sharedSliceModelToGraphQLType = sharedSliceModelToGraphQLType;
//# sourceMappingURL=sharedSliceModelToGraphQLType.cjs.map
;