Skip to content

Commit 4942b58

Browse files
authored
⚗️ explanation for issue #237 (#240)
1 parent 625f083 commit 4942b58

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

.changeset/silly-flies-act.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
Optimisation of sibling operations loading (cache by project)

packages/plugin/src/sibling-operations.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function loadSiblings(baseDir: string, loadPaths: string[]): Source[] {
5454
}
5555

5656
const operationsCache: Map<string, Source[]> = new Map();
57+
const siblingOperationsCache: Map<Source[], SiblingOperations> = new Map();
5758

5859
export function getSiblingOperations(options: ParserOptions, gqlConfig: GraphQLConfig): SiblingOperations {
5960
let siblings: Source[] | null = null;
@@ -116,6 +117,13 @@ export function getSiblingOperations(options: ParserOptions, gqlConfig: GraphQLC
116117
};
117118
}
118119

120+
// Since the siblings array is cached, we can use it as cache key.
121+
// We should get the same array reference each time we get
122+
// to this point for the same graphql project
123+
if(siblingOperationsCache.has(siblings)) {
124+
return siblingOperationsCache.get(siblings)
125+
}
126+
119127
let fragmentsCache: FragmentSource[] | null = null;
120128

121129
const getFragments = (): FragmentSource[] => {
@@ -197,7 +205,7 @@ export function getSiblingOperations(options: ParserOptions, gqlConfig: GraphQLC
197205
return collected;
198206
};
199207

200-
return {
208+
const siblingOperations = {
201209
available: true,
202210
getFragments,
203211
getOperations,
@@ -208,4 +216,6 @@ export function getSiblingOperations(options: ParserOptions, gqlConfig: GraphQLC
208216
getOperationByType: type => getOperations().filter(o => o.document.operation === type),
209217
getFragmentsInUse: (selectable, recursive = true) => Array.from(collectFragments(selectable, recursive).values()),
210218
};
219+
siblingOperationsCache.set(siblings, siblingOperations)
220+
return siblingOperations
211221
}

0 commit comments

Comments
 (0)