Skip to content

Commit

Permalink
fix: detect RichText automatically to create types
Browse files Browse the repository at this point in the history
  • Loading branch information
notrab committed Jan 11, 2022
1 parent f93a95b commit d4d84a2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
1 change: 0 additions & 1 deletion demo/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module.exports = {
'https://api-eu-central-1.graphcms.com/v2/ckclvjtet0f0901z69og3f3gm/master',
locales: ['en', 'de'],
stages: ['DRAFT', 'PUBLISHED'],
richTextEmbedTypeNames: ['ProductDescription'],
},
},
],
Expand Down
23 changes: 11 additions & 12 deletions gatsby-source-graphcms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,17 @@ module.exports = {

### Options

| Key | Type | Description |
| ------------------------ | ---------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `endpoint` | String (**required**) | The endpoint URL for the GraphCMS project. This can be found in the [project settings UI](https://graphcms.com/docs/guides/concepts/apis#working-with-apis). |
| `token` | String | If your GraphCMS project is **not** publicly accessible, you will need to provide a [Permanent Auth Token](https://graphcms.com/docs/reference/authorization) to correctly authorize with the API. You can learn more about creating and managing API tokens [here](https://graphcms.com/docs/guides/concepts/apis#working-with-apis). |
| `typePrefix` | String _(Default: `GraphCMS_`)\_ | The string by which every generated type name is prefixed with. For example, a type of `Post` in GraphCMS would become `GraphCMS_Post` by default. If using multiple instances of the source plugin, you **must** provide a value here to prevent type conflicts. |
| `downloadLocalImages` | Boolean _(Default: `false`)_ | Download and cache GraphCMS image assets in your Gatsby project. [Learn more](#downloading-local-image-assets). |
| `buildMarkdownNodes` | Boolean _(Default: `false`)_ | Build markdown nodes for all [`RichText`](https://graphcms.com/docs/reference/fields/rich-text) fields in your GraphCMS schema. [Learn more](#using-markdown-nodes). |
| `fragmentsPath` | String _(Default: `graphcms-fragments`)_ | The local project path where generated query fragments are saved. This is relative to your current working directory. If using multiple instances of the source plugin, you **must** provide a value here to prevent type and/or fragment conflicts. |
| `locales` | String _(Default: `['en']`)_ | An array of locale key strings from your GraphCMS project. [Learn more](#querying-localised-nodes). You can read more about working with localisation in GraphCMS [here](https://graphcms.com/docs/guides/concepts/i18n). |
| `stages` | String _(Default: `['PUBLISHED']`)_ | An array of Content Stages from your GraphCMS project. [Learn more](#querying-from-content-stages). You can read more about using Content Stages [here](https://graphcms.com/guides/working-with-content-stages). |
| `queryConcurrency` | Integer _(Default: 10)_ | The number of promises ran at once when executing queries. |
| `richTextEmbedTypeNames` | String[] _(Default: [])_ | An array of Rich Text fields that have embeds enabled in the format of `[Model][Field]`, e.g. `ProductDescription` (`Product` being the model, `Description` the field.) |
| Key | Type | Description |
| --------------------- | ---------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `endpoint` | String (**required**) | The endpoint URL for the GraphCMS project. This can be found in the [project settings UI](https://graphcms.com/docs/guides/concepts/apis#working-with-apis). |
| `token` | String | If your GraphCMS project is **not** publicly accessible, you will need to provide a [Permanent Auth Token](https://graphcms.com/docs/reference/authorization) to correctly authorize with the API. You can learn more about creating and managing API tokens [here](https://graphcms.com/docs/guides/concepts/apis#working-with-apis). |
| `typePrefix` | String _(Default: `GraphCMS_`)\_ | The string by which every generated type name is prefixed with. For example, a type of `Post` in GraphCMS would become `GraphCMS_Post` by default. If using multiple instances of the source plugin, you **must** provide a value here to prevent type conflicts. |
| `downloadLocalImages` | Boolean _(Default: `false`)_ | Download and cache GraphCMS image assets in your Gatsby project. [Learn more](#downloading-local-image-assets). |
| `buildMarkdownNodes` | Boolean _(Default: `false`)_ | Build markdown nodes for all [`RichText`](https://graphcms.com/docs/reference/fields/rich-text) fields in your GraphCMS schema. [Learn more](#using-markdown-nodes). |
| `fragmentsPath` | String _(Default: `graphcms-fragments`)_ | The local project path where generated query fragments are saved. This is relative to your current working directory. If using multiple instances of the source plugin, you **must** provide a value here to prevent type and/or fragment conflicts. |
| `locales` | String _(Default: `['en']`)_ | An array of locale key strings from your GraphCMS project. [Learn more](#querying-localised-nodes). You can read more about working with localisation in GraphCMS [here](https://graphcms.com/docs/guides/concepts/i18n). |
| `stages` | String _(Default: `['PUBLISHED']`)_ | An array of Content Stages from your GraphCMS project. [Learn more](#querying-from-content-stages). You can read more about using Content Stages [here](https://graphcms.com/guides/working-with-content-stages). |
| `queryConcurrency` | Integer _(Default: 10)_ | The number of promises ran at once when executing queries. |

## Features

Expand Down
16 changes: 10 additions & 6 deletions gatsby-source-graphcms/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ export function pluginOptionsSchema({ Joi }) {
.min(1)
.default(10)
.description(`The number of promises to run at one time.`),
richTextEmbedTypeNames: Joi.array()
.description('An array of Rich Text fields that have embeds enabled')
.items(Joi.string())
.default([]),
})
}

Expand Down Expand Up @@ -143,6 +139,12 @@ const createSourcingConfig = async (
const query = schema.getType('Query')
const queryFields = query.getFields()
const possibleTypes = schema.getPossibleTypes(nodeInterface)
const typeMap = schema.getTypeMap()

const richTextTypes = Object.keys(typeMap)
.filter((typeName) => typeName.endsWith('RichText'))
.map((value) => value.replace('RichText', ''))
.filter(Boolean)

const singularRootFieldName = (type) =>
Object.keys(queryFields).find(
Expand Down Expand Up @@ -221,6 +223,7 @@ const createSourcingConfig = async (
}),
gatsbyTypePrefix: typePrefix,
gatsbyNodeDefs: buildNodeDefinitions({ gatsbyNodeTypes, documents }),
richTextTypes,
}
}

Expand All @@ -233,11 +236,12 @@ export async function createSchemaCustomization(gatsbyApi, pluginOptions) {
buildMarkdownNodes = false,
downloadLocalImages = false,
typePrefix = 'GraphCMS_',
richTextEmbedTypeNames = [],
} = pluginOptions

const config = await createSourcingConfig(gatsbyApi, pluginOptions)

const { richTextTypes } = config

await createToolkitSchemaCustomization(config)

if (webhookBody && Object.keys(webhookBody).length) {
Expand Down Expand Up @@ -289,7 +293,7 @@ export async function createSchemaCustomization(gatsbyApi, pluginOptions) {
type ${typePrefix}RichText {
markdownNode: ${typePrefix}MarkdownNode @link
}
${richTextEmbedTypeNames.map(
${richTextTypes.map(
(typeName) => `
type ${typePrefix}${typeName}RichText implements Node {
markdownNode: ${typePrefix}MarkdownNode @link
Expand Down

0 comments on commit d4d84a2

Please sign in to comment.