|
| 1 | +import Vue from 'vue' |
1 | 2 | import { |
2 | 3 | WatchQueryOptions, |
| 4 | + OperationVariables, |
3 | 5 | MutationOptions, |
4 | 6 | SubscriptionOptions, |
5 | 7 | SubscribeToMoreOptions, |
6 | 8 | ObservableQuery, |
7 | 9 | NetworkStatus, |
8 | 10 | ApolloQueryResult, |
| 11 | + ApolloError |
9 | 12 | } from 'apollo-client'; |
10 | 13 | import { FetchResult } from 'apollo-link'; |
11 | 14 | import { ServerError, ServerParseError } from 'apollo-link-http-common'; |
12 | 15 | import { DocumentNode, GraphQLError } from 'graphql'; |
13 | 16 |
|
14 | | -// include Omit type from https://github.com/Microsoft/TypeScript/issues/12215 |
15 | | -type Property = string | number | symbol; |
16 | | -type Diff<T extends Property, U extends Property> = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T]; |
17 | | -type Omit<T, K extends keyof T> = { [P in Diff<keyof T, K>]?: T[P] }; |
| 17 | +/* Component options */ |
18 | 18 |
|
19 | | -type ApolloVueThisType<V> = V & { [key: string]: any }; |
20 | | -type VariableFn<V> = ((this: ApolloVueThisType<V>) => Object) | Object; |
21 | | -type ApolloVueUpdateQueryFn<V> = (this: ApolloVueThisType<V>, previousQueryResult: { [key: string]: any }, options: { |
22 | | - error: any, |
23 | | - subscriptionData: { data: any; }; |
24 | | - variables?: { [key: string]: any; }; |
25 | | -}) => Object; |
26 | | - |
27 | | -interface ApolloVueSubscribeToMoreOptions<V> { |
28 | | - document: DocumentNode; |
29 | | - variables?: VariableFn<V>; |
30 | | - updateQuery?: ApolloVueUpdateQueryFn<V>; |
31 | | - onError?: (error: Error) => void; |
| 19 | +export interface AllVueApolloComponentSpecialOptions<Instance> { |
| 20 | + $skip: boolean |
| 21 | + $skipAllQueries: boolean |
| 22 | + $skipAllSubscriptions: boolean |
| 23 | + $deep: boolean |
| 24 | + $client: string |
| 25 | + $loadingKey: string |
| 26 | + $watchLoading: WatchLoading |
| 27 | + $error: ErrorHandler |
| 28 | + $query: Partial<VueApolloQueryDefinition<Instance>> |
| 29 | + $subscribe: VueApolloSubscriptionProperty |
32 | 30 | } |
33 | 31 |
|
34 | | -export type WatchLoading<V> = (this: ApolloVueThisType<V>, isLoading: boolean, countModifier: number) => void |
| 32 | +export type VueApolloComponentSpecialOptions<Instance> = |
| 33 | + Partial<AllVueApolloComponentSpecialOptions<Instance>> |
35 | 34 |
|
36 | | -export interface ErrorResponse { |
37 | | - graphQLErrors?: ReadonlyArray<GraphQLError>; |
38 | | - networkError?: Error | ServerError | ServerParseError; |
| 35 | +export interface VueApolloComponentOptions<Instance> |
| 36 | + extends VueApolloComponentSpecialOptions<Instance> { |
| 37 | + [key: string] : VueApolloQueryProperty<Instance> | |
| 38 | + VueApolloComponentSpecialOptions<Instance>[keyof VueApolloComponentSpecialOptions<Instance>] |
39 | 39 | } |
40 | | -export type ErrorHandler<V> = (this: ApolloVueThisType<V>, error: ErrorResponse) => void |
41 | 40 |
|
42 | | -type _WatchQueryOptions = Omit<WatchQueryOptions, 'query'>; // exclude query prop because it causes type incorrectly error |
| 41 | +/* Special component options */ |
43 | 42 |
|
44 | | -interface ExtendableVueApolloQueryOptions<V, R> extends _WatchQueryOptions { |
45 | | - update?: (this: ApolloVueThisType<V>, data: R) => any; |
46 | | - result?: (this: ApolloVueThisType<V>, data: ApolloQueryResult<R>, loader: any, netWorkStatus: NetworkStatus) => void; |
47 | | - error?: ErrorHandler<V>; |
48 | | - loadingKey?: string; |
49 | | - watchLoading?: WatchLoading<V>; |
50 | | - skip?: ((this: ApolloVueThisType<V>) => boolean) | boolean; |
51 | | - manual?: boolean; |
52 | | - subscribeToMore?: ApolloVueSubscribeToMoreOptions<V> | ApolloVueSubscribeToMoreOptions<V>[]; |
53 | | - prefetch?: ((context: any) => any) | boolean; |
54 | | - deep?: boolean; |
55 | | -} |
56 | | -export interface VueApolloQueryOptions<V, R> extends ExtendableVueApolloQueryOptions<V, R> { |
57 | | - query: ((this: ApolloVueThisType<V>) => DocumentNode) | DocumentNode; |
58 | | - variables?: VariableFn<V>; |
59 | | - client?: String |
60 | | -} |
| 43 | +export type WatchLoading = (isLoading: boolean, countModifier: number) => void |
| 44 | +export type ErrorHandler = (error: ApolloError) => void |
61 | 45 |
|
62 | | -export interface VueApolloMutationOptions<V, R> extends MutationOptions<R> { |
63 | | - mutation: DocumentNode; |
64 | | - variables?: VariableFn<V>; |
65 | | - optimisticResponse?: ((this: ApolloVueThisType<V>) => R) | R; |
66 | | - client?: String |
67 | | -} |
| 46 | +/* Query */ |
68 | 47 |
|
69 | | -export interface VueApolloSubscriptionOptions<V, R> extends SubscriptionOptions { |
70 | | - query: DocumentNode; |
71 | | - variables?: VariableFn<V>; |
72 | | - skip?: (this: ApolloVueThisType<V>) => boolean | boolean; |
73 | | - result?: (this: V, data: FetchResult<R>) => void; |
| 48 | +type QueryVariables = (() => OperationVariables) | OperationVariables; |
| 49 | + |
| 50 | +export type VueApolloQueryProperty<Instance> = |
| 51 | + DocumentNode | |
| 52 | + VueApolloQueryDefinition<Instance> | |
| 53 | + (() => VueApolloQueryDefinition<Instance> | null) |
| 54 | + |
| 55 | +// exclude query prop because it causes type incorrectly error |
| 56 | +type WatchQueryOptionsWithoutQuery = Omit<WatchQueryOptions, 'query'>; |
| 57 | + |
| 58 | +export interface VueApolloQueryDefinition<Instance = Vue, R = any> extends WatchQueryOptionsWithoutQuery { |
| 59 | + query: DocumentNode | (() => DocumentNode | null) |
| 60 | + variables?: QueryVariables |
| 61 | + update?: (data: R) => any |
| 62 | + result?: (result: ApolloQueryResult<R>, key: string) => void |
| 63 | + error?: ErrorHandler |
| 64 | + manual?: boolean |
| 65 | + loadingKey?: string |
| 66 | + watchLoading?: WatchLoading |
| 67 | + skip?: (() => boolean) | boolean |
| 68 | + prefetch?: ((context: any) => any) | boolean |
| 69 | + client?: string |
| 70 | + deep?: boolean |
| 71 | + subscribeToMore?: VueApolloSubscribeToMoreOptions | (VueApolloSubscribeToMoreOptions & ThisType<Instance>)[] |
74 | 72 | } |
75 | 73 |
|
76 | | -type QueryComponentProperty<V> = ((this: ApolloVueThisType<V>) => VueApolloQueryOptions<V, any>) | VueApolloQueryOptions<V, any> |
77 | | -type SubscribeComponentProperty<V> = VueApolloSubscriptionOptions<V, any> | { [key: string]: VueApolloSubscriptionOptions<V, any> } |
| 74 | +/* Subscriptions */ |
78 | 75 |
|
79 | | -export type VueApolloOptions<V> = { |
80 | | - $skip?: boolean, |
81 | | - $skipAllQueries?: boolean, |
82 | | - $skipAllSubscriptions?: boolean, |
83 | | - $deep?: boolean, |
84 | | - $client?: string, |
85 | | - $loadingKey?: string, |
86 | | - $watchLoading?: WatchLoading<V>, |
87 | | - $error?: ErrorHandler<V>, |
88 | | - $query?: ExtendableVueApolloQueryOptions<V, any> |
| 76 | +interface VueApolloSubscribeToMoreOptions extends SubscribeToMoreOptions { |
| 77 | + variables?: QueryVariables; |
89 | 78 | } |
90 | 79 |
|
91 | | -export interface VueApolloComponentOption<V> extends VueApolloOptions<V> { |
92 | | - [key: string]: QueryComponentProperty<V> | SubscribeComponentProperty<V> | ExtendableVueApolloQueryOptions<V, any> | string | boolean | Function | undefined; |
93 | | - $subscribe?: SubscribeComponentProperty<V>; |
| 80 | +interface VueApolloSubscriptionDefinition extends SubscriptionOptions { |
| 81 | + variables?: QueryVariables |
94 | 82 | } |
| 83 | + |
| 84 | +export type VueApolloSubscriptionProperty = |
| 85 | + VueApolloSubscriptionDefinition | |
| 86 | + { [key: string]: VueApolloSubscriptionDefinition } |
0 commit comments