A customFetch implementation for using GraphQL Code Generator with Prismic CMS
This package helps you connect an existing GraphQL Code Generator setup to an existing Prismic CMS endpoint.
If you're not familiar with graphql-codegen, follow the GraphQL Code Generator getting started guide to set up graphql-codegen (and graphql itself) with your npm/yarn-based app, and then come back here to get graphql-codegen talking to your Prismic endpoint.
npm i --save codegen-prismic-fetchOr with yarn
yarn add codegen-prismic-fetchAdd a customFetch to your codegen.yml:
customFetch: codegen-prismic-fetchSee the codegen docs for details
on customFetch usage.
If your API is private you'll need to specify an access token. You can do this in any of three ways:
- Set the
PRISMIC_ACCESS_TOKENenvironment variable - Include
access_tokenin yourcodegen.ymlschema URL:schema: "https://[your-repo-name].cdn.prismic.io/graphql?access_token=YOUR_TOKEN" - Include an
Authorizationheader underschemain yourcodegen.yml. Make sure to include the prefixToken, eg:
schema:
https://[your-repo-name].cdn.prismic.io/graphql:
headers:
Authorization: Token YOUR_TOKENSetting customFetch globally
customFetch: "codegen-prismic-fetch"
schema: https://[your-repo-name].cdn.prismic.io/graphqlSetting customFetch under the schema
schema:
https://[your-repo-name].cdn.prismic.io/graphql:
customFetch: "codegen-prismic-fetch"With an access token
schema:
https://[your-repo-name].cdn.prismic.io/graphql:
customFetch: "codegen-prismic-fetch"
headers:
Authorization: Token YOUR_TOKENPrismic has an unusual requirement which makes this library necessary - before making a graphql query we must make a call to
the REST API to find the identifier of the current "master" ref, and then we must pass it along in any graphql query
by setting the Prismic-ref HTTP header. This library infers the API endpoint from your graphql endpoint, performs a
fetch to get your master ref, and then adds that into codegen's graphql introspection queries.
Prismic also requires all queries to use the GET method. You can specify this with codegen's method: GET option,
but that's not necessary when using this library - we effectively set it for you by transforming all requests to GET
requests.