Skip to content

Commit

Permalink
GraphQLCache: fix multiple projects in one graphqlrc
Browse files Browse the repository at this point in the history
  • Loading branch information
scamden authored and acao committed May 5, 2023
1 parent 63762fd commit 632a7c6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-bears-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-language-service-server': patch
---

allow caching for multiple projects in graphql config
22 changes: 14 additions & 8 deletions packages/graphql-language-service-server/src/GraphQLCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,28 @@ export class GraphQLCache implements GraphQLCacheInterface {
return referencedFragments;
};

_cacheKeyForProject = ({ dirpath, name }: GraphQLProjectConfig): string => {
return `${dirpath}-${name}`;
};

getFragmentDefinitions = async (
projectConfig: GraphQLProjectConfig,
): Promise<Map<string, FragmentInfo>> => {
// This function may be called from other classes.
// If then, check the cache first.
const rootDir = projectConfig.dirpath;
if (this._fragmentDefinitionsCache.has(rootDir)) {
return this._fragmentDefinitionsCache.get(rootDir) || new Map();
const cacheKey = this._cacheKeyForProject(projectConfig);
if (this._fragmentDefinitionsCache.has(cacheKey)) {
return this._fragmentDefinitionsCache.get(cacheKey) || new Map();
}

const list = await this._readFilesFromInputDirs(rootDir, projectConfig);

const { fragmentDefinitions, graphQLFileMap } =
await this.readAllGraphQLFiles(list);

this._fragmentDefinitionsCache.set(rootDir, fragmentDefinitions);
this._graphQLFileListCache.set(rootDir, graphQLFileMap);
this._fragmentDefinitionsCache.set(cacheKey, fragmentDefinitions);
this._graphQLFileListCache.set(cacheKey, graphQLFileMap);

return fragmentDefinitions;
};
Expand Down Expand Up @@ -286,14 +291,15 @@ export class GraphQLCache implements GraphQLCacheInterface {
// This function may be called from other classes.
// If then, check the cache first.
const rootDir = projectConfig.dirpath;
if (this._typeDefinitionsCache.has(rootDir)) {
return this._typeDefinitionsCache.get(rootDir) || new Map();
const cacheKey = this._cacheKeyForProject(projectConfig);
if (this._typeDefinitionsCache.has(cacheKey)) {
return this._typeDefinitionsCache.get(cacheKey) || new Map();
}
const list = await this._readFilesFromInputDirs(rootDir, projectConfig);
const { objectTypeDefinitions, graphQLFileMap } =
await this.readAllGraphQLFiles(list);
this._typeDefinitionsCache.set(rootDir, objectTypeDefinitions);
this._graphQLFileListCache.set(rootDir, graphQLFileMap);
this._typeDefinitionsCache.set(cacheKey, objectTypeDefinitions);
this._graphQLFileListCache.set(cacheKey, graphQLFileMap);

return objectTypeDefinitions;
};
Expand Down

0 comments on commit 632a7c6

Please sign in to comment.