GraphQL Schema
When working with GraphQL it is useful to have the schema for development of other projects that need to call on your GraphQL endpoint. In Payload the schema is controlled by your collections and globals and is made available to the developer or third parties, it is not necessary for developers using Payload to write schema types manually.
Schema generation script
Run the following command in a Payload project to generate your project's GraphQL schema from Payload:
You can run this command whenever you need to regenerate your GraphQL schema and output it to a file, and then you can use the schema for writing your own GraphQL elsewhere in other projects.
Custom output file path
Custom Field Schemas
For array
, block
, group
and named tab
fields, you can generate top level reusable interfaces. The following group field config:
will generate:
The above example outputs all your definitions to a file relative from your payload config as ./graphql/schema.graphql
. By default, the file will be output to your current working directory as schema.graphql
.
Adding an NPM script
Payload will automatically try and locate your config, but might not always be able to find it. For example, if you are working in a /src
directory or similar, you need to tell Payload where to find your config manually by using an environment variable. If this applies to you, you can create an NPM script to make generating your types easier.
To add an NPM script to generate your types and show Payload where to find your config, open your package.json
and update the scripts
property to the following:
Now you can run yarn generate:graphQLSchema
to easily generate your schema.