Skip to content

Commit 34b4300

Browse files
author
Dimitri POSTOLOV
authored
Fix schema loading when graphql-config is used (#537)
* fix(graphql-config): pass real filepath to `gqlConfig.getProjectForFile` * fix(graphql-config): cache schema by `projectForFile.schema` key
1 parent 9eab654 commit 34b4300

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

.changeset/real-pigs-develop.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+
fix(graphql-config): pass real filepath to `gqlConfig.getProjectForFile`

packages/plugin/src/schema.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { buildSchema, GraphQLSchema } from 'graphql';
88
import { GraphQLConfig } from 'graphql-config';
99
import { dirname } from 'path';
1010
import { ParserOptions } from './types';
11+
import { getOnDiskFilepath } from './utils';
1112

1213
const schemaCache: Map<string, GraphQLSchema> = new Map();
1314

@@ -32,26 +33,24 @@ export function getSchema(options: ParserOptions, gqlConfig: GraphQLConfig): Gra
3233

3334
// We first try to use graphql-config for loading the schema, based on the type of the file,
3435
// We are using the directory of the file as the key for the schema caching, to avoid reloading of the schema.
35-
if (gqlConfig && options?.filePath) {
36-
const fileDir = dirname(options.filePath);
36+
if (gqlConfig && options.filePath) {
37+
const realFilepath = getOnDiskFilepath(options.filePath);
38+
const projectForFile = gqlConfig.getProjectForFile(realFilepath);
39+
const schemaKey = projectForFile.schema.toString();
3740

38-
if (schemaCache.has(fileDir)) {
39-
schema = schemaCache.get(fileDir);
41+
if (schemaCache.has(schemaKey)) {
42+
schema = schemaCache.get(schemaKey);
4043
} else {
41-
const projectForFile = gqlConfig.getProjectForFile(options.filePath);
42-
43-
if (projectForFile) {
44-
schema = projectForFile.getSchemaSync();
45-
schemaCache.set(fileDir, schema);
46-
}
44+
schema = projectForFile.getSchemaSync();
45+
schemaCache.set(schemaKey, schema);
4746
}
4847
}
4948

5049
// If schema was not loaded yet, and user configured it in the parserConfig, we can try to load it,
5150
// In this case, the cache key is the path for the schema. This is needed in order to allow separate
5251
// configurations for different file paths (a very edgey case).
53-
if (!schema && options?.schema) {
54-
const schemaKey = Array.isArray(options.schema) ? options.schema.join(',') : options.schema;
52+
if (!schema && options.schema) {
53+
const schemaKey = options.schema.toString();
5554

5655
if (schemaCache.has(schemaKey)) {
5756
schema = schemaCache.get(schemaKey);

0 commit comments

Comments
 (0)