Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

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:

1
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.

Custom output file path

1
{
2
// the remainder of your config
3
graphQL: {
4
schemaOutputFile: path.resolve(__dirname, './graphql/schema.graphql'),
5
},
6
}

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', <-- here!!
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!!
2
type SharedMeta {
3
title: String
4
description: String
5
}
6
7
// example usage inside collection 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, 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:

1
{
2
"scripts": {
3
"generate:graphQLSchema": "cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload generate:graphQLSchema"
4
}
5
}

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

Next

Querying your Documents