|
1 | | -import { ApolloError as BaseApolloError } from 'apollo-client'; |
2 | | -import { Vue } from 'vue/types/vue'; |
3 | | -import { GraphQLError as BaseGraphQLError } from 'graphql'; |
| 1 | +import { VueApolloSubscribeToMoreOptions } from 'vue-apollo/types/options'; |
4 | 2 |
|
5 | | -export type ApolloOperationContext<TAttrs = Record<string, any>> = TAttrs; |
6 | | - |
7 | | -export enum ApolloErrorType { |
8 | | - NETWORK_ERROR = 'NETWORK_ERROR', |
9 | | - SERVER_ERROR = 'SERVER_ERROR', |
10 | | - UNAUTHORIZED_ERROR = 'UNAUTHORIZED_ERROR', |
11 | | - BAD_USER_INPUT = 'BAD_USER_INPUT', |
12 | | - INPUT_VALIDATION_ERROR = 'INPUT_VALIDATION_ERROR', |
13 | | -} |
| 3 | +type OverrideThis<F, T> = F extends (...args: infer A) => infer B ? (this: T, ...args: A) => B : F; |
14 | 4 |
|
15 | | -// Upstream type declares networkError as Error, but it can contain additional properties, we override to add these |
16 | | -export type ApolloError = BaseApolloError & { |
17 | | - networkError: ApolloNetworkError | null; |
18 | | - graphQLErrors: GraphQLError[]; |
| 5 | +export type OverrideAllThis<O, T> = { |
| 6 | + [key in keyof O]: OverrideThis<O[key], T>; |
19 | 7 | }; |
20 | 8 |
|
21 | | -export interface ApolloNetworkError extends Error { |
22 | | - statusCode?: number; |
23 | | - response?: any; |
24 | | - result?: { |
25 | | - errors: GraphQLError[]; |
26 | | - }; |
27 | | -} |
28 | | - |
29 | | -export type GraphQLError = Omit<BaseGraphQLError, 'message'> & { |
30 | | - message: string | Record<string, any>; |
| 9 | +export type SubscribeToMoreOptionsPatched<TComponent, TResult, TVariables> = OverrideAllThis< |
| 10 | + Omit<VueApolloSubscribeToMoreOptions<TResult, TVariables>, 'updateQuery' | 'variables'>, |
| 11 | + TComponent |
| 12 | +> & { |
| 13 | + variables?: (this: TComponent) => any; |
| 14 | + updateQuery?: UpdateQueryFn<TComponent, TResult, any, any>; // TODO: How should we pass subscript data & variables types? |
31 | 15 | }; |
32 | 16 |
|
33 | | -export type ProcessedApolloError = |
34 | | - | NetworkError |
35 | | - | ServerError |
36 | | - | UnauthorizedError |
37 | | - | UserInputError |
38 | | - | InputValidationError; |
39 | | - |
40 | | -export interface NetworkError { |
41 | | - type: ApolloErrorType.NETWORK_ERROR; |
42 | | - error: Error; |
43 | | - message: string; |
44 | | - statusCode?: number; |
45 | | -} |
46 | | - |
47 | | -export interface ServerError { |
48 | | - type: ApolloErrorType.SERVER_ERROR; |
49 | | - error: Error; |
50 | | - path?: readonly (string | number)[]; |
51 | | - message: string; |
52 | | -} |
53 | | - |
54 | | -export interface UnauthorizedError { |
55 | | - type: ApolloErrorType.UNAUTHORIZED_ERROR; |
56 | | - error: Error; |
57 | | - path?: readonly (string | number)[]; |
58 | | - message: string; |
59 | | -} |
| 17 | +type UpdateQueryFn<TComponent = any, TResult = any, TSubscriptionVariables = any, TSubscriptionData = any> = ( |
| 18 | + this: TComponent, |
| 19 | + previousQueryResult: TResult, |
| 20 | + options: { |
| 21 | + subscriptionData: { |
| 22 | + data: TSubscriptionData; |
| 23 | + }; |
| 24 | + variables?: TSubscriptionVariables; |
| 25 | + }, |
| 26 | +) => TResult; |
60 | 27 |
|
61 | | -export interface UserInputError { |
62 | | - type: ApolloErrorType.BAD_USER_INPUT; |
63 | | - error: Error; |
64 | | - path?: readonly (string | number)[]; |
65 | | - message: string; |
66 | | -} |
67 | | - |
68 | | -export interface InputValidationError { |
69 | | - type: ApolloErrorType.INPUT_VALIDATION_ERROR; |
70 | | - error: Error; |
71 | | - path?: readonly (string | number)[]; |
72 | | - message: string; |
73 | | - invalidArgs: string[]; |
74 | | - violations: ValidationRuleViolation[]; |
75 | | -} |
76 | | - |
77 | | -export interface ValidationRuleViolation { |
78 | | - path: string[]; |
79 | | - message: string; |
80 | | - value?: any; |
81 | | -} |
82 | | - |
83 | | -export interface ApolloErrorHandlerResult { |
84 | | - processedErrors: ProcessedApolloError[]; |
85 | | - validationRuleViolations?: ValidationRuleViolation[]; |
86 | | -} |
87 | | - |
88 | | -export type ApolloOperationErrorHandlerFunction< |
89 | | - TError = BaseApolloError, |
90 | | - TApp extends Vue = Vue, |
91 | | - TContext = ApolloOperationContext, |
92 | | -> = (error: TError, app: TApp, context?: TContext) => ApolloErrorHandlerResult; |
| 28 | +export type ApolloOperationContext<TAttrs = Record<string, any>> = TAttrs; |
0 commit comments