Skip to content

Commit ae2e2a3

Browse files
author
Guillaume Chau
committed
refactor(types): rewritten most types
1 parent 268d4a5 commit ae2e2a3

File tree

8 files changed

+339
-158
lines changed

8 files changed

+339
-158
lines changed

src/apollo-provider.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ export class ApolloProvider {
99
this.watchLoading = options.watchLoading
1010
this.errorHandler = options.errorHandler
1111
this.prefetch = options.prefetch
12-
13-
this.prefetchQueries = []
1412
}
1513

1614
provide (key = '$apolloProvider') {

src/dollar-apollo.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ export class DollarApollo {
1919
return this.vm.$apolloProvider
2020
}
2121

22-
query (options) {
23-
return this.getClient(options).query(options)
24-
}
25-
2622
getClient (options = null) {
2723
if (!options || !options.client) {
2824
if (typeof this.client === 'object') {
@@ -48,6 +44,10 @@ export class DollarApollo {
4844
return client
4945
}
5046

47+
query (options) {
48+
return this.getClient(options).query(options)
49+
}
50+
5151
watchQuery (options) {
5252
const observable = this.getClient(options).watchQuery(options)
5353
const _subscribe = observable.subscribe.bind(observable)

types/apollo-provider.d.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
/* eslint no-unused-vars: 0 */
22

33
import Vue, { AsyncComponent } from 'vue'
4-
import { VueApolloComponentOption } from './options'
54
import { ApolloClient } from 'apollo-client';
6-
import { WatchLoading, ErrorHandler, VueApolloOptions } from './options'
5+
import {
6+
VueApolloComponentOptions,
7+
WatchLoading,
8+
ErrorHandler
9+
} from './options'
710

8-
export type VueApolloComponent<V extends Vue = Vue> = VueApolloComponentOption<V> | typeof Vue | AsyncComponent;
11+
export type VueApolloComponent<V extends Vue = Vue> = VueApolloComponentOptions<V> | typeof Vue | AsyncComponent;
912

1013
export class ApolloProvider<TCacheShape=any> {
1114
provide: (key?: string) => this
1215
constructor (options: {
1316
defaultClient: ApolloClient<TCacheShape>,
14-
defaultOptions?: VueApolloOptions<any>,
17+
defaultOptions?: VueApolloComponentOptions<Vue>,
1518
clients?: { [key: string]: ApolloClient<TCacheShape> },
16-
watchLoading?: WatchLoading<any>,
17-
errorHandler?: ErrorHandler<any>
19+
watchLoading?: WatchLoading,
20+
errorHandler?: ErrorHandler,
21+
prefetch?: boolean
1822
})
1923
clients: { [key: string]: ApolloClient<TCacheShape> }
2024
defaultClient: ApolloClient<TCacheShape>
21-
prefetchQueries: any
2225
}

types/options.d.ts

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,86 @@
1+
import Vue from 'vue'
12
import {
23
WatchQueryOptions,
4+
OperationVariables,
35
MutationOptions,
46
SubscriptionOptions,
57
SubscribeToMoreOptions,
68
ObservableQuery,
79
NetworkStatus,
810
ApolloQueryResult,
11+
ApolloError
912
} from 'apollo-client';
1013
import { FetchResult } from 'apollo-link';
1114
import { ServerError, ServerParseError } from 'apollo-link-http-common';
1215
import { DocumentNode, GraphQLError } from 'graphql';
1316

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 */
1818

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
3230
}
3331

34-
export type WatchLoading<V> = (this: ApolloVueThisType<V>, isLoading: boolean, countModifier: number) => void
32+
export type VueApolloComponentSpecialOptions<Instance> =
33+
Partial<AllVueApolloComponentSpecialOptions<Instance>>
3534

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>]
3939
}
40-
export type ErrorHandler<V> = (this: ApolloVueThisType<V>, error: ErrorResponse) => void
4140

42-
type _WatchQueryOptions = Omit<WatchQueryOptions, 'query'>; // exclude query prop because it causes type incorrectly error
41+
/* Special component options */
4342

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
6145

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 */
6847

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>)[]
7472
}
7573

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 */
7875

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;
8978
}
9079

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
9482
}
83+
84+
export type VueApolloSubscriptionProperty =
85+
VueApolloSubscriptionDefinition |
86+
{ [key: string]: VueApolloSubscriptionDefinition }

0 commit comments

Comments
 (0)