https://embed.notionlytics.com/wt/ZXlKd1lXZGxTV1FpT2lJelkyRm1aR0l5Wm1GbE56TTBNbVl3WW1ReFpEa3pNRGhoWVdZNU9UTmtaaUlzSW5kdmNtdHpjR0ZqWlZSeVlXTnJaWEpKWkNJNklrbGpiM1pzV2pOcU1VUnBRM1ZUUm1WNFRtdHlJbjA9
Our task is to provide the data with the requested shape. To do it, we need to create a thing that will help us with collecting and organizing information within certain rules.
This thing is called a resolver.
GraphQL provides two types of constructs for accessing data:
A developer must create and assign a resolver for each of these mechanisms.
In Magento 2, the GraphQL query resolver is realized as a class that implements one of the special GraphQL interfaces and returns the result in the form of a callable function. In the most common case, GraphQL resolver is the PHP-class that implements ResolverInterface
.
The ResolverInterface
obliges us to implement the resolve()
method. Its arguments are:
$field
– contains information about the current field that the resolver is working with.$context
– provides a reference to an object containing information common to several launched resolvers.$info
– this variable may contain information accumulated after calling other resolvers. The extracted and processed information during the operation of this resolver can be added to the data already contained in this variable.<aside> ➡️ The GraphQl engine maps data to the response type. All fields that did not match the requested schema will be omitted in the response but available in child resolvers. This allows, for example, to share the loaded product model from product resolver down to product field resolvers.
</aside>
$value
– contains additional query parameters.$args
– incoming request parameters are available from this variable.