diff --git a/demo/gatsby-config.js b/demo/gatsby-config.js index e282d0f..3b440d3 100644 --- a/demo/gatsby-config.js +++ b/demo/gatsby-config.js @@ -15,7 +15,6 @@ module.exports = { 'https://api-eu-central-1.graphcms.com/v2/ckclvjtet0f0901z69og3f3gm/master', locales: ['en', 'de'], stages: ['DRAFT', 'PUBLISHED'], - richTextEmbedTypeNames: ['ProductDescription'], }, }, ], diff --git a/gatsby-source-graphcms/README.md b/gatsby-source-graphcms/README.md index 748f755..e7e5fb9 100644 --- a/gatsby-source-graphcms/README.md +++ b/gatsby-source-graphcms/README.md @@ -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 diff --git a/gatsby-source-graphcms/src/gatsby-node.js b/gatsby-source-graphcms/src/gatsby-node.js index b080f3f..53b38e4 100644 --- a/gatsby-source-graphcms/src/gatsby-node.js +++ b/gatsby-source-graphcms/src/gatsby-node.js @@ -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([]), }) } @@ -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( @@ -221,6 +223,7 @@ const createSourcingConfig = async ( }), gatsbyTypePrefix: typePrefix, gatsbyNodeDefs: buildNodeDefinitions({ gatsbyNodeTypes, documents }), + richTextTypes, } } @@ -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) { @@ -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