2020-10-19 23:56:17 +02:00
---
title: Building the schema
summary: Turn your schema configuration into executable code
2022-06-08 07:23:48 +02:00
icon: hammer
2020-10-19 23:56:17 +02:00
---
# Getting started
[CHILDREN asList]
[alert]
You are viewing docs for a pre-release version of silverstripe/graphql (4.x).
Help us improve it by joining #graphql on the [Community Slack ](https://www.silverstripe.org/blog/community-slack-channel/ ),
and report any issues at [github.com/silverstripe/silverstripe-graphql ](https://github.com/silverstripe/silverstripe-graphql ).
Docs for the current stable version (3.x) can be found
[here ](https://github.com/silverstripe/silverstripe-graphql/tree/3 )
[/alert]
## Building the schema
2022-06-08 07:23:48 +02:00
The primary API surface of the `silverstripe/graphql` module is the yaml configuration, along
with some [procedural configuration ](using_procedural_code ). It is important to understand
2020-10-19 23:56:17 +02:00
that **none of this configuration gets interpreted at runtime** . Loading the schema configuration
2022-06-08 07:23:48 +02:00
(which we refer to as the "schema definition") at runtime and converting it to executable code
has dire effects on performance, making API requests slower and slower as the schema grows larger.
2020-10-19 23:56:17 +02:00
To mitigate this problem, the schema that gets executed at runtime is **generated PHP code** .
This code generation happens during a build step, and it is critical to run this build step
2022-06-08 07:23:48 +02:00
whenever the schema definition changes, or a new schema definition is added.
2020-10-19 23:56:17 +02:00
2022-06-08 07:23:48 +02:00
### What triggers a GraphQL code build?
2020-10-19 23:56:17 +02:00
2022-06-08 07:23:48 +02:00
* Any time you run the `dev/graphql/build` command to explicitly build your GraphQL schemas.
* Any time you run the `dev/build` command on your project.
* `silverstripe/graphql` will attempt to generate your schema "on-demand" on the first GraphQL request _only_ if it wasn’ t already generated.
2020-10-19 23:56:17 +02:00
2022-06-08 07:23:48 +02:00
[warning]
Relying on the "on-demand" schema generation on the first GraphQL request requires some additional consideration.
See [deploying the schema ](deploying_the_schema#on-demand ).
[/warning]
2020-10-19 23:56:17 +02:00
2022-06-08 07:23:48 +02:00
#### Running `dev/graphql/build`
2020-10-19 23:56:17 +02:00
2022-06-08 07:23:48 +02:00
The main command for generating the schema code is `dev/graphql/build` .
`vendor/bin/sake dev/graphql/build`
This command takes an optional `schema` parameter. If you only want to generate a specific schema
(e.g. generate your custom schema, but not the CMS schema), you should pass in the name of the
schema you want to build.
[info]
If you do not provide a `schema` parameter, the command will build all schemas.
[/info]
`vendor/bin/sake dev/graphql/build schema=default`
2020-10-19 23:56:17 +02:00
[info]
2022-06-08 07:23:48 +02:00
Most of the time, the name of your custom schema is `default` . If you're editing DataObjects
that are accessed with GraphQL in the CMS, you may have to rebuild the `admin` schema as well.
2021-02-17 00:40:27 +01:00
[/info]
2020-10-19 23:56:17 +02:00
2022-06-08 07:23:48 +02:00
Keep in mind that some of your changes will be in YAML in the `_config/` directory, which also
requires a flush.
2020-10-19 23:56:17 +02:00
2022-06-08 07:23:48 +02:00
`vendor/bin/sake dev/graphql/build schema=default flush=1`
#### Building on dev/build
By default, all schemas will be built during `dev/build` . To disable this, change the config:
2020-10-19 23:56:17 +02:00
```yaml
SilverStripe\GraphQL\Extensions\DevBuildExtension:
enabled: false
```
### Caching
2022-06-08 07:23:48 +02:00
Generating code is a pretty expensive process. A large schema with 50 `DataObject` classes exposing
2020-10-19 23:56:17 +02:00
all their operations can take up to **20 seconds** to generate. This may be acceptable
for initial builds and deployments, but during incremental development this can really
slow things down.
To mitigate this, the generated code for each type is cached against a signature.
2022-06-08 07:23:48 +02:00
If the type hasn't changed, it doesn't get re-built. This reduces build times to **under one second** for incremental changes.
2020-10-19 23:56:17 +02:00
2022-06-08 07:23:48 +02:00
#### Clearing the schema cache
2020-10-19 23:56:17 +02:00
2022-06-08 07:23:48 +02:00
If you want to completely re-generate your schema from scratch, you can add `clear=1` to the `dev/graphql/build` command.
2020-10-19 23:56:17 +02:00
2022-06-08 07:23:48 +02:00
`vendor/bin/sake dev/graphql/build schema=default clear=1`
2020-10-19 23:56:17 +02:00
If your schema is producing unexpected results, try using `clear=1` to eliminate the possibility
of a caching issue. If the issue is resolved, record exactly what you changed and [create an issue ](https://github.com/silverstripe/silverstripe-graphql/issues/new ).
### Build gotchas
2022-06-08 07:23:48 +02:00
Keep in mind that it's not always explicit schema definition changes that require a build.
2020-10-19 23:56:17 +02:00
Anything influencing the output of the schema will require a build. This could include
tangential changes such as:
2022-06-08 07:23:48 +02:00
* Updating the `$db` array (or relationships) of a `DataObject` class that has `fields: '*'` (i.e. include all fields on that class in the schema).
2020-10-19 23:56:17 +02:00
* Adding a new resolver for a type that uses [resolver discovery ](../working_with_generic_types/resolver_discovery )
2022-06-08 07:23:48 +02:00
* Adding an extension to a `DataObject` class
* Adding a new subclass of a `DataObject` class that is already exposed
2020-10-19 23:56:17 +02:00
### Viewing the generated code
2022-06-08 07:23:48 +02:00
By default, the generated PHP code is placed in the `.graphql-generated/` directory in the root of your project.
It is not meant to be accessible through your webserver, Which is ensured by keeping it outside of the
`public/` webroot and the inclusion of a `.htaccess` file in each schema folder.
2021-02-17 00:40:27 +01:00
Additional files are generated for CMS operation in `public/_graphql/` , and
2022-06-08 07:23:48 +02:00
those _are_ meant to be accessible through your webserver.
2021-02-17 00:40:27 +01:00
See [Tips and Tricks: Schema Introspection ](tips_and_tricks#schema-introspection )
to find out how to generate these files for your own schema.
2022-06-08 07:23:48 +02:00
[alert]
While it is safe for you to view these files, you should not manually alter them. If you need to make a change
to your GraphQL schema, you should [update the schema definition ](configuring_your_schema ) and rebuild your schema.
[/alert]
2020-10-19 23:56:17 +02:00
### Further reading
[CHILDREN]