Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(gatsby): add a way to skip tracking inline objects #38805

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
GatsbyNode,
NodePluginSchema,
CreateSchemaCustomizationArgs,
IGatsbyResolverContext,
} from "gatsby"
import {
GraphQLFieldConfig,
Expand Down Expand Up @@ -31,15 +32,29 @@ import type {
} from "./types/contentful"
import { detectMarkdownField, makeContentTypeIdMap } from "./utils"
import { restrictedNodeFields } from "./config"
import { Document } from "@contentful/rich-text-types"

type CreateTypes = CreateSchemaCustomizationArgs["actions"]["createTypes"]

interface IContentfulGraphQLField
extends Omit<Partial<GraphQLFieldConfig<unknown, unknown>>, "type"> {
extends Omit<
Partial<
GraphQLFieldConfig<
IContentfulEntry,
IGatsbyResolverContext<IContentfulEntry, unknown>
>
>,
"type"
> {
type: string | GraphQLType
id?: string
}

interface IRichTextFieldStructure {
richTextData: Document
spaceId: string
}

// Contentful content type schemas
const ContentfulDataTypes: Map<
string,
Expand Down Expand Up @@ -112,7 +127,18 @@ const ContentfulDataTypes: Map<
[
`RichText`,
(): IContentfulGraphQLField => {
return { type: `ContentfulRichText` }
return {
type: `ContentfulRichText`,
resolve: (source, args, context, info): IRichTextFieldStructure => {
const richTextData = context.defaultFieldResolver(
source,
args,
context,
info
)
return { richTextData, spaceId: source.sys.spaceId }
},
}
},
],
])
Expand Down Expand Up @@ -584,16 +610,13 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
const makeRichTextLinksResolver =
(nodeType, entityType) =>
async (
source,
source: IRichTextFieldStructure,
_args,
context
): Promise<Array<IContentfulEntry> | null> => {
const links = getRichTextEntityLinks(source, nodeType)[entityType].map(
({ id }) => id
)

const node = context.nodeModel.findRootNodeAncestor(source)
pieh marked this conversation as resolved.
Show resolved Hide resolved
if (!node) return null
const links = getRichTextEntityLinks(source.richTextData, nodeType)[
entityType
].map(({ id }) => id)

const res = await context.nodeModel.findAll({
query: {
Expand All @@ -602,7 +625,7 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
id: {
in: links,
},
spaceId: { eq: node.sys.spaceId },
spaceId: { eq: source.spaceId },
},
},
},
Expand Down Expand Up @@ -678,8 +701,8 @@ export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"]
fields: {
json: {
type: `JSON`,
resolve(source) {
return source
resolve(source: IRichTextFieldStructure) {
return source.richTextData
},
},
links: {
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-source-contentful/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ export const createNodesForContentType = ({
type: makeTypeName(contentTypeItemId, contentTypePrefix),
// The content of an entry is guaranteed to be updated if and only if the .sys.updatedAt field changed
contentDigest: entryItem.sys.updatedAt as string,
dontTrackInlineObjects: true,
},
// https://www.contentful.com/developers/docs/references/content-delivery-api/#/introduction/common-resource-attributes
// https://www.contentful.com/developers/docs/references/graphql/#/reference/schema-generation/sys-field
Expand Down Expand Up @@ -944,6 +945,7 @@ export const createAssetNodes = async ({
type: `ContentfulAsset`,
// The content of an asset is guaranteed to be updated if and only if the .sys.updatedAt field changed
contentDigest: assetItem.sys.updatedAt,
dontTrackInlineObjects: true,
},
// https://www.contentful.com/developers/docs/references/content-delivery-api/#/introduction/common-resource-attributes
// https://www.contentful.com/developers/docs/references/graphql/#/reference/schema-generation/sys-field
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export {

export * from "gatsby-script"

export { IGatsbyResolverContext } from "./src/schema/type-definitions"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess we can skip this and I overlooked an already exported type for https://github.com/gatsbyjs/gatsby/pull/38805/files#diff-2a342b3e927ced0bfb4d19fe7f56ef73e0f08094f83526aaccc252d29af11870R44

🤔

Copy link
Contributor

@pieh pieh Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really have good types for schema customization - most of it is ... any 🙈 so this is fine


export {
AdapterInit,
IAdapter,
Expand Down Expand Up @@ -1777,6 +1779,7 @@ export interface NodeInput {
contentDigest: string
description?: string
contentFilePath?: string
dontTrackInlineObjects?: boolean
}
[key: string]: unknown
}
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/joi-schemas/joi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export const nodeSchema: Joi.ObjectSchema<IGatsbyNode> = Joi.object()
ignoreType: Joi.boolean(),
counter: Joi.number(),
contentFilePath: Joi.string(),
dontTrackInlineObjects: Joi.boolean(),
})
.unknown(false), // Don't allow non-standard fields
})
Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/src/schema/node-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,9 @@ const addRootNodeToInlineObject = (
const isPlainObject = _.isPlainObject(data)

if (isPlainObject || _.isArray(data)) {
if (data?.internal.dontTrackInlineObjects) {
return
}
if (path.has(data)) return
path.add(data)

Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/schema/types/node-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const getOrCreateNodeInterface = <TSource, TArgs>(
owner: `String!`,
type: `String!`,
contentFilePath: `String`,
dontTrackInlineObjects: `Boolean`,
})
// TODO: Can be removed with graphql-compose 5.11
tc.getInputTypeComposer()
Expand Down