Skip to content

Commit

Permalink
align sys to pattern of Contentful GraphQL API
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Apr 27, 2021
1 parent dbbb6f9 commit 4a1301a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/gatsby-source-contentful/src/__tests__/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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`,
},
}
}
Expand Down
11 changes: 6 additions & 5 deletions packages/gatsby-source-contentful/src/generate-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}
`)

Expand Down
55 changes: 38 additions & 17 deletions packages/gatsby-source-contentful/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -447,6 +447,7 @@ exports.createNodesForContentType = ({
.filter(Boolean)

// Create a node for each content type

const contentTypeNode = {
id: createNodeId(contentTypeItemId),
name: contentTypeItem.name,
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 4a1301a

Please sign in to comment.