Resolvers, Mutations and Subscriptions
Fetch and change data through resolvers.
Resolvers Are Where Logic Lives
The schema describes what exists; resolvers decide how each field's value is produced. A resolver is just a callable. Mutations are resolvers that change state. Subscriptions stream values over time. This lesson covers all three and the execution model that ties them together.
The Resolver Signature
Every resolver receives four arguments: ($objectValue, $args, $context, ResolveInfo $info).
- $objectValue — the parent's resolved value (the
rootValueat the top). - $args — the field's arguments.
- $context — per-request shared state (DB handle, current user).
- $info — AST/field metadata (field name, selection set, path).
<?php
use GraphQL\Type\Definition\ResolveInfo;
$resolve = function ($objectValue, array $args, $context, ResolveInfo $info) {
// $context['db'], $context['user'] set up per request
return $context['db']->find($args['id']);
};
All lessons in this course
- GraphQL vs REST
- Building a Schema with graphql-php
- Resolvers, Mutations and Subscriptions
- Performance: N+1 and DataLoader