-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathindex.js
180 lines (140 loc) · 5.13 KB
/
index.js
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
var parser = require('graphql/language/parser');
var parse = parser.parse;
// Strip insignificant whitespace
// Note that this could do a lot more, such as reorder fields etc.
function normalize(string) {
return string.replace(/[\s,]+/g, ' ').trim();
}
// A map docString -> graphql document
var docCache = {};
// A map fragmentName -> [normalized source]
var fragmentSourceMap = {};
function cacheKeyFromLoc(loc) {
return normalize(loc.source.body.substring(loc.start, loc.end));
}
// For testing.
function resetCaches() {
docCache = {};
fragmentSourceMap = {};
}
// Take a unstripped parsed document (query/mutation or even fragment), and
// check all fragment definitions, checking for name->source uniqueness.
// We also want to make sure only unique fragments exist in the document.
var printFragmentWarnings = true;
function processFragments(ast) {
var astFragmentMap = {};
var definitions = [];
for (var i = 0; i < ast.definitions.length; i++) {
var fragmentDefinition = ast.definitions[i];
if (fragmentDefinition.kind === 'FragmentDefinition') {
var fragmentName = fragmentDefinition.name.value;
var sourceKey = cacheKeyFromLoc(fragmentDefinition.loc);
// We know something about this fragment
if (fragmentSourceMap.hasOwnProperty(fragmentName) && !fragmentSourceMap[fragmentName][sourceKey]) {
// this is a problem because the app developer is trying to register another fragment with
// the same name as one previously registered. So, we tell them about it.
if (printFragmentWarnings) {
console.warn("Warning: fragment with name " + fragmentName + " already exists.\n"
+ "graphql-tag enforces all fragment names across your application to be unique; read more about\n"
+ "this in the docs: http://dev.apollodata.com/core/fragments.html#unique-names");
}
fragmentSourceMap[fragmentName][sourceKey] = true;
} else if (!fragmentSourceMap.hasOwnProperty(fragmentName)) {
fragmentSourceMap[fragmentName] = {};
fragmentSourceMap[fragmentName][sourceKey] = true;
}
if (!astFragmentMap[sourceKey]) {
astFragmentMap[sourceKey] = true;
definitions.push(fragmentDefinition);
}
} else {
definitions.push(fragmentDefinition);
}
}
ast.definitions = definitions;
return ast;
}
function disableFragmentWarnings() {
printFragmentWarnings = false;
}
function stripLoc(doc, removeLocAtThisLevel) {
var docType = Object.prototype.toString.call(doc);
if (docType === '[object Array]') {
return doc.map(function (d) {
return stripLoc(d, removeLocAtThisLevel);
});
}
if (docType !== '[object Object]') {
throw new Error('Unexpected input.');
}
// We don't want to remove the root loc field so we can use it
// for fragment substitution (see below)
if (removeLocAtThisLevel && doc.loc) {
delete doc.loc;
}
// https://github.com/apollographql/graphql-tag/issues/40
if (doc.loc) {
delete doc.loc.startToken;
delete doc.loc.endToken;
}
var keys = Object.keys(doc);
var key;
var value;
var valueType;
for (key in keys) {
if (keys.hasOwnProperty(key)) {
value = doc[keys[key]];
valueType = Object.prototype.toString.call(value);
if (valueType === '[object Object]' || valueType === '[object Array]') {
doc[keys[key]] = stripLoc(value, true);
}
}
}
return doc;
}
var experimentalFragmentVariables = false;
function parseDocument(doc) {
var cacheKey = normalize(doc);
if (docCache[cacheKey]) {
return docCache[cacheKey];
}
var parsed = parse(doc, { experimentalFragmentVariables: experimentalFragmentVariables });
if (!parsed || parsed.kind !== 'Document') {
throw new Error('Not a valid GraphQL document.');
}
// check that all "new" fragments inside the documents are consistent with
// existing fragments of the same name
parsed = processFragments(parsed);
parsed = stripLoc(parsed, false);
docCache[cacheKey] = parsed;
return parsed;
}
function enableExperimentalFragmentVariables() {
experimentalFragmentVariables = true;
}
function disableExperimentalFragmentVariables() {
experimentalFragmentVariables = false;
}
// XXX This should eventually disallow arbitrary string interpolation, like Relay does
function gql(/* arguments */) {
var args = Array.prototype.slice.call(arguments);
var literals = args[0];
// We always get literals[0] and then matching post literals for each arg given
var result = (typeof(literals) === "string") ? literals : literals[0];
for (var i = 1; i < args.length; i++) {
if (args[i] && args[i].kind && args[i].kind === 'Document') {
result += args[i].loc.source.body;
} else {
result += args[i];
}
result += literals[i];
}
return parseDocument(result);
}
// Support typescript, which isn't as nice as Babel about default exports
gql.default = gql;
gql.resetCaches = resetCaches;
gql.disableFragmentWarnings = disableFragmentWarnings;
gql.enableExperimentalFragmentVariables = enableExperimentalFragmentVariables;
gql.disableExperimentalFragmentVariables = disableExperimentalFragmentVariables;
module.exports = gql;