Change a resolver behavior
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 to register the module in Front-Commerce.
For this example, the ProductMetaDescription
module 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.
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.