Customize GraphQL Queries in Commerce React Components
Introduction
As of v14.7.1, the Commerce React Components allow applications to customize the internal GraphQL queries and variables. When using a custom hook provided by one of the Commerce React Components, application components may provide an optional CustomQueryOptionsProcessor implementation in the input properties. If provided, the Commerce React Component will invoke the CustomQueryOptionsProcessor implementation in order to pre-process the internal GraphQL query and variables before making any GraphQL queries. If the CustomQueryOptionsProcessor implementation updates the GraphQL query document or variables, then the updated GraphQL query or variables will be used in the Commerce React Component instead of the default ones.
CustomQueryOptionsProcessor Interface
Since v14.7.1, most of the Commerce React Components take an optional CustomQueryOptionsProcessor implementation object in their input properties as shown below:
For example, the useProductGridSearch hook in the Commerce React Components accepts the input properties like the following, and the input properties now contains an optional customQueryOptionsProcessor property of type CustomQueryOptionsProcessor<TData, TVariables>, where TData generic type is specified as Items and TVariables generic type as ItemsVariables for the product search feature.
As shown in the example above, most of Commerce React Components support the optional customQueryOptionsProcessor input property which can be supplied by the application. If provided, the Commerce React Component will do the following:
- The Commerce React Components create an CustomQueryOptions object containing the internal GraphQL query document object (query property) and the internal GraphQL query options containing variables (options property).
- Now the customQueryOptionsProcessor has a chance to look up the CustomQueryOptions input object containing the internal, original GraphQL query document object and the internal GraphQL query options containing variables.
- If the application doesn't need to customize anything, then it may just return the CustomQueryOptions input object without any updates.
- Or if the application wants to update the GraphQL query document or query options/variables, then it may return an updated CustomQueryOptions object.
- The Commerce React Components receive the CustomQueryOptions object returned by the customQueryOptionsProcessor and uses the GraphQL queries and options/variables returned by the customQueryOptionsProcessor afterward.
Therefore, an application may customize the internal GraphQL query documents or the internal GraphQL query variables if necessary by implementing a CustomQueryOptionsProcessor and providing it as the optional input property to the hook of the Commerce React Component.
Example
Suppose you want to customize the GraphQL query when using the useCategory hook which is used when retrieving a commerce category.
For example, the following code will initialize a hook to query a commerce category by a category ID:
Now, suppose you want to optimize the GraphQL query in your application with fewer fields and add a new variable to the query variables. You can do that like the following example: