Skip to content

Commit

Permalink
feat: type Rich Text and Title fields with custom PrismicStructuredTe…
Browse files Browse the repository at this point in the history
…xt scalar (#473)
  • Loading branch information
angeloashmore authored Nov 5, 2021
1 parent e8f52c2 commit 5737479
Show file tree
Hide file tree
Showing 8 changed files with 1,193 additions and 999 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-source-prismic/src/buildDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const buildDependencies = async (
buildObjectType: gatsbyContext.schema.buildObjectType,
buildEnumType: gatsbyContext.schema.buildEnumType,
buildInterfaceType: gatsbyContext.schema.buildInterfaceType,
buildScalarType: gatsbyContext.schema.buildScalarType,
getNode: gatsbyContext.getNode,
getNodes: gatsbyContext.getNodes,
schema: gatsbyContext.schema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { pipe, identity } from "fp-ts/function";
import { buildObjectType } from "../lib/buildObjectType";

import { Dependencies } from "../types";
import { buildScalarType } from "../lib/buildScalarType";
import { requiredTypeName } from "../lib/requiredTypeName";
import { createType } from "../lib/createType";

/**
* Builds a GraphQL Type used by StructuredText fields. The resulting type can
Expand All @@ -19,9 +22,17 @@ export const buildStructuredTextType: RTE.ReaderTaskEither<
gatsby.GatsbyGraphQLType
> = pipe(
RTE.ask<Dependencies>(),
RTE.chain((deps) =>
RTE.bind("structuredTextScalar", (deps) =>
buildScalarType({
name: deps.globalNodeHelpers.createTypeName("StructuredText"),
description:
"Text content with rich formatting capabilities using a Prismic format called Structured Text.",
}),
),
RTE.chainFirst((scope) => createType(scope.structuredTextScalar)),
RTE.chain((scope) =>
buildObjectType({
name: deps.nodeHelpers.createTypeName("StructuredTextType"),
name: scope.nodeHelpers.createTypeName("StructuredTextType"),
fields: {
text: {
type: "String",
Expand All @@ -32,13 +43,16 @@ export const buildStructuredTextType: RTE.ReaderTaskEither<
resolve: (source: prismicT.RichTextField) =>
prismicH.asHTML(
source,
deps.pluginOptions.linkResolver,
deps.pluginOptions.htmlSerializer,
scope.pluginOptions.linkResolver,
scope.pluginOptions.htmlSerializer,
),
},
richText: { type: "JSON", resolve: identity },
richText: {
type: requiredTypeName(scope.structuredTextScalar.config.name),
resolve: identity,
},
raw: {
type: "JSON",
type: requiredTypeName(scope.structuredTextScalar.config.name),
resolve: identity,
deprecationReason:
"This field has been renamed to `richText`. The `richText` field has the same value the `raw` field.",
Expand Down
17 changes: 17 additions & 0 deletions packages/gatsby-source-prismic/src/lib/buildScalarType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as gatsby from "gatsby";
import * as gqlc from "graphql-compose";
import * as RTE from "fp-ts/ReaderTaskEither";

import { Dependencies } from "../types";

/**
* Builds a GraphQL scalar type using the environment's `buildScalarType` function.
*
* @param config - Configuration for the scalar type.
*
* @returns Return value of the environment's `buildScalarType` function.
*/
export const buildScalarType = (
config: gqlc.ScalarTypeComposerAsObjectDefinition,
): RTE.ReaderTaskEither<Dependencies, never, gatsby.GatsbyGraphQLScalarType> =>
RTE.asks((deps) => deps.buildScalarType(config));
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const image = <Value extends prismicT.ImageField>(
for (const thumbnailName of thumbnailNames) {
result.thumbnails[thumbnailName as keyof typeof result.thumbnails] =
buildImageField({
value: config.value[thumbnailName],
value: config.value[thumbnailName as keyof typeof config.value],
imageImgixParams: config.imageImgixParams,
imagePlaceholderImgixParams: config.imagePlaceholderImgixParams,
});
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-prismic/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export interface Dependencies {
buildObjectType: gatsby.NodePluginSchema["buildObjectType"];
buildUnionType: gatsby.NodePluginSchema["buildUnionType"];
buildEnumType: gatsby.NodePluginSchema["buildEnumType"];
buildScalarType: gatsby.NodePluginSchema["buildScalarType"];
buildInterfaceType: gatsby.NodePluginSchema["buildInterfaceType"];
getNode: gatsby.SourceNodesArgs["getNode"];
getNodes: gatsby.SourceNodesArgs["getNodes"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export const createGatsbyContext = (): PartialDeep<gatsby.NodePluginArgs> & {
kind: "INPUT_OBJECT",
config,
})),
buildScalarType: sinon.stub().callsFake((config) => ({
kind: "SCALAR",
config,
})),
},
getNode: sinon.stub().callsFake((id: string) => nodeStore.get(id)),
getNodes: sinon.stub().callsFake(() => [...nodeStore.values()]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ test("creates base type", async (t) => {
resolve: sinon.match.func,
}),
richText: sinon.match({
type: "JSON",
type: "PrismicStructuredText!",
resolve: sinon.match.func,
}),
raw: sinon.match({
type: "JSON",
type: "PrismicStructuredText!",
resolve: sinon.match.func,
}),
},
Expand Down
2,137 changes: 1,147 additions & 990 deletions yarn.lock

Large diffs are not rendered by default.

0 comments on commit 5737479

Please sign in to comment.