-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathhelpers.ts
255 lines (227 loc) · 6.61 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import type {
ApolloError,
NetworkStatus,
ObservableQuery,
WatchQueryOptions,
} from "@apollo/client";
import type { Cache } from "@apollo/client/cache";
import type {
DocumentNode,
OperationDefinitionNode,
FragmentDefinitionNode,
} from "graphql/language";
import type { QueryData, Variables } from "../../application/types/scalars";
import { getPrivateAccess } from "../../privateAccess";
import { getOperationName } from "@apollo/client/utilities";
import { pick } from "../../application/utilities/pick";
import type { GraphQLFormattedError } from "graphql";
export type QueryOptions = Pick<
WatchQueryOptions,
| "context"
| "fetchPolicy"
| "errorPolicy"
| "pollInterval"
| "partialRefetch"
| "canonizeResults"
| "returnPartialData"
| "refetchWritePolicy"
| "notifyOnNetworkStatusChange"
> & { nextFetchPolicy?: string };
export interface SerializedApolloError extends Pick<ApolloError, "message"> {
name: "ApolloError";
clientErrors: string[];
networkError?: SerializedError;
graphQLErrors: ReadonlyArray<GraphQLFormattedError>;
protocolErrors: string[];
}
export interface SerializedError {
message: string;
name: string;
stack?: string;
}
export type QueryDetails = {
id: string;
document: DocumentNode;
variables?: Variables;
cachedData?: QueryData; // Not a member of the actual Apollo Client QueryInfo type
options?: QueryOptions;
networkStatus: NetworkStatus;
error?: SerializedApolloError;
pollInterval?: number;
};
export type MutationDetails = {
document: DocumentNode;
variables?: Variables;
loading: boolean;
error: SerializedApolloError | SerializedError | null;
};
// Transform the map of observable queries into a list of QueryInfo objects usable by DevTools
export function getQueries(
observableQueries: Map<string, ObservableQuery>
): QueryDetails[] {
const queries: QueryDetails[] = [];
if (observableQueries) {
observableQueries.forEach((oc, queryId) => {
const observableQuery = getPrivateAccess(oc);
const { document, variables } = observableQuery.queryInfo;
const diff = observableQuery.queryInfo.getDiff();
if (!document) return;
const name = getOperationName(document);
if (name === "IntrospectionQuery") {
return;
}
const { pollingInfo } = observableQuery;
const { networkStatus, error } = observableQuery.getCurrentResult(false);
queries.push({
id: queryId,
document,
variables,
cachedData: diff.result,
options: getQueryOptions(oc),
networkStatus,
error: error ? serializeApolloError(error) : undefined,
pollInterval: pollingInfo && Math.floor(pollingInfo.interval),
});
});
}
return queries;
}
function serializeApolloError(error: ApolloError): SerializedApolloError {
return {
clientErrors: error.clientErrors.map((e) => e.message),
name: "ApolloError",
networkError: error.networkError
? serializeError(error.networkError)
: undefined,
message: error.message,
graphQLErrors: error.graphQLErrors,
protocolErrors: error.protocolErrors.map((e) => e.message),
};
}
function getQueryOptions(observableQuery: ObservableQuery) {
const { options } = observableQuery;
const queryOptions = {
...pick(options, [
"context",
"pollInterval",
"partialRefetch",
"canonizeResults",
"returnPartialData",
"refetchWritePolicy",
"notifyOnNetworkStatusChange",
"fetchPolicy",
"errorPolicy",
]),
nextFetchPolicy:
typeof options.nextFetchPolicy === "function"
? "<function>"
: options.nextFetchPolicy,
};
if (queryOptions.nextFetchPolicy == null) {
delete queryOptions.nextFetchPolicy;
}
if (queryOptions.context) {
queryOptions.context = JSON.parse(
JSON.stringify(queryOptions.context, (_key, value) => {
if (typeof value === "function") {
return `<function>`;
}
return value;
})
) as Record<string, unknown>;
}
return queryOptions;
}
// Version of getQueries compatible with Apollo Client versions < 3.4.0
export function getQueriesLegacy(
queryMap: Map<
string,
{
document: DocumentNode;
variables: Variables;
diff: Cache.DiffResult<any>;
networkStatus?: NetworkStatus;
}
>
): QueryDetails[] {
let queries: QueryDetails[] = [];
if (queryMap) {
queries = [...queryMap.entries()].map(
([queryId, { document, variables, diff, networkStatus }]) => ({
id: queryId,
document,
variables,
cachedData: diff?.result,
networkStatus: networkStatus ?? 1,
})
);
}
return queries;
}
interface MutationStoreValue {
mutation: DocumentNode;
variables: Variables;
loading: boolean;
error: Error | null;
}
export function getMutations(
mutationsObj: Record<string, MutationStoreValue>
): MutationDetails[] {
const keys = Object.keys(mutationsObj);
if (keys.length === 0) {
return [];
}
return keys.map((key) => {
const { mutation, variables, loading, error } = mutationsObj[key];
return {
document: mutation,
variables,
loading,
error: getSerializedMutationError(error),
};
});
}
function serializeError(error: Error | string) {
return typeof error !== "object"
? { message: String(error), name: typeof error }
: { message: error.message, name: error.name, stack: error.stack };
}
function isApolloError(error: Error): error is ApolloError {
return error.name === "ApolloError";
}
function getSerializedMutationError(error: Error | null) {
if (!error) {
return null;
}
return isApolloError(error)
? serializeApolloError(error)
: serializeError(error);
}
export function getMainDefinition(
queryDoc: DocumentNode
): OperationDefinitionNode | FragmentDefinitionNode {
let fragmentDefinition;
for (const definition of queryDoc.definitions) {
if (definition.kind === "OperationDefinition") {
const operation = (definition as OperationDefinitionNode).operation;
if (
operation === "query" ||
operation === "mutation" ||
operation === "subscription"
) {
return definition as OperationDefinitionNode;
}
}
if (definition.kind === "FragmentDefinition" && !fragmentDefinition) {
// we do this because we want to allow multiple fragment definitions
// to precede an operation definition.
fragmentDefinition = definition as FragmentDefinitionNode;
}
}
if (fragmentDefinition) {
return fragmentDefinition;
}
throw new Error(
"Expected a parsed GraphQL query with a query, mutation, subscription, or a fragment."
);
}