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.

GraphQL Schema generate script

Run the following command in a Payload project to generate your project's GraphQL schema from Payload:

payload generate:graphQLSchema

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.

GraphQL schema output file

// the remainder of your config
graphQL: {
schemaOutputFile: path.resolve(__dirname, './graphql/schema.graphql'),

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:

"scripts": {
"generate:graphQLSchema": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:graphQLSchema",

Now you can run yarn generate:graphQLSchema to easily generate your schema.