diff --git a/packages/gatsby-source-graphql/README.md b/packages/gatsby-source-graphql/README.md index 5774f7eefacc6..39160efc04bd3 100644 --- a/packages/gatsby-source-graphql/README.md +++ b/packages/gatsby-source-graphql/README.md @@ -150,6 +150,58 @@ module.exports = { } ``` +## Custom transform schema function (advanced) + +It's possible to modify the remote schema, via a `transformSchema` option which customizes the way the default schema is transformed before it is merged on the Gatsby schema by the stitching process. + +The `transformSchema` function gets an object argument with the following fields: + +- schema (introspected remote schema) +- link (default link) +- resolver (default resolver) +- defaultTransforms (an array with the default transforms) +- options (plugin options) + +The return value is expected to be the final schema used for stitching. + +Below an example configuration that uses the default implementation (equivalent to not using the `transformSchema` option at all): + +```js +const { wrapSchema } = require(`@graphql-tools/wrap`) +const { linkToExecutor } = require(`@graphql-tools/links`) + +module.exports = { + plugins: [ + { + resolve: "gatsby-source-graphql", + options: { + typeName: "SWAPI", + fieldName: "swapi", + url: "https://api.graphcms.com/simple/v1/swapi", + transformSchema: ({ + schema, + link, + resolver, + defaultTransforms, + options, + }) => { + return wrapSchema( + { + schema, + executor: linkToExecutor(link), + }, + defaultTransforms + ) + } + }, + ] +} +``` + +For details, refer to [https://www.graphql-tools.com/docs/schema-wrapping](https://www.graphql-tools.com/docs/schema-wrapping). + +An use case for this feature can be seen in [this issue](https://github.com/gatsbyjs/gatsby/issues/23552). + # Refetching data By default, `gatsby-source-graphql` will only refetch the data once the server is restarted. It's also possible to configure the plugin to periodically refetch the data. The option is called `refetchInterval` and specifies the timeout in seconds. diff --git a/packages/gatsby-source-graphql/src/gatsby-node.js b/packages/gatsby-source-graphql/src/gatsby-node.js index 18fc3f2488a3e..03492ecab09d4 100644 --- a/packages/gatsby-source-graphql/src/gatsby-node.js +++ b/packages/gatsby-source-graphql/src/gatsby-node.js @@ -32,6 +32,7 @@ exports.sourceNodes = async ( createSchema, refetchInterval, batch = false, + transformSchema, } = options invariant( @@ -95,21 +96,31 @@ exports.sourceNodes = async ( return {} } - const schema = wrapSchema( - { - schema: introspectionSchema, - executor: linkToExecutor(link), - }, - [ - new StripNonQueryTransform(), - new RenameTypes(name => `${typeName}_${name}`), - new NamespaceUnderFieldTransform({ - typeName, - fieldName, + const defaultTransforms = [ + new StripNonQueryTransform(), + new RenameTypes(name => `${typeName}_${name}`), + new NamespaceUnderFieldTransform({ + typeName, + fieldName, + resolver, + }), + ] + + const schema = transformSchema + ? transformSchema({ + schema: introspectionSchema, + link, resolver, - }), - ] - ) + defaultTransforms, + options, + }) + : wrapSchema( + { + schema: introspectionSchema, + executor: linkToExecutor(link), + }, + defaultTransforms + ) addThirdPartySchema({ schema })