-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
148 lines (123 loc) · 4.21 KB
/
mod.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
import { parse } from './deps.ts'
import type { DefinitionNode, DocumentNode, Location } from './deps.ts'
// A map docString -> graphql document
const docCache = new Map<string, DocumentNode>()
// A map fragmentName -> [normalized source]
const fragmentSourceMap = new Map<string, Set<string>>()
let printFragmentWarnings = true
let allowLegacyFragmentVariables = false
// Strip insignificant whitespace
// Note that this could do a lot more, such as reorder fields etc.
function normalize(string: string) {
return string.replace(/[\s,]+/g, ' ').trim()
}
function cacheKeyFromLoc(loc: Location) {
return normalize(loc.source.body.substring(loc.start, loc.end))
}
// 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.
function processFragments(ast: DocumentNode) {
const seenKeys = new Set<string>()
const definitions: DefinitionNode[] = []
ast.definitions.forEach((fragmentDefinition) => {
if (fragmentDefinition.kind === 'FragmentDefinition') {
const fragmentName = fragmentDefinition.name.value
const sourceKey = cacheKeyFromLoc(fragmentDefinition.loc!)
// We know something about this fragment
let sourceKeySet = fragmentSourceMap.get(fragmentName)!
if (sourceKeySet && !sourceKeySet.has(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',
)
}
} else if (!sourceKeySet) {
fragmentSourceMap.set(fragmentName, sourceKeySet = new Set())
}
sourceKeySet.add(sourceKey)
if (!seenKeys.has(sourceKey)) {
seenKeys.add(sourceKey)
definitions.push(fragmentDefinition)
}
} else {
definitions.push(fragmentDefinition)
}
})
return {
...ast,
definitions,
}
}
function stripLoc(doc: DocumentNode) {
const workSet = new Set<DefinitionNode>(doc.definitions)
workSet.forEach((node) => {
if (node.loc) Reflect.deleteProperty(node, 'loc')
Object.keys(node).forEach((key) => {
const value = node[key as keyof DefinitionNode]
if (value && typeof value === 'object') {
workSet.add(value as unknown as DefinitionNode)
}
})
})
const loc = doc.loc
if (loc) {
Reflect.deleteProperty(loc, 'startToken');
Reflect.deleteProperty(loc, 'endToken');
}
return doc
}
function parseDocument(source: string) {
const cacheKey = normalize(source)
if (!docCache.has(cacheKey)) {
const parsed = parse(source, {
allowLegacyFragmentVariables,
})
if (!parsed || parsed.kind !== 'Document') {
throw new Error('Not a valid GraphQL document.')
}
docCache.set(
cacheKey,
// check that all "new" fragments inside the documents are consistent with
// existing fragments of the same name
stripLoc(processFragments(parsed)),
)
}
return docCache.get(cacheKey)!
}
// XXX This should eventually disallow arbitrary string interpolation, like Relay does
export function gql(
literals: string | readonly string[],
...args: DocumentNode[]
) {
if (typeof literals === 'string') {
literals = [literals]
}
let result = literals[0]
args.forEach((arg, i) => {
if (arg && arg.kind === 'Document') {
result += arg.loc!.source.body
} else {
result += arg
}
result += literals[i + 1]
})
return parseDocument(result)
}
export function resetCaches() {
docCache.clear()
fragmentSourceMap.clear()
}
export function disableFragmentWarnings() {
printFragmentWarnings = false
}
export function enableExperimentalFragmentVariables() {
allowLegacyFragmentVariables = true
}
export function disableExperimentalFragmentVariables() {
allowLegacyFragmentVariables = false
}