-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathindex.ts
172 lines (150 loc) · 4.33 KB
/
index.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
import { invariant } from "../../utilities/globals/index.js";
import type {
DocumentNode,
DefinitionNode,
VariableDefinitionNode,
OperationDefinitionNode,
} from "graphql";
import {
AutoCleanedWeakCache,
cacheSizes,
defaultCacheSizes,
} from "../../utilities/index.js";
import { registerGlobalCache } from "../../utilities/caching/getMemoryInternals.js";
export enum DocumentType {
Query,
Mutation,
Subscription,
}
export interface IDocumentDefinition {
type: DocumentType;
name: string;
variables: ReadonlyArray<VariableDefinitionNode>;
}
let cache:
| undefined
| AutoCleanedWeakCache<
DocumentNode,
{
name: string;
type: DocumentType;
variables: readonly VariableDefinitionNode[];
}
>;
export function operationName(type: DocumentType) {
let name;
switch (type) {
case DocumentType.Query:
name = "Query";
break;
case DocumentType.Mutation:
name = "Mutation";
break;
case DocumentType.Subscription:
name = "Subscription";
break;
}
return name;
}
// This parser is mostly used to safety check incoming documents.
export function parser(document: DocumentNode): IDocumentDefinition {
if (!cache) {
cache = new AutoCleanedWeakCache(
cacheSizes.parser || defaultCacheSizes.parser
);
}
const cached = cache.get(document);
if (cached) return cached;
let variables, type, name;
invariant(
!!document && !!document.kind,
`Argument of %s passed to parser was not a valid GraphQL ` +
`DocumentNode. You may need to use 'graphql-tag' or another method ` +
`to convert your operation into a document`,
document
);
const fragments: DefinitionNode[] = [];
const queries: DefinitionNode[] = [];
const mutations: DefinitionNode[] = [];
const subscriptions: DefinitionNode[] = [];
for (const x of document.definitions) {
if (x.kind === "FragmentDefinition") {
fragments.push(x);
continue;
}
if (x.kind === "OperationDefinition") {
switch (x.operation) {
case "query":
queries.push(x);
break;
case "mutation":
mutations.push(x);
break;
case "subscription":
subscriptions.push(x);
break;
}
}
}
invariant(
!fragments.length ||
queries.length ||
mutations.length ||
subscriptions.length,
`Passing only a fragment to 'graphql' is not yet supported. ` +
`You must include a query, subscription or mutation as well`
);
invariant(
queries.length + mutations.length + subscriptions.length <= 1,
`react-apollo only supports a query, subscription, or a mutation per HOC. ` +
`%s had %s queries, %s ` +
`subscriptions and %s mutations. ` +
`You can use 'compose' to join multiple operation types to a component`,
document,
queries.length,
subscriptions.length,
mutations.length
);
type = queries.length ? DocumentType.Query : DocumentType.Mutation;
if (!queries.length && !mutations.length) type = DocumentType.Subscription;
const definitions =
queries.length ? queries
: mutations.length ? mutations
: subscriptions;
invariant(
definitions.length === 1,
`react-apollo only supports one definition per HOC. %s had ` +
`%s definitions. ` +
`You can use 'compose' to join multiple operation types to a component`,
document,
definitions.length
);
const definition = definitions[0] as OperationDefinitionNode;
variables = definition.variableDefinitions || [];
if (definition.name && definition.name.kind === "Name") {
name = definition.name.value;
} else {
name = "data"; // fallback to using data if no name
}
const payload = { name, type, variables };
cache.set(document, payload);
return payload;
}
parser.resetCache = () => {
cache = undefined;
};
if (__DEV__) {
registerGlobalCache("parser", () => (cache ? cache.size : 0));
}
export function verifyDocumentType(document: DocumentNode, type: DocumentType) {
const operation = parser(document);
const requiredOperationName = operationName(type);
const usedOperationName = operationName(operation.type);
invariant(
operation.type === type,
`Running a %s requires a graphql ` + `%s, but a %s was used instead.`,
requiredOperationName,
requiredOperationName,
usedOperationName
);
}