-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
sibling-operations.ts
201 lines (175 loc) · 6.48 KB
/
sibling-operations.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
import { resolve } from 'path';
import {
FragmentDefinitionNode,
FragmentSpreadNode,
Kind,
OperationDefinitionNode,
SelectionSetNode,
visit,
OperationTypeNode,
} from 'graphql';
import { Source, asArray } from '@graphql-tools/utils';
import { GraphQLConfig } from 'graphql-config';
import { ParserOptions } from './types';
import { getOnDiskFilepath, loaderCache } from './utils';
export type FragmentSource = { filePath: string; document: FragmentDefinitionNode };
export type OperationSource = { filePath: string; document: OperationDefinitionNode };
export type SiblingOperations = {
available: boolean;
getOperations(): OperationSource[];
getFragments(): FragmentSource[];
getFragment(fragmentName: string): FragmentSource[];
getFragmentByType(typeName: string): FragmentSource[];
getFragmentsInUse(
baseOperation: OperationDefinitionNode | FragmentDefinitionNode | SelectionSetNode,
recursive: boolean
): FragmentDefinitionNode[];
getOperation(operationName: string): OperationSource[];
getOperationByType(operationType: OperationTypeNode): OperationSource[];
};
const handleVirtualPath = (documents: Source[]): Source[] => {
const filepathMap: Record<string, number> = Object.create(null);
return documents.map(source => {
const { location } = source;
if (['.gql', '.graphql'].some(extension => location.endsWith(extension))) {
return source;
}
filepathMap[location] ??= -1;
const index = filepathMap[location] += 1;
return {
...source,
location: resolve(location, `${index}_document.graphql`)
};
});
};
const operationsCache: Map<string, Source[]> = new Map();
const siblingOperationsCache: Map<Source[], SiblingOperations> = new Map();
const getSiblings = (filePath: string, gqlConfig: GraphQLConfig): Source[] => {
const realFilepath = filePath ? getOnDiskFilepath(filePath) : null;
const projectForFile = realFilepath ? gqlConfig.getProjectForFile(realFilepath) : gqlConfig.getDefault();
const documentsKey = asArray(projectForFile.documents)
.sort()
.join(',');
if (!documentsKey) {
return [];
}
let siblings = operationsCache.get(documentsKey);
if (!siblings) {
const documents = projectForFile.loadDocumentsSync(projectForFile.documents, {
skipGraphQLImport: true,
cache: loaderCache
});
siblings = handleVirtualPath(documents)
operationsCache.set(documentsKey, siblings);
}
return siblings;
};
export function getSiblingOperations(options: ParserOptions, gqlConfig: GraphQLConfig): SiblingOperations {
const siblings = getSiblings(options.filePath, gqlConfig);
if (siblings.length === 0) {
let printed = false;
const noopWarn = () => {
if (!printed) {
// eslint-disable-next-line no-console
console.warn(
`getSiblingOperations was called without any operations. Make sure to set "parserOptions.operations" to make this feature available!`
);
printed = true;
}
return [];
};
return {
available: false,
getFragments: noopWarn,
getOperations: noopWarn,
getFragment: noopWarn,
getFragmentByType: noopWarn,
getOperation: noopWarn,
getOperationByType: noopWarn,
getFragmentsInUse: noopWarn,
};
}
// Since the siblings array is cached, we can use it as cache key.
// We should get the same array reference each time we get
// to this point for the same graphql project
let siblingOperations = siblingOperationsCache.get(siblings);
if (!siblingOperations) {
let fragmentsCache: FragmentSource[] | null = null;
const getFragments = (): FragmentSource[] => {
if (fragmentsCache === null) {
const result: FragmentSource[] = [];
for (const source of siblings) {
for (const definition of source.document.definitions || []) {
if (definition.kind === Kind.FRAGMENT_DEFINITION) {
result.push({
filePath: source.location,
document: definition as FragmentDefinitionNode,
});
}
}
}
fragmentsCache = result;
}
return fragmentsCache;
};
let cachedOperations: OperationSource[] | null = null;
const getOperations = (): OperationSource[] => {
if (cachedOperations === null) {
const result: OperationSource[] = [];
for (const source of siblings) {
for (const definition of source.document.definitions || []) {
if (definition.kind === Kind.OPERATION_DEFINITION) {
result.push({
filePath: source.location,
document: definition as OperationDefinitionNode,
});
}
}
}
cachedOperations = result;
}
return cachedOperations;
};
const getFragment = (name: string) => getFragments().filter(f => f.document.name?.value === name);
const collectFragments = (
selectable: SelectionSetNode | OperationDefinitionNode | FragmentDefinitionNode,
recursive = true,
collected: Map<string, FragmentDefinitionNode> = new Map()
) => {
visit(selectable, {
FragmentSpread(spread: FragmentSpreadNode) {
const name = spread.name.value;
const fragmentInfo = getFragment(name);
if (fragmentInfo.length === 0) {
// eslint-disable-next-line no-console
console.warn(
`Unable to locate fragment named "${name}", please make sure it's loaded using "parserOptions.operations"`
);
return;
}
const fragment = fragmentInfo[0];
const alreadyVisited = collected.has(name);
if (!alreadyVisited) {
collected.set(name, fragment.document);
if (recursive) {
collectFragments(fragment.document, recursive, collected);
}
}
},
});
return collected;
};
siblingOperations = {
available: true,
getFragments,
getOperations,
getFragment,
getFragmentByType: typeName => getFragments().filter(f => f.document.typeCondition?.name?.value === typeName),
getOperation: name => getOperations().filter(o => o.document.name?.value === name),
getOperationByType: type => getOperations().filter(o => o.document.operation === type),
getFragmentsInUse: (selectable, recursive = true) => Array.from(collectFragments(selectable, recursive).values()),
};
siblingOperationsCache.set(siblings, siblingOperations);
}
return siblingOperations;
}