Skip to content

Commit

Permalink
add field extension for Contentful localization
Browse files Browse the repository at this point in the history
  • Loading branch information
axe312ger committed May 20, 2021
1 parent 45f2006 commit cccee87
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 39 deletions.
34 changes: 34 additions & 0 deletions packages/gatsby-source-contentful/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,40 @@ const localeState = new CascadedContext()

exports.createSchemaCustomization = ({ actions }) => {
actions.createResolverContext({ localeState })
actions.createFieldExtension({
name: `contentfulLocalized`,
args: {
contentfulFieldId: {
type: `String!`,
},
},
extend(options) {
return {
args: {
locale: `String`,
},
resolve(source, args, context, info) {
console.log(
JSON.stringify(
{ source, args, context: context.sourceContentful },
null,
2
)
)

let locale
if (args.locale) {
context.sourceContentful.localeState.set(info, args.locale)
locale = args.locale
} else {
locale = context.sourceContentful.localeState.get(info) || `en-US` // @todo we need default locale
}
const fieldValue = source.localeTest[options.contentfulFieldId] || {}
return fieldValue[locale] || null
},
}
},
})
}

exports.createResolvers = ({ createResolvers }) => {
Expand Down
102 changes: 63 additions & 39 deletions packages/gatsby-source-contentful/src/generate-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export function generateSchema({
schema,
pluginConfig,
contentTypeItems,
defaultLocale,
}) {
const getLinkFieldType = (linkType, field) => {
return {
Expand Down Expand Up @@ -47,8 +46,15 @@ export function generateSchema({
const ContentfulDataTypes = new Map([
[
`Symbol`,
() => {
return { type: `String` }
field => {
return {
type: `String`,
extensions: {
contentfulLocalized: {
contentfulFieldId: field.id,
},
},
}
},
],
[
Expand All @@ -67,76 +73,94 @@ export function generateSchema({
],
[
`Integer`,
() => {
field => {
return {
type: `Int`,
resolve(source, args, context, info) {
let locale
if (args.locale) {
context.sourceContentful.localeState.set(info, args.locale)
locale = args.locale
} else {
locale =
context.sourceContentful.localeState.get(info) || defaultLocale
}

console.log(
JSON.stringify(
{
source,
args,
context: context.sourceContentful,
info,
locale,
},
null,
2
)
)
return source[info.fieldName]
extensions: {
contentfulLocalized: {
contentfulFieldId: field.id,
},
},
}
},
],
[
`Number`,
() => {
return { type: `Float` }
field => {
return {
type: `Float`,
extensions: {
contentfulLocalized: {
contentfulFieldId: field.id,
},
},
}
},
],
[
`Date`,
() => {
field => {
return {
type: `Date`,
extensions: {
contentfulLocalized: {
contentfulFieldId: field.id,
},
dateformat: {},
},
}
},
],
[
`Object`,
() => {
return { type: `JSON` }
field => {
return {
type: `JSON`,
extensions: {
contentfulLocalized: {
contentfulFieldId: field.id,
},
},
}
},
],
[
`Boolean`,
() => {
return { type: `Boolean` }
field => {
return {
type: `Boolean`,
extensions: {
contentfulLocalized: {
contentfulFieldId: field.id,
},
},
}
},
],
[
`Location`,
() => {
return { type: `ContentfulNodeTypeLocation` }
field => {
return {
type: `ContentfulNodeTypeLocation`,
extensions: {
contentfulLocalized: {
contentfulFieldId: field.id,
},
},
}
},
],
[
`RichText`,
() => {
return { type: `ContentfulNodeTypeRichText` }
field => {
return {
type: `ContentfulNodeTypeRichText`,
extensions: {
contentfulLocalized: {
contentfulFieldId: field.id,
},
},
}
},
],
])
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-contentful/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ exports.createNodesForContentType = ({
publishedAt: entryItem.sys.updatedAt,
publishedVersion: entryItem.sys.revision,
},
localeTest: entryItem.fields,
}

// Replace text fields with text nodes so we can process their markdown
Expand Down

0 comments on commit cccee87

Please sign in to comment.