Просмотр исходного кода

docs: Add docs isGraphQlErrorResult function

Michael Bromley 2 лет назад
Родитель
Сommit
0763432e3e

+ 27 - 0
docs/docs/guides/developer-guide/error-handling/index.mdx

@@ -231,6 +231,33 @@ Here's how a response would look in both the success and error result cases:
 </TabItem>
 </Tabs>
 
+### Handling ErrorResults in plugin code
+
+If you are writing a plugin which deals with internal Vendure service methods that may return ErrorResults,
+then you can use the `isGraphQlErrorResult()` function to check whether the result is an ErrorResult:
+
+```ts
+import { Injectable} from '@nestjs/common';
+import { isGraphQlErrorResult, Order, OrderService, OrderState, RequestContext } from '@vendure/core';
+
+@Injectable()
+export class MyService {
+
+    constructor(private orderService: OrderService) {}
+
+    async myMethod(ctx: RequestContext, order: Order, newState: OrderState) {
+        const transitionResult = await this.orderService.transitionToState(ctx, order.id, newState);
+        if (isGraphQlErrorResult(transitionResult)) {
+            // The transition failed with an ErrorResult
+            throw transitionResult;
+        } else {
+            // TypeScript will correctly infer the type of `transitionResult` to be `Order`
+            return transitionResult;
+        }
+    }
+}
+```
+
 ### Handling ErrorResults in client code
 
 Because we know all possible ErrorResult that may occur for a given mutation, we can handle them in an exhaustive manner. In other

+ 23 - 1
packages/core/src/common/error/error-result.ts

@@ -37,6 +37,9 @@ export type JustErrorResults<T extends GraphQLErrorResult | U, U = any> = Exclud
  * type UpdateOrderItemsResult = Order | OrderModificationError | OrderLimitError | NegativeQuantityError;
  * type T1 = ErrorResultUnion<UpdateOrderItemsResult, VendureEntityOrder>;
  * // T1 = VendureEntityOrder | OrderModificationError | OrderLimitError | NegativeQuantityError;
+ * ```
+ *
+ * @docsCategory errors
  */
 export type ErrorResultUnion<T extends GraphQLErrorResult | U, E extends VendureEntity, U = any> =
     | JustErrorResults<T>
@@ -44,7 +47,26 @@ export type ErrorResultUnion<T extends GraphQLErrorResult | U, E extends Vendure
 
 /**
  * @description
- * Returns true if the ErrorResultUnion is actually an ErrorResult type.
+ * Returns true if the {@link ErrorResultUnion} is actually an ErrorResult type. This is useful when dealing with
+ * certain internal service method that return an ErrorResultUnion.
+ *
+ * @example
+ * ```ts
+ * import { isGraphQlErrorResult } from '\@vendure/core';
+ *
+ * // ...
+ *
+ * const transitionResult = await this.orderService.transitionToState(ctx, order.id, newState);
+ * if (isGraphQlErrorResult(transitionResult)) {
+ *     // The transition failed with an ErrorResult
+ *     throw transitionResult;
+ * } else {
+ *     // TypeScript will correctly infer the type of `transitionResult` to be `Order`
+ *     return transitionResult;
+ * }
+ * ```
+ *
+ * @docsCategory errors
  */
 export function isGraphQlErrorResult<T extends GraphQLErrorResult | U, U = any>(
     input: T,