From e05204f2cbe301730cd2c0ee92e1a5a0a94336f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedikt=20R=C3=B6tsch?= Date: Thu, 6 May 2021 16:23:37 +0200 Subject: [PATCH] refactor(contentful-schema): prefix generated types with contentfulContentType --- .../references/content-reference.js | 2 +- .../src/components/references/index.js | 8 ++-- .../src/components/references/location.js | 2 +- .../src/components/references/number.js | 2 +- .../src/components/references/text.js | 2 +- e2e-tests/contentful/src/pages/boolean.js | 4 +- .../contentful/src/pages/content-reference.js | 40 ++++++++--------- e2e-tests/contentful/src/pages/date.js | 10 +++-- e2e-tests/contentful/src/pages/json.js | 8 +++- e2e-tests/contentful/src/pages/location.js | 4 +- .../contentful/src/pages/media-reference.js | 4 +- e2e-tests/contentful/src/pages/number.js | 4 +- e2e-tests/contentful/src/pages/rich-text.js | 28 ++++++------ e2e-tests/contentful/src/pages/text.js | 16 ++++--- .../src/generate-schema.js | 44 +++++++++---------- .../gatsby-source-contentful/src/normalize.js | 6 +-- 16 files changed, 97 insertions(+), 87 deletions(-) diff --git a/e2e-tests/contentful/src/components/references/content-reference.js b/e2e-tests/contentful/src/components/references/content-reference.js index d3b0e02d24cf3..3fafcd9293e39 100644 --- a/e2e-tests/contentful/src/components/references/content-reference.js +++ b/e2e-tests/contentful/src/components/references/content-reference.js @@ -1,6 +1,6 @@ import React from "react" -export const ContentfulContentReference = ({ one, many, title }) => { +export const ContentfulContentTypeContentReference = ({ one, many, title }) => { const references = [one, ...(many || [])].filter(Boolean) return (

diff --git a/e2e-tests/contentful/src/components/references/index.js b/e2e-tests/contentful/src/components/references/index.js index a9d0675bb1577..f9e79b30222d6 100644 --- a/e2e-tests/contentful/src/components/references/index.js +++ b/e2e-tests/contentful/src/components/references/index.js @@ -1,4 +1,4 @@ -export { ContentfulContentReference } from "./content-reference" -export { ContentfulLocation } from "./location" -export { ContentfulNumber } from "./number" -export { ContentfulText } from "./text" +export { ContentfulContentTypeContentReference } from "./content-reference" +export { ContentfulContentTypeLocation } from "./location" +export { ContentfulContentTypeNumber } from "./number" +export { ContentfulContentTypeText } from "./text" diff --git a/e2e-tests/contentful/src/components/references/location.js b/e2e-tests/contentful/src/components/references/location.js index d25a79e529402..81c2118b6b3c4 100644 --- a/e2e-tests/contentful/src/components/references/location.js +++ b/e2e-tests/contentful/src/components/references/location.js @@ -1,6 +1,6 @@ import React from "react" -export const ContentfulLocation = ({ location }) => ( +export const ContentfulContentTypeLocation = ({ location }) => (

[ContentfulLocation] Lat: {location.lat}, Long: {location.lon}

diff --git a/e2e-tests/contentful/src/components/references/number.js b/e2e-tests/contentful/src/components/references/number.js index e05997352b12a..75782e80db76b 100644 --- a/e2e-tests/contentful/src/components/references/number.js +++ b/e2e-tests/contentful/src/components/references/number.js @@ -1,5 +1,5 @@ import React from "react" -export const ContentfulNumber = ({ integer }) => ( +export const ContentfulContentTypeNumber = ({ integer }) => (

[ContentfulInteger] {integer}

) diff --git a/e2e-tests/contentful/src/components/references/text.js b/e2e-tests/contentful/src/components/references/text.js index 78fa8732feb38..a2fd087152f3d 100644 --- a/e2e-tests/contentful/src/components/references/text.js +++ b/e2e-tests/contentful/src/components/references/text.js @@ -1,5 +1,5 @@ import React from "react" -export const ContentfulText = ({ short }) => ( +export const ContentfulContentTypeText = ({ short }) => (

[ContentfulText] {short}

) diff --git a/e2e-tests/contentful/src/pages/boolean.js b/e2e-tests/contentful/src/pages/boolean.js index 90b0cd34279ee..56b60fd701a29 100644 --- a/e2e-tests/contentful/src/pages/boolean.js +++ b/e2e-tests/contentful/src/pages/boolean.js @@ -5,7 +5,7 @@ import slugify from "slugify" import Layout from "../components/layout" const BooleanPage = ({ data }) => { - const entries = data.allContentfulBoolean.nodes + const entries = data.allContentfulContentTypeBoolean.nodes return ( {entries.map(({ title, boolean }) => { @@ -25,7 +25,7 @@ export default BooleanPage export const pageQuery = graphql` query BooleanQuery { - allContentfulBoolean(sort: { fields: sys___id }) { + allContentfulContentTypeBoolean(sort: { fields: sys___id }) { nodes { title boolean diff --git a/e2e-tests/contentful/src/pages/content-reference.js b/e2e-tests/contentful/src/pages/content-reference.js index 3fc172bdf5fc9..8e01a59a6a79d 100644 --- a/e2e-tests/contentful/src/pages/content-reference.js +++ b/e2e-tests/contentful/src/pages/content-reference.js @@ -17,7 +17,7 @@ function renderReferencedComponent(ref) { } const ContentReferencePage = ({ data }) => { - const entries = data.allContentfulContentReference.nodes + const entries = data.allContentfulContentTypeContentReference.nodes return ( {entries.map(({ sys: { id }, title, one, many }) => { @@ -47,7 +47,7 @@ export default ContentReferencePage export const pageQuery = graphql` query ContentReferenceQuery { - allContentfulContentReference(sort: { fields: title }) { + allContentfulContentTypeContentReference(sort: { fields: title }) { nodes { title sys { @@ -58,39 +58,39 @@ export const pageQuery = graphql` sys { id } - ... on ContentfulText { + ... on ContentfulContentTypeText { title short } - ... on ContentfulNumber { + ... on ContentfulContentTypeNumber { title integer } - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title one { - ... on ContentfulText { + ... on ContentfulContentTypeText { title short } - ... on ContentfulNumber { + ... on ContentfulContentTypeNumber { title integer } - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title } } many { - ... on ContentfulText { + ... on ContentfulContentTypeText { title short } - ... on ContentfulNumber { + ... on ContentfulContentTypeNumber { title integer } - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title } } @@ -101,39 +101,39 @@ export const pageQuery = graphql` sys { id } - ... on ContentfulText { + ... on ContentfulContentTypeText { title short } - ... on ContentfulNumber { + ... on ContentfulContentTypeNumber { title integer } - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title one { - ... on ContentfulText { + ... on ContentfulContentTypeText { title short } - ... on ContentfulNumber { + ... on ContentfulContentTypeNumber { title integer } - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title } } many { - ... on ContentfulText { + ... on ContentfulContentTypeText { title short } - ... on ContentfulNumber { + ... on ContentfulContentTypeNumber { title integer } - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title } } diff --git a/e2e-tests/contentful/src/pages/date.js b/e2e-tests/contentful/src/pages/date.js index a88d72c825359..a270b78c73ecd 100644 --- a/e2e-tests/contentful/src/pages/date.js +++ b/e2e-tests/contentful/src/pages/date.js @@ -26,19 +26,23 @@ export default DatePage export const pageQuery = graphql` query DateQuery { - dateTime: contentfulDate(sys: { id: { eq: "38akBjGb3T1t4AjB87wQjo" } }) { + dateTime: contentfulContentTypeDate( + sys: { id: { eq: "38akBjGb3T1t4AjB87wQjo" } } + ) { title date: dateTime formatted: dateTime(formatString: "D.M.YYYY - hh:mm") } - dateTimeTimezone: contentfulDate( + dateTimeTimezone: contentfulContentTypeDate( sys: { id: { eq: "6dZ8pK4tFWZDZPHgSC0tNS" } } ) { title date: dateTimeTimezone formatted: dateTimeTimezone(formatString: "D.M.YYYY - hh:mm (z)") } - date: contentfulDate(sys: { id: { eq: "5FuULz0jl0rKoKUKp2rshf" } }) { + date: contentfulContentTypeDate( + sys: { id: { eq: "5FuULz0jl0rKoKUKp2rshf" } } + ) { title date formatted: date(formatString: "D.M.YYYY") diff --git a/e2e-tests/contentful/src/pages/json.js b/e2e-tests/contentful/src/pages/json.js index 2f5389fdc8a83..d2ae1f3ca7abf 100644 --- a/e2e-tests/contentful/src/pages/json.js +++ b/e2e-tests/contentful/src/pages/json.js @@ -52,10 +52,14 @@ export default JSONPage export const pageQuery = graphql` query JSONQuery { - simple: contentfulJson(sys: { id: { eq: "2r6tNjP8brkyy5yLR39hhh" } }) { + simple: contentfulContentTypeJson( + sys: { id: { eq: "2r6tNjP8brkyy5yLR39hhh" } } + ) { json } - complex: contentfulJson(sys: { id: { eq: "2y71nV0cpW9vzTmJybq571" } }) { + complex: contentfulContentTypeJson( + sys: { id: { eq: "2y71nV0cpW9vzTmJybq571" } } + ) { json } } diff --git a/e2e-tests/contentful/src/pages/location.js b/e2e-tests/contentful/src/pages/location.js index 7f2e395f67f55..9da0167328bac 100644 --- a/e2e-tests/contentful/src/pages/location.js +++ b/e2e-tests/contentful/src/pages/location.js @@ -5,7 +5,7 @@ import slugify from "slugify" import Layout from "../components/layout" const LocationPage = ({ data }) => { - const entries = data.allContentfulLocation.nodes + const entries = data.allContentfulContentTypeLocation.nodes return ( {entries.map(({ title, location }) => { @@ -28,7 +28,7 @@ export default LocationPage export const pageQuery = graphql` query LocationQuery { - allContentfulLocation(sort: { fields: sys___id }) { + allContentfulContentTypeLocation(sort: { fields: sys___id }) { nodes { title location { diff --git a/e2e-tests/contentful/src/pages/media-reference.js b/e2e-tests/contentful/src/pages/media-reference.js index ed7b84a7cb2b0..aa62a4947c67a 100644 --- a/e2e-tests/contentful/src/pages/media-reference.js +++ b/e2e-tests/contentful/src/pages/media-reference.js @@ -6,7 +6,7 @@ import slugify from "slugify" import Layout from "../components/layout" const MediaReferencePage = ({ data }) => { - const entries = data.allContentfulMediaReference.nodes + const entries = data.allContentfulContentTypeMediaReference.nodes return ( {entries.map(({ sys: { id }, title, one, many }) => { @@ -38,7 +38,7 @@ export default MediaReferencePage export const pageQuery = graphql` query MediaReferenceQuery { - allContentfulMediaReference(sort: { fields: title }) { + allContentfulContentTypeMediaReference(sort: { fields: title }) { nodes { title sys { diff --git a/e2e-tests/contentful/src/pages/number.js b/e2e-tests/contentful/src/pages/number.js index 9b042d7c88a1d..da46205f8d007 100644 --- a/e2e-tests/contentful/src/pages/number.js +++ b/e2e-tests/contentful/src/pages/number.js @@ -5,7 +5,7 @@ import slugify from "slugify" import Layout from "../components/layout" const NumberPage = ({ data }) => { - const entries = data.allContentfulNumber.nodes + const entries = data.allContentfulContentTypeNumber.nodes return ( {entries.map(({ title, integer, decimal }) => { @@ -25,7 +25,7 @@ export default NumberPage export const pageQuery = graphql` query NumberQuery { - allContentfulNumber(sort: { fields: sys___id }) { + allContentfulContentTypeNumber(sort: { fields: sys___id }) { nodes { title integer diff --git a/e2e-tests/contentful/src/pages/rich-text.js b/e2e-tests/contentful/src/pages/rich-text.js index 64d07ee69a9be..b60f3454f9560 100644 --- a/e2e-tests/contentful/src/pages/rich-text.js +++ b/e2e-tests/contentful/src/pages/rich-text.js @@ -67,7 +67,7 @@ const makeOptions = ({ assetBlockMap, entryBlockMap, entryInlineMap }) => ({ }) const RichTextPage = ({ data }) => { - const entries = data.allContentfulRichText.nodes + const entries = data.allContentfulContentTypeRichText.nodes return ( {entries.map(({ id, title, richText }) => { @@ -88,7 +88,7 @@ export default RichTextPage export const pageQuery = graphql` query RichTextQuery { - allContentfulRichText(sort: { fields: title }) { + allContentfulContentTypeRichText(sort: { fields: title }) { nodes { id title @@ -112,36 +112,36 @@ export const pageQuery = graphql` id type } - ... on ContentfulText { + ... on ContentfulContentTypeText { title short } - ... on ContentfulLocation { + ... on ContentfulContentTypeLocation { location { lat lon } } - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title one { __typename sys { id } - ... on ContentfulText { + ... on ContentfulContentTypeText { title short } - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title one { - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title } } many { - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title } } @@ -152,23 +152,23 @@ export const pageQuery = graphql` sys { id } - ... on ContentfulText { + ... on ContentfulContentTypeText { title short } - ... on ContentfulNumber { + ... on ContentfulContentTypeNumber { title integer } - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title one { - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title } } many { - ... on ContentfulContentReference { + ... on ContentfulContentTypeContentReference { title } } diff --git a/e2e-tests/contentful/src/pages/text.js b/e2e-tests/contentful/src/pages/text.js index e4cdf9368d1e1..b16e8ca0ca6b4 100644 --- a/e2e-tests/contentful/src/pages/text.js +++ b/e2e-tests/contentful/src/pages/text.js @@ -53,18 +53,24 @@ export default TextPage export const pageQuery = graphql` query TextQuery { - short: contentfulText(sys: { id: { eq: "5ZtcN1o7KpN7J7xgiTyaXo" } }) { + short: contentfulContentTypeText( + sys: { id: { eq: "5ZtcN1o7KpN7J7xgiTyaXo" } } + ) { short } - shortList: contentfulText(sys: { id: { eq: "7b5U927WTFcQXO2Gewwa2k" } }) { + shortList: contentfulContentTypeText( + sys: { id: { eq: "7b5U927WTFcQXO2Gewwa2k" } } + ) { shortList } - longPlain: contentfulText(sys: { id: { eq: "6ru8cSC9hZi3Ekvtw7P77S" } }) { + longPlain: contentfulContentTypeText( + sys: { id: { eq: "6ru8cSC9hZi3Ekvtw7P77S" } } + ) { longPlain { raw } } - longMarkdownSimple: contentfulText( + longMarkdownSimple: contentfulContentTypeText( sys: { id: { eq: "NyPJw0mcSuCwY2gV0zYny" } } ) { longMarkdown { @@ -73,7 +79,7 @@ export const pageQuery = graphql` } } } - longMarkdownComplex: contentfulText( + longMarkdownComplex: contentfulContentTypeText( sys: { id: { eq: "3pwKS9UWsYmOguo4UdE1EB" } } ) { longMarkdown { diff --git a/packages/gatsby-source-contentful/src/generate-schema.js b/packages/gatsby-source-contentful/src/generate-schema.js index 9b6c0890c740f..896e310d6a88b 100644 --- a/packages/gatsby-source-contentful/src/generate-schema.js +++ b/packages/gatsby-source-contentful/src/generate-schema.js @@ -14,7 +14,7 @@ const ContentfulDataTypes = new Map([ `Text`, field => { return { - type: `ContentfulNodeTypeText`, + type: `ContentfulText`, extensions: { link: { by: `id`, from: `${field.id}___NODE` }, }, @@ -59,13 +59,13 @@ const ContentfulDataTypes = new Map([ [ `Location`, () => { - return { type: `ContentfulNodeTypeLocation` } + return { type: `ContentfulLocation` } }, ], [ `RichText`, () => { - return { type: `ContentfulNodeTypeRichText` } + return { type: `ContentfulRichText` } }, ], ]) @@ -106,8 +106,8 @@ const translateFieldType = field => { function generateAssetTypes({ createTypes }) { createTypes(` - type ContentfulAsset implements ContentfulInternalReference & Node { - sys: ContentfulInternalSys + type ContentfulAsset implements ContentfulReference & Node { + sys: ContentfulSys id: ID! title: String description: String @@ -129,9 +129,9 @@ export function generateSchema({ }) { // Generic Types createTypes(` - interface ContentfulInternalReference implements Node { + interface ContentfulReference implements Node { id: ID! - sys: ContentfulInternalSys + sys: ContentfulSys } `) @@ -145,7 +145,7 @@ export function generateSchema({ `) createTypes(` - type ContentfulInternalSys @dontInfer { + type ContentfulSys @dontInfer { type: String! id: String! spaceId: String! @@ -161,7 +161,7 @@ export function generateSchema({ createTypes(` interface ContentfulEntry implements Node @dontInfer { id: ID! - sys: ContentfulInternalSys + sys: ContentfulSys } `) @@ -191,7 +191,7 @@ export function generateSchema({ // Contentful specific types createTypes( schema.buildObjectType({ - name: `ContentfulNodeTypeRichTextAssets`, + name: `ContentfulRichTextAssets`, fields: { block: { type: `[ContentfulAsset]!`, @@ -207,7 +207,7 @@ export function generateSchema({ createTypes( schema.buildObjectType({ - name: `ContentfulNodeTypeRichTextEntries`, + name: `ContentfulRichTextEntries`, fields: { inline: { type: `[ContentfulEntry]!`, @@ -227,16 +227,16 @@ export function generateSchema({ createTypes( schema.buildObjectType({ - name: `ContentfulNodeTypeRichTextLinks`, + name: `ContentfulRichTextLinks`, fields: { assets: { - type: `ContentfulNodeTypeRichTextAssets`, + type: `ContentfulRichTextAssets`, resolve(source) { return source }, }, entries: { - type: `ContentfulNodeTypeRichTextEntries`, + type: `ContentfulRichTextEntries`, resolve(source) { return source }, @@ -247,7 +247,7 @@ export function generateSchema({ createTypes( schema.buildObjectType({ - name: `ContentfulNodeTypeRichText`, + name: `ContentfulRichText`, fields: { json: { type: `JSON`, @@ -256,7 +256,7 @@ export function generateSchema({ }, }, links: { - type: `ContentfulNodeTypeRichTextLinks`, + type: `ContentfulRichTextLinks`, resolve(source) { return source }, @@ -269,7 +269,7 @@ export function generateSchema({ // Location createTypes( schema.buildObjectType({ - name: `ContentfulNodeTypeLocation`, + name: `ContentfulLocation`, fields: { lat: { type: `Float!` }, lon: { type: `Float!` }, @@ -284,7 +284,7 @@ export function generateSchema({ // @todo Is there a way to have this as string and let transformer-remark replace it with an object? createTypes( schema.buildObjectType({ - name: `ContentfulNodeTypeText`, + name: `ContentfulText`, fields: { raw: `String!`, }, @@ -315,14 +315,10 @@ export function generateSchema({ name: makeTypeName(type), fields: { id: { type: `ID!` }, - sys: { type: `ContentfulInternalSys` }, + sys: { type: `ContentfulSys` }, ...fields, }, - interfaces: [ - `ContentfulInternalReference`, - `ContentfulEntry`, - `Node`, - ], + interfaces: [`ContentfulReference`, `ContentfulEntry`, `Node`], extensions: { dontInfer: {} }, }) ) diff --git a/packages/gatsby-source-contentful/src/normalize.js b/packages/gatsby-source-contentful/src/normalize.js index 1cdefcab9ea84..0e7310fecbe98 100644 --- a/packages/gatsby-source-contentful/src/normalize.js +++ b/packages/gatsby-source-contentful/src/normalize.js @@ -1,6 +1,6 @@ const _ = require(`lodash`) -const typePrefix = `Contentful` +const typePrefix = `ContentfulContentType` export const makeTypeName = type => _.upperFirst(_.camelCase(`${typePrefix} ${type}`)) @@ -185,7 +185,7 @@ function prepareTextNode(id, node, key, text) { parent: node.id, raw: str, internal: { - type: `ContentfulNodeTypeText`, + type: `ContentfulText`, mediaType: `text/markdown`, content: str, // entryItem.sys.publishedAt is source of truth from contentful @@ -512,7 +512,7 @@ exports.createAssetNodes = ({ parent: null, children: [], internal: { - type: `${makeTypeName(`Asset`)}`, + 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, },