Skip to content

Commit

Permalink
Merge pull request #491 from prismicio/aa/fix-empty-slice-zone
Browse files Browse the repository at this point in the history
fix(source): exclude empty Slice Zones in GraphQL type
  • Loading branch information
angeloashmore authored Jan 10, 2022
2 parents fc753fd + 23cb131 commit e5d7157
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/gatsby-source-prismic/src/lib/buildFieldConfigMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ export const buildFieldConfigMap = (
R.sequence(RTE.ApplicativeSeq),
),
),
RTE.map(
R.filter((fieldConfig): fieldConfig is NonNullable<typeof fieldConfig> =>
Boolean(fieldConfig),
),
),
);
11 changes: 9 additions & 2 deletions packages/gatsby-source-prismic/src/lib/toFieldConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import { buildIntegrationFieldConfig } from "../builders/buildIntegrationFieldCo
* Returns a GraphQL field configuration object for a Custom Type field. The
* resulting configuration object can be used in a GraphQL type.
*
* In some cases, `undefined` will be returned. Fields that return `undefined`
* should be omitted from the GraphQL schema.
*
* @param path - Path to the field.
* @param schema - Schema definition for the field.
*
Expand All @@ -37,7 +40,7 @@ export const toFieldConfig = (
): RTE.ReaderTaskEither<
Dependencies,
Error,
gqlc.ObjectTypeComposerFieldConfigDefinition<unknown, unknown>
gqlc.ObjectTypeComposerFieldConfigDefinition<unknown, unknown> | undefined
> => {
switch (schema.type) {
case prismicT.CustomTypeModelFieldType.Boolean: {
Expand Down Expand Up @@ -85,7 +88,11 @@ export const toFieldConfig = (
}

case prismicT.CustomTypeModelFieldType.Slices: {
return buildSlicesFieldConfig(path, schema);
if (Object.keys(schema.config.choices).length > 0) {
return buildSlicesFieldConfig(path, schema);
} else {
return RTE.right(undefined);
}
}

case prismicT.CustomTypeModelFieldType.StructuredText: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,43 @@ test("creates types for each slice choice", async (t) => {
);
});

test("slice zones with no slices are excluded from the custom type", async (t) => {
const gatsbyContext = createGatsbyContext();
const pluginOptions = createPluginOptions(t);

const customTypeModel = createMockCustomTypeModelWithFields(t, {
// Including an extra field so we have something to check for in the test.
keyText: prismicM.model.keyText({ seed: t.title }),
// This field should not be included in the Custom Type's type.
slices: prismicM.model.sliceZone({
seed: t.title,
choices: {},
}),
});
customTypeModel.id = "foo";

pluginOptions.customTypeModels = [customTypeModel];

await createSchemaCustomization(
gatsbyContext as gatsby.CreateSchemaCustomizationArgs,
pluginOptions,
noop,
);

t.true(
(gatsbyContext.actions.createTypes as sinon.SinonStub).calledWith({
kind: "OBJECT",
config: {
name: "PrismicPrefixFooDataType",
fields: {
keyText: "String",
},
},
}),
"The `slices` Slice Zone should not be included",
);
});

test("id field resolves to a unique id", async (t) => {
const gatsbyContext = createGatsbyContext();
const pluginOptions = createPluginOptions(t);
Expand Down

0 comments on commit e5d7157

Please sign in to comment.