You are here

protected function Server::getContext in GraphQL 8.4

Returns the context object to use during query execution.

May return a callable to instantiate a context object for each individual query instead of a shared context. This may be useful e.g. when running batched queries where each query operation within the same request should use a separate context object.

The returned value will be passed as an argument to every type and field resolver during execution.



public function getContext() {
  $shared = ['foo' => 'bar'];

  return function (OperationParams $params, DocumentNode $document, $operation) use ($shared) {
    $private = ['bar' => 'baz'];

    return new MyContext($shared, $private);
  };
}

Parameters

\Drupal\graphql\Plugin\SchemaPluginInterface $schema: The schema plugin instance.

array $config:

Return value

mixed|callable The context object for query execution or a callable factory.

1 call to Server::getContext()
Server::configuration in src/Entity/Server.php

File

src/Entity/Server.php, line 290

Class

Server
The main GraphQL configuration and request entry point.

Namespace

Drupal\graphql\Entity

Code

protected function getContext(SchemaPluginInterface $schema, array $config) {

  // Each document (e.g. in a batch query) gets its own resolve context. This
  // allows us to collect the cache metadata and contextual values (e.g.
  // inheritance for language) for each query separately.
  return function (OperationParams $params, DocumentNode $document, $type) use ($schema, $config) {
    $context = new ResolveContext($this, $params, $document, $type, $config);
    $context
      ->addCacheTags([
      'graphql_response',
    ]);
    if ($this instanceof CacheableDependencyInterface) {
      $context
        ->addCacheableDependency($this);
    }
    if ($schema instanceof CacheableDependencyInterface) {
      $context
        ->addCacheableDependency($schema);
    }
    return $context;
  };
}