1 |
|
2 |
|
3 | import ApolloClient from 'apollo-client';
|
4 |
|
5 | import {
|
6 | assign,
|
7 | } from 'lodash';
|
8 |
|
9 | import {
|
10 | GraphQLResult,
|
11 | } from 'graphql';
|
12 |
|
13 | import {
|
14 | isEqual,
|
15 | noop,
|
16 | forIn,
|
17 | } from 'lodash';
|
18 |
|
19 | export interface ApolloOptionsQueries {
|
20 | context: any;
|
21 | };
|
22 |
|
23 | export interface ApolloOptionsMutations {
|
24 | context: any;
|
25 | };
|
26 |
|
27 | export declare interface ApolloOptions {
|
28 | client: ApolloClient;
|
29 | queries?(opts: ApolloOptionsQueries): any;
|
30 | mutations?(opts: ApolloOptionsMutations): any;
|
31 | };
|
32 |
|
33 | export function Apollo({
|
34 | client,
|
35 | queries,
|
36 | mutations,
|
37 | }: ApolloOptions) {
|
38 | const { watchQuery, mutate } = client;
|
39 |
|
40 | queries = queries || noop;
|
41 | mutations = mutations || noop;
|
42 |
|
43 |
|
44 | const lastQueryVariables = {};
|
45 | const queryHandles = {};
|
46 |
|
47 | return (sourceTarget: any) => {
|
48 | const target = sourceTarget;
|
49 |
|
50 | const oldHooks = {};
|
51 | const hooks = {
|
52 | |
53 |
|
54 |
|
55 |
|
56 | ngOnInit() {
|
57 |
|
58 | handleQueries(this);
|
59 | handleMutations(this);
|
60 | },
|
61 | |
62 |
|
63 |
|
64 |
|
65 | ngDoCheck() {
|
66 |
|
67 | handleQueries(this);
|
68 | handleMutations(this);
|
69 | },
|
70 | |
71 |
|
72 |
|
73 | ngOnDestroy() {
|
74 | unsubscribe();
|
75 | },
|
76 | };
|
77 |
|
78 |
|
79 | forIn(hooks, (hook, name) => {
|
80 | wrapPrototype(name, hook);
|
81 | });
|
82 |
|
83 | function handleQueries(component: any) {
|
84 | forIn(queries(component), (options, queryName: string) => {
|
85 | if (!equalVariablesOf(queryName, options.variables)) {
|
86 | createQuery(component, queryName, options);
|
87 | }
|
88 | });
|
89 | }
|
90 |
|
91 | function handleMutations(component: any) {
|
92 | forIn(mutations(component), (method: Function, mutationName: string) => {
|
93 | createMutation(component, mutationName, method);
|
94 | });
|
95 | }
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | function createQuery(component: any, queryName: string, options) {
|
105 |
|
106 | lastQueryVariables[queryName] = options.variables;
|
107 |
|
108 | subscribe(component, queryName, watchQuery(options));
|
109 | }
|
110 |
|
111 | |
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function createMutation(component: any, mutationName: string, method: Function) {
|
120 |
|
121 | component[mutationName] = (...args): Promise<GraphQLResult> => {
|
122 | const { mutation, variables } = method.apply(client, args);
|
123 |
|
124 | return mutate({ mutation, variables });
|
125 | };
|
126 | }
|
127 |
|
128 | function subscribe(component: any, queryName: string, obs: any) {
|
129 | component[queryName] = {
|
130 | errors: null,
|
131 | loading: true,
|
132 | };
|
133 |
|
134 | const setQuery = ({ errors, data = {} }: any) => {
|
135 | component[queryName] = assign({
|
136 | errors,
|
137 | loading: false,
|
138 | unsubscribe: queryHandles[queryName].unsubscribe,
|
139 | refetch: queryHandles[queryName].refetch,
|
140 | stopPolling: queryHandles[queryName].stopPolling,
|
141 | startPolling: queryHandles[queryName].startPolling,
|
142 | }, data);
|
143 | };
|
144 |
|
145 |
|
146 | unsubscribe(queryName);
|
147 |
|
148 | queryHandles[queryName] = obs.subscribe({
|
149 | next: setQuery,
|
150 | error(errors) {
|
151 | setQuery({ errors });
|
152 | },
|
153 | });
|
154 | };
|
155 |
|
156 | function unsubscribe(queryName?: string) {
|
157 | if (queryHandles) {
|
158 | if (queryName) {
|
159 |
|
160 | if (queryHandles[queryName]) {
|
161 | queryHandles[queryName].unsubscribe();
|
162 | }
|
163 | } else {
|
164 |
|
165 | for (const key in queryHandles) {
|
166 | if (!queryHandles.hasOwnProperty(key)) {
|
167 | continue;
|
168 | }
|
169 |
|
170 | queryHandles[key].unsubscribe();
|
171 | }
|
172 | }
|
173 | }
|
174 | }
|
175 |
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | function equalVariablesOf(queryName: string, variables: any): boolean {
|
184 | return lastQueryVariables.hasOwnProperty(queryName) && isEqual(lastQueryVariables[queryName], variables);
|
185 | }
|
186 |
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | function wrapPrototype(name: string, func: Function) {
|
195 | oldHooks[name] = sourceTarget.prototype[name];
|
196 |
|
197 | target.prototype[name] = function(...args) {
|
198 |
|
199 | func.apply(this, args);
|
200 |
|
201 |
|
202 | if (oldHooks[name]) {
|
203 | oldHooks[name].apply(this, args);
|
204 | }
|
205 | };
|
206 | }
|
207 |
|
208 |
|
209 | return target;
|
210 | };
|
211 | }
|