An OrderInterceptor is a class which can be used to intercept and modify the behavior of order-related operations.
It does this by providing methods which are called whenever the contents of an order are about to get changed. These methods are able to prevent the operation from proceeding by returning a string error message.
Examples of use-cases for an OrderInterceptor include:
This is configured via the orderOptions.orderInterceptors property of
your VendureConfig.
OrderInterceptors are executed when the following mutations are called:
addItemToOrderadjustOrderLineremoveItemFromOrderAdditionally, if you are working directly with the OrderService, the following methods will trigger any registered OrderInterceptors:
addItemToOrderaddItemsToOrderadjustOrderLineadjustOrderLinesremoveItemFromOrderremoveItemsFromOrderWhen an OrderInterceptor is registered, it will be called in the order in which it was registered. If an interceptor method resolves to a string, the operation will be prevented and the string will be used as the error message.
When multiple interceptors are registered, the first interceptor to resolve to a string will prevent the operation from proceeding.
Errors returned by OrderInterceptors are surfaced to the GraphQL API as an OrderInterceptorError and can be
queried like this:
In the above example, the error message returned by the OrderInterceptor would be available in the interceptorError field.
Let's say we want to allow ProductVariants to specify the minimum or maximum amount which may be added to an order. We can define custom fields to store this information and then use this custom field value to prevent an order line from being added to the order if the quantity is below the minimum.
Example
InjectableStrategy(ctx: RequestContext, order: Order, input: WillAddItemToOrderInput) => Promise<void | string> | void | stringCalled when a new item is about to be added to the order,
as in the addItemToOrder mutation or the addItemToOrder() / addItemsToOrder() method
of the OrderService.
(ctx: RequestContext, order: Order, input: WillAdjustOrderLineInput) => Promise<void | string> | void | stringCalled when an existing order line is about to be adjusted,
as in the adjustOrderLine mutation or the adjustOrderLine() / adjustOrderLines() method
of the OrderService.
(ctx: RequestContext, order: Order, orderLine: OrderLine) => Promise<void | string> | void | stringCalled when an item is about to be removed from the order,
as in the removeItemFromOrder mutation or the removeItemFromOrder() / removeItemsFromOrder() method
of the OrderService.