exception-in-graphql-banner-image

GraphQL Resolvers in Magento 2

A Resolver is the core execution unit behind every GraphQL query and mutation in Magento 2. Resolvers are responsible for processing requests, fetching data, applying business logic, and transforming results into GraphQL-compliant responses.

In simple terms, a resolver acts as the bridge between the GraphQL schema and Magento’s underlying models, services, and repositories.

How a GraphQL Resolver Works

When a GraphQL request is executed, Magento invokes the attached resolver class. Each resolver processes the request using the following parameters:

  • $field – Metadata for the GraphQL field being resolved
  • $context – Shared execution context (user, store, auth info)
  • $info – Schema and resolve-tree details
  • $value – Parent resolver’s output (usually null)
  • $args – Input arguments passed in the query
$field   : Magento\Framework\GraphQl\Config\Element\Field
$context : Magento\Framework\GraphQl\Query\Resolver\ContextInterface
$info    : Magento\Framework\GraphQl\Schema\Type\ResolveInfo
$value   : array|null
$args    : array
  

Resolver Interfaces in Magento 2

Magento provides three resolver interfaces. Choosing the correct one impacts performance and scalability.

  • ResolverInterface – Resolves one request at a time
  • BatchResolverInterface – Resolves multiple requests in a batch
  • BatchServiceContractResolverInterface – Delegates batch resolving to a service

ResolverInterface (Single Resolver)

This is the simplest resolver type. It resolves a single field or branch and is commonly used for simple queries and mutations.

<?php
namespace Vendor\Module\Model\Resolver;

use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class SimpleResolver implements ResolverInterface
{
    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        return 'Resolver Response';
    }
}
  

BatchResolverInterface

Batch resolvers collect multiple GraphQL requests for the same field and resolve them in a single operation. This drastically improves performance for nested or repeated queries.

class RelatedProducts implements BatchResolverInterface
{
    public function resolve($context, $field, array $requests)
    {
        $response = new BatchResponse();

        foreach ($requests as $request) {
            $response->addResponse(
                $request,
                ['sku' => 'related-sku']
            );
        }

        return $response;
    }
}
  

Batch resolvers are ideal for nested queries like loading related products for many products in a single request tree.

BatchServiceContractResolverInterface

This resolver delegates data loading to a service contract. The resolver only converts GraphQL requests to DTOs and formats the service response.

class RelatedProductsResolver
    implements BatchServiceContractResolverInterface
{
    public function getServiceContract(): array
    {
        return [ProductLinksRetriever::class, 'getRelatedProducts'];
    }

    public function convertToServiceArgument($request)
    {
        return new RootProductCriteria(
            $request->getValue()['model']->getId()
        );
    }

    public function convertFromServiceResult($result, $request)
    {
        return $result->getLinkedProducts();
    }
}
  

Resolvers for Mutations

Mutations are defined in the schema.graphqls file and mapped directly to a resolver class using the @resolver directive.

type Mutation {
    createEmptyCart: String
        @resolver(class:
            "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CreateEmptyCart")
        @doc(description: "Creates an empty cart")
}
  

Mutation Resolver Example

<?php
namespace Vendor\Module\Model\Resolver;

use Magento\Framework\GraphQl\Query\ResolverInterface;

class MutationResolver implements ResolverInterface
{
    public function resolve(
        $field,
        $context,
        $info,
        array $value = null,
        array $args = null
    ) {
        return [
            'status' => 'success'
        ];
    }
}
  

Example Mutation Query

mutation {
    createCustomer(
        input: {
            firstname: "John"
            lastname: "Doe"
            email: "j.doe@example.com"
            password: "1w2E3R456"
        }
    ) {
        customer {
            firstname
            lastname
            email
        }
    }
}
  

Sample Response

{
  "data": {
    "createCustomer": {
      "customer": {
        "firstname": "John",
        "lastname": "Doe",
        "email": "j.doe@example.com"
      }
    }
  }
}
  

Conclusion

Resolvers form the backbone of Magento 2 GraphQL execution. By using batch resolvers and service-based resolvers wherever possible, developers can build fast, scalable, and efficient GraphQL APIs. Selecting the right resolver type ensures optimal performance and clean separation between GraphQL schema, business logic, and data services.