You are here

public function ResolveContext::getContext in GraphQL 8.3

Get a contextual value for the current field.

Allows field resolvers to inherit contextual values from their ancestors.

Parameters

string $name: The name of the context.

\GraphQL\Type\Definition\ResolveInfo $info: The resolve info object.

mixed $default: An arbitrary default value in case the context is not set.

Return value

mixed The current value of the given context or the given default value if the context wasn't set.

File

src/GraphQL/Execution/ResolveContext.php, line 62

Class

ResolveContext

Namespace

Drupal\graphql\GraphQL\Execution

Code

public function getContext($name, ResolveInfo $info, $default = NULL) {
  $operation = isset($info->operation->name->value) ? $info->operation->name->value : $info->operation->operation;
  $path = $info->path;
  do {
    $key = implode('.', $path);
    if (isset($this->contexts[$operation][$key]) && array_key_exists($name, $this->contexts[$operation][$key])) {
      return $this->contexts[$operation][$key][$name];
    }
    array_pop($path);
  } while (count($path));
  if (isset($this->rootContext[$name])) {
    return $this->rootContext[$name];
  }
  return $default;
}