|
| 1 | +import Vue, { PluginObject, PluginFunction } from 'vue'; |
| 2 | +import { DocumentNode } from 'graphql'; |
| 3 | +import { ApolloClient } from 'apollo-client'; |
| 4 | +import { WatchQueryOptions, MutationOptions, SubscriptionOptions, SubscribeToMoreOptions, ObservableQuery, NetworkStatus } from 'apollo-client' |
| 5 | +import { DataProxy } from 'apollo-cache'; |
| 6 | +import { subscribe } from 'graphql/subscription/subscribe'; |
| 7 | + |
| 8 | +// include Omit type from https://github.com/Microsoft/TypeScript/issues/12215 |
| 9 | +type Diff<T extends string, U extends string> = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T]; |
| 10 | +type Omit<T, K extends keyof T> = { [P in Diff<keyof T, K>]?: T[P] }; |
| 11 | + |
| 12 | +type VueApolloOptions = { |
| 13 | + $skip?: boolean, |
| 14 | + $skipAllQueries?: boolean, |
| 15 | + $skipAllSubscriptions?: boolean, |
| 16 | + $client?: string, |
| 17 | + $loadingKey?: string, |
| 18 | + $error?: Function |
| 19 | +} |
| 20 | + |
| 21 | +export class VueApollo implements PluginObject<{}> { |
| 22 | + [key: string]: any; |
| 23 | + install: PluginFunction<{}>; |
| 24 | + constructor (options: { defaultClient: ApolloClient<{}>, defaultOptions?: VueApolloOptions }); |
| 25 | + static install(pVue: typeof Vue, options?:{} | undefined): void; |
| 26 | +} |
| 27 | + |
| 28 | +type ApolloVueThisType<V> = V & { [key: string]: any }; |
| 29 | +type VariableFn<V> = ((this: ApolloVueThisType<V>) => Object) | Object; |
| 30 | +type ApolloVueUpdateQueryFn<V> = (this: ApolloVueThisType<V>, previousQueryResult: { [key: string]: any }, options: { |
| 31 | + error: any, |
| 32 | + subscriptionData: { data: any; }; |
| 33 | + variables?: { [key: string]: any; }; |
| 34 | +}) => Object; |
| 35 | + |
| 36 | +interface ApolloVueSubscribeToMoreOptions<V> { |
| 37 | + document: DocumentNode; |
| 38 | + variables?: VariableFn<V>; |
| 39 | + updateQuery?: ApolloVueUpdateQueryFn<V>; |
| 40 | + onError?: (error: Error) => void; |
| 41 | +} |
| 42 | + |
| 43 | +type _WatchQueryOptions = Omit<WatchQueryOptions, 'query'>; // exclude query prop because it causes type incorrectly error |
| 44 | +export interface VueApolloQueryOptions<V, R> extends _WatchQueryOptions { |
| 45 | + query: ((this: ApolloVueThisType<V>) => DocumentNode) | DocumentNode; |
| 46 | + variables?: VariableFn<V>; |
| 47 | + update?: (this: ApolloVueThisType<V>, data: R) => any; |
| 48 | + result?: (this: ApolloVueThisType<V>, data: R, loader: any, netWorkStatus: NetworkStatus) => void; |
| 49 | + error?: (this: ApolloVueThisType<V>, error: any) => void; |
| 50 | + loadingKey?: string; |
| 51 | + watchLoading?: (isLoading: boolean, countModifier: number) => void; |
| 52 | + skip?: (this: ApolloVueThisType<V>) => boolean | boolean; |
| 53 | + manual?: boolean; |
| 54 | + subscribeToMore?: ApolloVueSubscribeToMoreOptions<V> | ApolloVueSubscribeToMoreOptions<V>[]; |
| 55 | +} |
| 56 | + |
| 57 | +export interface VueApolloMutationOptions<V, R> extends MutationOptions<R> { |
| 58 | + mutation: DocumentNode; |
| 59 | + variables?: VariableFn<V>; |
| 60 | + optimisticResponse?: ((this: ApolloVueThisType<V>) => any) | Object; |
| 61 | +} |
| 62 | + |
| 63 | +export interface VueApolloSubscriptionOptions<V, R> extends SubscriptionOptions { |
| 64 | + query: DocumentNode; |
| 65 | + variables?: VariableFn<V>; |
| 66 | + result?: (this: V, data: R) => void; |
| 67 | +} |
| 68 | + |
| 69 | +type Query<V> = (key: string, options: VueApolloQueryOptions<V, any>) => void; |
| 70 | +type Mutate<V, R=any> = <R=any>(params: VueApolloMutationOptions<V, R>) => Promise<R>; |
| 71 | +type Subscribe<R=any> = <R=any>(params: SubscriptionOptions) => ObservableQuery<R>; |
| 72 | +export interface ApolloProperty<V> { |
| 73 | + [key: string]: Query<V> | Mutate<V> | Subscribe; // smart query |
| 74 | + queries: any; |
| 75 | + mutate: Mutate<V>; |
| 76 | + subscribe: Subscribe; |
| 77 | +} |
| 78 | +type QueryComponentProperty<V> = ((this: ApolloVueThisType<V>) => VueApolloQueryOptions<V, any>) | VueApolloQueryOptions<V, any> |
| 79 | +type SubscribeComponentProperty<V> = VueApolloSubscriptionOptions<V, any> | { [key: string]: VueApolloSubscriptionOptions<V, any> } |
| 80 | + |
| 81 | +export interface VueApolloComponentOption<V> extends VueApolloOptions { |
| 82 | + [key: string]: QueryComponentProperty<V> | SubscribeComponentProperty<V> | string | boolean | Function | undefined; |
| 83 | + $subscribe?: SubscribeComponentProperty<V>; |
| 84 | +} |
0 commit comments