Change a resolver behavior

In a project, it is quite usual to have the need to change the implementation of the resolver associated with a GraphQL field. This page explains how to do it properly so that the project remains as maintenable as possible.

As an example, we will change the way the Product meta_description field value is generated.

Create dedicated GraphQL module

First, you have to create a GraphQL module. For that, you can follow the process detailed in the Create a new GraphQL module. Don’t forget to also register the module in Front-Commerce.

For this example, this results in src/server/modules/productmetadescription/index.js containing:

export default {
  namespace: "ProductMetaDescription",
};

Set the module dependency

Before being able to inject you custom resolver logic, you first need to find the module that defines the resolver for the field. In our example, the Product meta_description is resolved by the resolver provided by the Magento2/Catalog/Products module. As a result, the Magento2/Catalog/Products module must be added as a dependency of our custom module:

export default {
  namespace: "ProductMetaDescription",
+ dependencies: ["Magento2/Catalog/Products"],
};

Implement your custom resolver logic

Our custom module can now provide a resolver with a custom logic. In src/server/modules/productmetadescription/index.js:

+import resolvers from "./resolvers";
+
export default {
  namespace: "ChangeResolverBehavior",
  dependencies: ["Magento2/Catalog/Products"],
+ resolvers,
};

And the resolver could look like:

export default {
  Product: {
    meta_description: (product) => {
      // your implementation here
      return "my custom description";
    },
  },
};

This custom resolver will now be merged with the existing ones and the meta_description will be resolved with our custom implementation instead of the default one.

Edit on GitHub