Skip to content

Commit

Permalink
refactor: clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed Jul 20, 2021
1 parent cf159fe commit a7c246b
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import React from "react"

export const ContentfulContentReference = ({
one,
many,
content_reference,
title,
}) => {
const references = [
one,
...(many || []),
...(content_reference || []),
].filter(Boolean)
export const ContentfulContentReference = ({ one, many, title }) => {
const references = [one, ...(many || [])].filter(Boolean)
return (
<p data-cy-id="reference">
[ContentfulReference] {title}: [
Expand Down
7 changes: 2 additions & 5 deletions e2e-tests/contentful/src/pages/content-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,12 @@ export const pageQuery = graphql`
contentful_id
one {
__typename
contentful_id
... on ContentfulText {
contentful_id
title
short
}
... on ContentfulContentReference {
contentful_id
title
one {
... on ContentfulText {
Expand Down Expand Up @@ -138,18 +137,16 @@ export const pageQuery = graphql`
}
many {
__typename
contentful_id
... on ContentfulText {
contentful_id
title
short
}
... on ContentfulNumber {
contentful_id
title
integer
}
... on ContentfulContentReference {
contentful_id
title
one {
... on ContentfulText {
Expand Down
28 changes: 20 additions & 8 deletions e2e-tests/contentful/src/pages/rich-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,46 @@ export const pageQuery = graphql`
contentful_id
title
one {
__typename
contentful_id
... on ContentfulText {
title
short
}
... on ContentfulContentReference {
contentful_id
title
content_reference {
one {
... on ContentfulContentReference {
title
}
}
many {
... on ContentfulContentReference {
contentful_id
title
}
}
}
}
many {
__typename
contentful_id
... on ContentfulText {
contentful_id
title
short
}
... on ContentfulNumber {
contentful_id
title
integer
}
... on ContentfulContentReference {
contentful_id
title
content_reference {
one {
... on ContentfulContentReference {
title
}
}
many {
... on ContentfulContentReference {
id
title
}
}
Expand Down
208 changes: 109 additions & 99 deletions packages/gatsby-source-contentful/src/generate-schema.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,106 @@
const fs = require(`fs`)
const { cloneDeep } = require(`lodash`)

import { makeTypeName } from "./normalize"

const types = []
// Contentful content type schemas
const ContentfulDataTypes = new Map([
[
`Symbol`,
() => {
return { type: `String` }
},
],
[
`Text`,
field => {
return {
type: `ContentfulNodeTypeText`,
extensions: {
link: { by: `id`, from: `${field.id}___NODE` },
},
}
},
],
[
`Integer`,
() => {
return { type: `Int` }
},
],
[
`Number`,
() => {
return { type: `Float` }
},
],
[
`Date`,
() => {
return {
type: `Date`,
extensions: {
dateformat: {},
},
}
},
],
[
`Object`,
() => {
return { type: `JSON` }
},
],
[
`Boolean`,
() => {
return { type: `Boolean` }
},
],
[
`Location`,
() => {
return { type: `ContentfulNodeTypeLocation` }
},
],
[
`RichText`,
() => {
return { type: `ContentfulNodeTypeRichText` }
},
],
])

const getLinkFieldType = (linkType, field) => {
return {
type: `Contentful${linkType}`,
extensions: {
link: { by: `id`, from: `${field.id}___NODE` },
},
}
}

const translateFieldType = field => {
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)

fieldType = { ...fieldData, type: `[${fieldData.type}]` }
} else if (field.type === `Link`) {
// Contentful Link (reference) field types
fieldType = getLinkFieldType(field.linkType, field)
} else {
// Primitive field types
fieldType = ContentfulDataTypes.get(field.type)(field)
}

if (field.required) {
fieldType.type = `${fieldType.type}!`
}

return fieldType
}

function generateAssetTypes({ createTypes }) {
createTypes(`
Expand All @@ -16,8 +113,8 @@ function generateAssetTypes({ createTypes }) {
contentful_id: String!
id: ID!
spaceId: String!
createdAt: String! # Date @dateform,
updatedAt: String! # Date @dateform,
createdAt: Date @dateformat
updatedAt: Date @dateformat
}
`)

Expand Down Expand Up @@ -58,13 +155,6 @@ export function generateSchema({
pluginConfig,
contentTypeItems,
}) {
// logger @todo remove it
const origTypes = createTypes
createTypes = (...all) => {
types.push(all)
origTypes(...all)
}

createTypes(`
interface ContentfulInternalReference implements Node {
contentful_id: String!
Expand Down Expand Up @@ -140,11 +230,9 @@ export function generateSchema({
const v = obj[k]
if (v && v.sys && v.sys.type === `Link`) {
if (v.sys.linkType === `Asset`) {
console.log(`adding asset`, v)
referencedAssets.add(v.sys.id)
}
if (v.sys.linkType === `Entry`) {
console.log(`adding entry`, v)
referencedEntries.add(v.sys.id)
}
} else if (v && typeof v === `object`) {
Expand Down Expand Up @@ -182,7 +270,7 @@ export function generateSchema({
})
)

// Is there a way to have this as string and let transformer-remark replace it with an object?
// @todo Is there a way to have this as string and let transformer-remark replace it with an object?
createTypes(
schema.buildObjectType({
name: `ContentfulNodeTypeText`,
Expand All @@ -196,88 +284,14 @@ export function generateSchema({
})
)

// Contentful content type schemas
const ContentfulDataTypes = new Map([
[`Symbol`, `String`],
[
`Text`,
{
type: `ContentfulNodeTypeText`,
extensions: {
link: { by: `id` },
},
},
],
[`Integer`, `Int`],
[`Number`, `Float`],
[
`Date`,
{
type: `Date`,
extensions: {
dateformat: {},
},
},
],
[`Object`, `JSON`],
[`Boolean`, `Boolean`],
[`Location`, `ContentfulNodeTypeLocation`],
[`RichText`, `ContentfulNodeTypeRichText`],
])

const getLinkFieldType = linkType => {
return {
type: `Contentful${linkType}`,
extensions: {
link: { by: `id` },
},
}
}

const translateFieldType = field => {
let id
if (field.type === `Array`) {
const fieldData =
field.items.type === `Link`
? getLinkFieldType(field.items.linkType)
: translateFieldType(field.items)

id =
typeof fieldData === `string`
? `[${fieldData}]`
: { ...fieldData, type: `[${fieldData.type}]` }
} else if (field.type === `Link`) {
id = getLinkFieldType(field.linkType)
} else {
id = ContentfulDataTypes.get(field.type)
}

if (typeof id === `string`) {
return [id, field.required && `!`].filter(Boolean).join(``)
}

id = cloneDeep(id)

if (field.required) {
id.type = `${id.type}!`
}

if (id?.extensions?.link) {
id.extensions.link.from = `${field.id}___NODE`
}

return id
}

for (const contentTypeItem of contentTypeItems) {
try {
const fields = {}
contentTypeItem.fields.forEach(field => {
if (field.disabled || field.omitted) {
return
}
const type = translateFieldType(field)
fields[field.id] = typeof type === `string` ? { type } : type
fields[field.id] = translateFieldType(field)
})

const type = pluginConfig.get(`useNameForId`)
Expand All @@ -294,8 +308,8 @@ export function generateSchema({
node_locale: { type: `String!` },
// @todo these should be real dates and in sys
spaceId: { type: `String!` },
createdAt: { type: `String!` }, // { type: `Date`, extensions: { dateform: {} } },
updatedAt: { type: `String!` }, // { type: `Date`, extensions: { dateform: {} } },
createdAt: { type: `Date`, extensions: { dateformat: {} } },
updatedAt: { type: `Date`, extensions: { dateformat: {} } },
// @todo add metadata
sys: { type: `ContentfulInternalSys` },
...fields,
Expand All @@ -305,19 +319,15 @@ export function generateSchema({
`ContentfulEntry`,
`Node`,
],
extensions: { dontInfer: {} },
})
)
} catch (err) {
err.message = `Unable to create schema for Contentful Content Type ${
contentTypeItem.name || contentTypeItem.sys.id
}:\n${err.message}`
console.log(err.stack)
throw err
}
}

fs.writeFileSync(
process.cwd() + `/generated-types.json`,
JSON.stringify(types, null, 2)
)
createTypes = origTypes
}
2 changes: 1 addition & 1 deletion packages/gatsby-source-contentful/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ exports.createAssetNodes = ({
assetNode.sys.revision = assetItem.sys.revision
}

assetNode.internal.createNodePromises.push(createNode(assetNode))
createNodePromises.push(createNode(assetNode))
})

return createNodePromises
Expand Down
Loading

0 comments on commit a7c246b

Please sign in to comment.