From 70937e150921b64110f7641728bb437f8b68f994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20R=C3=B6tsch?= Date: Thu, 22 Apr 2021 19:02:24 +0200 Subject: [PATCH] align sys to pattern of Contentful GraphQL API --- .../src/__tests__/normalize.js | 4 +- .../src/generate-schema.js | 11 ++-- .../gatsby-source-contentful/src/normalize.js | 55 +++++++++++++------ 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/packages/gatsby-source-contentful/src/__tests__/normalize.js b/packages/gatsby-source-contentful/src/__tests__/normalize.js index b634f3a7da6d3..fc14b3cc0b717 100644 --- a/packages/gatsby-source-contentful/src/__tests__/normalize.js +++ b/packages/gatsby-source-contentful/src/__tests__/normalize.js @@ -127,7 +127,7 @@ describe(`Skip existing nodes in warm build`, () => { // returned is not relevant to test so update if anything breaks. return { id, - internal: { contentDigest: entryList[0][0].sys.updatedAt }, + internal: { contentDigest: entryList[0][0].sys.publishedAt }, } } // All other nodes are new ("unknown") @@ -211,7 +211,7 @@ describe(`Process existing mutated nodes in warm build`, () => { return { id, internal: { - contentDigest: entryList[0][0].sys.updatedAt + `changed`, + contentDigest: entryList[0][0].sys.publishedAt + `changed`, }, } } diff --git a/packages/gatsby-source-contentful/src/generate-schema.js b/packages/gatsby-source-contentful/src/generate-schema.js index 60a46a0af2224..c946e82fe2165 100644 --- a/packages/gatsby-source-contentful/src/generate-schema.js +++ b/packages/gatsby-source-contentful/src/generate-schema.js @@ -163,12 +163,13 @@ export function generateSchema({ type ContentfulInternalSys @dontInfer { type: String! id: String! - space: JSON # ContentfulSpace @todo define + spaceId: String! + environmentId: String! contentType: ContentfulContentType @link(by: "id", from: "contentType___NODE") - revision: Int! - createdAt: Date! - updatedAt: Date! - locale: JSON # String + firstPublishedAt: Date! + publishedAt: Date! + publishedVersion: Int! + locale: String! } `) diff --git a/packages/gatsby-source-contentful/src/normalize.js b/packages/gatsby-source-contentful/src/normalize.js index 4631bdd1c5e0e..7984b3a06fa14 100644 --- a/packages/gatsby-source-contentful/src/normalize.js +++ b/packages/gatsby-source-contentful/src/normalize.js @@ -189,8 +189,8 @@ function prepareTextNode(id, node, key, text) { type: `ContentfulNodeTypeText`, mediaType: `text/markdown`, content: str, - // entryItem.sys.updatedAt is source of truth from contentful - contentDigest: node.sys.updatedAt, + // entryItem.sys.publishedAt is source of truth from contentful + contentDigest: node.sys.publishedAt, }, } @@ -369,18 +369,6 @@ exports.createNodesForContentType = ({ }) } - // https://www.contentful.com/developers/docs/references/content-delivery-api/#/introduction/common-resource-attributes - const sys = { - type: entryItem.sys.type, - id: entryItem.sys.id, - space: entryItem.sys.space, - contentType___NODE: createNodeId(contentTypeItemId), - revision: entryItem.sys.revision, - createdAt: entryItem.sys.createdAt, - updatedAt: entryItem.sys.updatedAt, - locale: entryItem.sys.locale, - } - // Create actual entry node let entryNode = { id: entryNodeId, @@ -391,7 +379,19 @@ exports.createNodesForContentType = ({ // The content of an entry is guaranteed to be updated if and only if the .sys.updatedAt field changed contentDigest: entryItem.sys.updatedAt, }, - sys, + // 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 + sys: { + type: entryItem.sys.type, + id: entryItem.sys.id, + locale: locale.code, + spaceId: entryItem.sys.space.sys.id, + environmentId: entryItem.sys.environment.sys.id, + contentType___NODE: createNodeId(contentTypeItemId), + firstPublishedAt: entryItem.sys.createdAt, + publishedAt: entryItem.sys.updatedAt, + publishedVersion: entryItem.sys.revision, + }, } // Replace text fields with text nodes so we can process their markdown @@ -447,6 +447,7 @@ exports.createNodesForContentType = ({ .filter(Boolean) // Create a node for each content type + const contentTypeNode = { id: createNodeId(contentTypeItemId), name: contentTypeItem.name, @@ -456,7 +457,17 @@ exports.createNodesForContentType = ({ type: `${makeTypeName(`ContentType`)}`, }, // https://www.contentful.com/developers/docs/references/content-delivery-api/#/introduction/common-resource-attributes - sys: contentTypeItem.sys, + // https://www.contentful.com/developers/docs/references/graphql/#/reference/schema-generation/sys-field + sys: { + type: contentTypeItem.sys.type, + id: contentTypeItem.sys.id, + locale: locale.code, + spaceId: contentTypeItem.sys.space.sys.id, + environmentId: contentTypeItem.sys.environment.sys.id, + firstPublishedAt: contentTypeItem.sys.createdAt, + publishedAt: contentTypeItem.sys.updatedAt, + publishedVersion: contentTypeItem.sys.revision, + }, } // The content of an entry is guaranteed to be updated if and only if the .sys.updatedAt field changed @@ -510,7 +521,17 @@ exports.createAssetNodes = ({ contentDigest: assetItem.sys.updatedAt, }, // https://www.contentful.com/developers/docs/references/content-delivery-api/#/introduction/common-resource-attributes - sys: assetItem.sys, + // https://www.contentful.com/developers/docs/references/graphql/#/reference/schema-generation/sys-field + sys: { + type: assetItem.sys.type, + id: assetItem.sys.id, + locale: locale.code, + spaceId: assetItem.sys.space.sys.id, + environmentId: assetItem.sys.environment.sys.id, + firstPublishedAt: assetItem.sys.createdAt, + publishedAt: assetItem.sys.updatedAt, + publishedVersion: assetItem.sys.revision, + }, } createNodePromises.push(createNode(assetNode))