Version 4 supported
This version of Silverstripe CMS is still supported though will not receive any additional features. Go to documentation for the most recent stable version.

Extending the schema

Adding a custom model
Add a new class-backed type beyond DataObject
Adding a custom operation
Add a new operation for model types
Adding middleware
Add middleware to to extend query execution
The global schema
How to push modifications to every schema in the project
You are viewing docs for silverstripe/graphql 4.x. If you are using 3.x, documentation can be found in the github repository

Adding middleware

Middleware is any piece of functionality that is interpolated into a larger process. A key feature of middleware is that it can be used with other middlewares in sequence and not have to worry about the order of execution.

In silverstripe/graphql, middleware is used for query execution, but could ostensibly be used elsewhere too if the API ever accomodates such an expansion.

The middleware API in the silverstripe/graphql module is separate from other common middleware APIs in Silverstripe CMS, such as HTTPMiddleware. The two are not interchangable.

The signature for middleware (defined in QueryMiddleware) looks like this:

public function process(Schema $schema, string $query, array $context, array $vars, callable $next)

The return value should be ExecutionResult or an array.

  • $schema: The underlying Schema object. Useful to inspect whether types are defined in a schema.
  • $query: The raw query string.
  • $context: An arbitrary array which holds information shared between resolvers. Use implementors of ContextProvider to get and set data, rather than relying on the array keys directly.
  • $vars: An array of (optional) Query Variables.
  • $next: A callable referring to the next middleware in the chain

Let's write a simple middleware that logs our queries as they come in.

namespace MyProject\Middleware;

use SilverStripe\GraphQL\Middleware\QueryMiddleware;
use SilverStripe\GraphQL\QueryHandler\UserContextProvider;
use GraphQL\Type\Schema;

class LoggingMiddleware implements QueryMiddleware
    public function process(Schema $schema, string $query, array $context, array $vars, callable $next)
        $member = UserContextProvider::get($context);
                'Query executed: %s by %s',
                $member ? $member->Title : '<anonymous>';
        // Hand off execution to the next middleware
        return $next($schema, $query, $context, $vars);

Now we can register the middleware with our query handler:

    class: SilverStripe\GraphQL\QueryHandler\QueryHandler
        logging: '%$MyProject\Middleware\LoggingMiddleware'