Skip to content

Commit

Permalink
⚗️ explanation for issue #237 (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmrysMyrddin committed Dec 23, 2020
1 parent 625f083 commit 4942b58
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/silly-flies-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

Optimisation of sibling operations loading (cache by project)
12 changes: 11 additions & 1 deletion packages/plugin/src/sibling-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function loadSiblings(baseDir: string, loadPaths: string[]): Source[] {
}

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

export function getSiblingOperations(options: ParserOptions, gqlConfig: GraphQLConfig): SiblingOperations {
let siblings: Source[] | null = null;
Expand Down Expand Up @@ -116,6 +117,13 @@ export function getSiblingOperations(options: ParserOptions, gqlConfig: GraphQLC
};
}

// 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
if(siblingOperationsCache.has(siblings)) {
return siblingOperationsCache.get(siblings)
}

let fragmentsCache: FragmentSource[] | null = null;

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

return {
const siblingOperations = {
available: true,
getFragments,
getOperations,
Expand All @@ -208,4 +216,6 @@ export function getSiblingOperations(options: ParserOptions, gqlConfig: GraphQLC
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
}

0 comments on commit 4942b58

Please sign in to comment.