Skip to content

Commit

Permalink
refactor: update e2e test schema
Browse files Browse the repository at this point in the history
WIP - first try to use union for reference field with validations

fix multi reference fields

shorten union names

support multiple reference fields for a single content type

fix: type name generation

remove comment

introduce validation for single reference fields
  • Loading branch information
axe312ger committed Nov 26, 2021
1 parent 567f6b9 commit 0468b64
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 30 deletions.
48 changes: 34 additions & 14 deletions e2e-tests/contentful/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type SiteBuildMetadata implements Node @dontInfer {

interface ContentfulReference implements Node {
id: ID!
sys: ContentfulSys
sys: ContentfulSys!
}

type ContentfulSys {
Expand All @@ -145,13 +145,23 @@ type ContentfulContentType implements Node @dontInfer {
description: String!
}

interface ContentfulEntry implements Node {
interface ContentfulEntry implements ContentfulReference & Node {
id: ID!
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
}

type ContentfulMetadata @dontInfer {
tags: [ContentfulTag]! @link(by: "id", from: "tags___NODE")
}

type ContentfulTag implements Node @dontInfer {
name: String!
contentful_id: String!
}

type ContentfulAsset implements ContentfulReference & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
title: String
description: String
contentType: String
Expand Down Expand Up @@ -203,7 +213,8 @@ type ContentfulText implements Node @dontInfer {
}

type ContentfulContentTypeNumber implements ContentfulReference & ContentfulEntry & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
title: String
integer: Int
integerLocalized: Int
Expand All @@ -212,7 +223,8 @@ type ContentfulContentTypeNumber implements ContentfulReference & ContentfulEntr
}

type ContentfulContentTypeText implements ContentfulReference & ContentfulEntry & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
title: String
short: String
shortLocalized: String
Expand All @@ -224,7 +236,8 @@ type ContentfulContentTypeText implements ContentfulReference & ContentfulEntry
}

type ContentfulContentTypeMediaReference implements ContentfulReference & ContentfulEntry & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
title: String
one: ContentfulAsset @link(by: "id", from: "one___NODE")
oneLocalized: ContentfulAsset @link(by: "id", from: "oneLocalized___NODE")
Expand All @@ -233,14 +246,16 @@ type ContentfulContentTypeMediaReference implements ContentfulReference & Conten
}

type ContentfulContentTypeBoolean implements ContentfulReference & ContentfulEntry & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
title: String
boolean: Boolean
booleanLocalized: Boolean
}

type ContentfulContentTypeDate implements ContentfulReference & ContentfulEntry & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
title: String
date: Date @dateformat
dateTime: Date @dateformat
Expand All @@ -249,29 +264,33 @@ type ContentfulContentTypeDate implements ContentfulReference & ContentfulEntry
}

type ContentfulContentTypeLocation implements ContentfulReference & ContentfulEntry & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
title: String
location: ContentfulLocation
locationLocalized: ContentfulLocation
}

type ContentfulContentTypeJson implements ContentfulReference & ContentfulEntry & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
title: String
json: JSON
jsonLocalized: JSON
}

type ContentfulContentTypeRichText implements ContentfulReference & ContentfulEntry & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
title: String
richText: ContentfulRichText
richTextLocalized: ContentfulRichText
richTextValidated: ContentfulRichText
}

type ContentfulContentTypeContentReference implements ContentfulReference & ContentfulEntry & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
title: String
one: ContentfulEntry @link(by: "id", from: "one___NODE")
oneLocalized: ContentfulEntry @link(by: "id", from: "oneLocalized___NODE")
Expand All @@ -280,7 +299,8 @@ type ContentfulContentTypeContentReference implements ContentfulReference & Cont
}

type ContentfulContentTypeValidatedContentReference implements ContentfulReference & ContentfulEntry & Node @dontInfer {
sys: ContentfulSys
sys: ContentfulSys!
metadata: ContentfulMetadata!
title: String
oneItemSingleType: ContentfulEntry @link(by: "id", from: "oneItemSingleType___NODE")
oneItemManyTypes: ContentfulEntry @link(by: "id", from: "oneItemManyTypes___NODE")
Expand Down
17 changes: 13 additions & 4 deletions e2e-tests/contentful/src/pages/content-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ export const pageQuery = graphql`
}
one {
__typename
sys {
id
... on ContentfulEntry {
sys {
id
}
}
... on ContentfulContentTypeText {
title
Expand Down Expand Up @@ -152,8 +154,10 @@ export const pageQuery = graphql`
}
many {
__typename
sys {
id
... on ContentfulEntry {
sys {
id
}
}
... on ContentfulContentTypeText {
title
Expand All @@ -165,6 +169,11 @@ export const pageQuery = graphql`
}
... on ContentfulContentTypeContentReference {
title
... on ContentfulEntry {
sys {
id
}
}
one {
... on ContentfulContentTypeText {
title
Expand Down
12 changes: 8 additions & 4 deletions e2e-tests/contentful/src/pages/rich-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ export const pageQuery = graphql`
title
one {
__typename
sys {
id
... on ContentfulEntry {
sys {
id
}
}
... on ContentfulContentTypeText {
title
Expand All @@ -179,8 +181,10 @@ export const pageQuery = graphql`
}
many {
__typename
sys {
id
... on ContentfulEntry {
sys {
id
}
}
... on ContentfulContentTypeText {
title
Expand Down
69 changes: 63 additions & 6 deletions packages/gatsby-source-contentful/src/generate-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,64 @@ const ContentfulDataTypes = new Map([
],
])

const getLinkFieldType = (linkType, field) => {
const unionsNameSet = new Set()

const getLinkFieldType = (linkType, field, schema, createTypes) => {
// Check for validations
const validations =
field.type === `Array` ? field.items?.validations : field?.validations

if (validations) {
// We only handle content type validations
const linkContentTypeValidation = validations.find(
({ linkContentType }) => !!linkContentType
)
if (linkContentTypeValidation) {
const { linkContentType } = linkContentTypeValidation
const contentTypes = Array.isArray(linkContentType)
? linkContentType
: [linkContentType]

// Full type names for union members, shorter variant for the union type name
const translatedTypeNames = contentTypes.map(typeName =>
makeTypeName(typeName)
)
const shortTypeNames = contentTypes.map(typeName =>
makeTypeName(typeName, ``)
)

// Single content type
if (translatedTypeNames.length === 1) {
return {
type: translatedTypeNames.shift(),
extensions: {
link: { by: `id`, from: `${field.id}___NODE` },
},
}
}

// Multiple content types
const unionName = [`UnionContentful`, ...shortTypeNames].join(``)

if (!unionsNameSet.has(unionName)) {
unionsNameSet.add(unionName)
createTypes(
schema.buildUnionType({
name: unionName,
types: translatedTypeNames,
})
)
}

return {
type: unionName,
extensions: {
link: { by: `id`, from: `${field.id}___NODE` },
},
}
}
}

return {
type: `Contentful${linkType}`,
extensions: {
Expand All @@ -79,19 +136,19 @@ const getLinkFieldType = (linkType, field) => {
}
}

const translateFieldType = field => {
const translateFieldType = (field, schema, createTypes) => {
let fieldType
if (field.type === `Array`) {
// Arrays of Contentful Links or primitive types
const fieldData =
field.items.type === `Link`
? getLinkFieldType(field.items.linkType, field)
: translateFieldType(field.items)
? getLinkFieldType(field.items.linkType, field, schema, createTypes)
: translateFieldType(field.items, schema, createTypes)

fieldType = { ...fieldData, type: `[${fieldData.type}]` }
} else if (field.type === `Link`) {
// Contentful Link (reference) field types
fieldType = getLinkFieldType(field.linkType, field)
fieldType = getLinkFieldType(field.linkType, field, schema, createTypes)
} else {
// Primitive field types
fieldType = ContentfulDataTypes.get(field.type)(field)
Expand Down Expand Up @@ -371,7 +428,7 @@ export function generateSchema({
if (field.disabled || field.omitted) {
return
}
fields[field.id] = translateFieldType(field)
fields[field.id] = translateFieldType(field, schema, createTypes)
})

const type = pluginConfig.get(`useNameForId`)
Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby-source-contentful/src/normalize.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// @ts-check
import _ from "lodash"

const typePrefix = `ContentfulContentType`
export const makeTypeName = type =>
export const makeTypeName = (type, typePrefix = `ContentfulContentType`) =>
_.upperFirst(_.camelCase(`${typePrefix} ${type}`))

export const getLocalizedField = ({ field, locale, localesFallback }) => {
Expand Down

0 comments on commit 0468b64

Please sign in to comment.