GraphQL Schema

In Payload the schema is controlled by your collections and globals. All you need to do is run the generate command and the entire schema will be created for you.

Schema generation script

Install @payloadcms/graphql as a dev dependency:

1
pnpm add @payloadcms/graphql --save-dev

Run the following command to generate the schema:

1
pnpm payload-graphql generate:schema

Custom Field Schemas

For array, block, group and named tab fields, you can generate top level reusable interfaces. The following group field config:

1
{
2
type: 'group',
3
name: 'meta',
4
interfaceName: 'SharedMeta',
5
fields: [
6
{
7
name: 'title',
8
type: 'text',
9
},
10
{
11
name: 'description',
12
type: 'text',
13
},
14
],
15
}

will generate:

1
// A top level reusable type will be generated
2
type SharedMeta {
3
title: String
4
description: String
5
}
6
7
// And will be referenced inside the generated schema
8
type Collection1 {
9
// ...other fields
10
meta: SharedMeta
11
}

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, create an NPM script to make generating types easier:

1
// package.json
2
3
{
4
"scripts": {
5
"generate:graphQLSchema": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload-graphql generate:schema"
6
}
7
}

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

Next

Querying your Documents