- Activating the default server
- Open up the default server that comes pre-configured with the module
- Building the schema
- Turn your schema configuration into executable code
- Building a schema with procedural code
- Use PHP code to build your schema
- Deploying the schema
- Deploy your GraphQL schema to a test or production environment
- Configuring your schema
- Add a basic type to the schema configuration
Building the schema
The primary API surface of the
silverstripe/graphql module is the yaml configuration, along
with some procedural configuration. It is important to understand
that none of this configuration gets interpreted at runtime. Loading the schema configuration
(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.
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 whenever the schema definition changes, or a new schema definition is added.
What triggers a GraphQL code build?
- Any time you run the
dev/graphql/buildcommand to explicitly build your GraphQL schemas.
- Any time you run the
dev/buildcommand on your project.
silverstripe/graphqlwill attempt to generate your schema "on-demand" on the first GraphQL request only if it wasn’t already generated.
The main command for generating the schema code is
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.
schemaparameter, the command will build all schemas.
vendor/bin/sake dev/graphql/build schema=default
default. If you're editing DataObjects that are accessed with GraphQL in the CMS, you may have to rebuild the
adminschema as well.
Keep in mind that some of your changes will be in YAML in the
_config/ directory, which also
requires a flush.
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:
SilverStripe\GraphQL\Extensions\DevBuildExtension: enabled: false
Generating code is a pretty expensive process. A large schema with 50
DataObject classes exposing
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. If the type hasn't changed, it doesn't get re-built. This reduces build times to under one second for incremental changes.
Clearing the schema cache
If you want to completely re-generate your schema from scratch, you can add
clear=1 to the
vendor/bin/sake dev/graphql/build schema=default clear=1
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.
Keep in mind that it's not always explicit schema definition changes that require a build. Anything influencing the output of the schema will require a build. This could include tangential changes such as:
- Updating the
$dbarray (or relationships) of a
DataObjectclass that has
fields: '*'(i.e. include all fields on that class in the schema).
- Adding a new resolver for a type that uses resolver discovery
- Adding an extension to a
- Adding a new subclass of a
DataObjectclass that is already exposed
Viewing the generated code
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.
Additional files are generated for CMS operation in
those are meant to be accessible through your webserver.
See Tips and Tricks: Schema Introspection
to find out how to generate these files for your own schema.