Skip to content

Commit 6f8c3b6

Browse files
fix: find graphqlrc files relative to linted file (#900)
1 parent 12b9fca commit 6f8c3b6

File tree

6 files changed

+34
-2
lines changed

6 files changed

+34
-2
lines changed

.changeset/relative-graphqlrc.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': minor
3+
---
4+
5+
fix: find graphqlrc files relative to linted file

packages/plugin/src/graphql-config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { GraphQLConfig, GraphQLExtensionDeclaration, loadConfigSync, SchemaPointer } from 'graphql-config';
22
import { CodeFileLoader } from '@graphql-tools/code-file-loader';
33
import { ParserOptions } from './types';
4+
import { dirname } from 'path';
45

56
let graphQLConfig: GraphQLConfig;
67

@@ -14,6 +15,8 @@ export function loadGraphQLConfig(options: ParserOptions): GraphQLConfig {
1415
const onDiskConfig = options.skipGraphQLConfig
1516
? null
1617
: loadConfigSync({
18+
// load config relative to the file being linted
19+
rootDir: options.filePath ? dirname(options.filePath) : undefined,
1720
throwOnEmpty: false,
1821
throwOnMissing: false,
1922
extensions: [addCodeFileLoaderExtension],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
schema: ./schema-in-config.graphql
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type Query {
2+
hello: String
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query {
2+
hello
3+
}

packages/plugin/tests/schema.spec.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,28 @@ describe('schema', () => {
146146
expect(consoleError.mock.calls[0][2]).toMatch(
147147
'Unable to find any GraphQL type definitions for the following pointers'
148148
);
149-
})
149+
});
150150

151151
it('should not log second time error from same schema', () => {
152152
expect(getSchema(undefined, gqlConfig)).toBe(null);
153153
expect(consoleError).toHaveBeenCalledTimes(1);
154-
})
154+
});
155+
});
156+
157+
it('should load the graphql-config rc file relative to the linted file', () => {
158+
const schema = resolve(__dirname, 'mocks/using-config/schema.graphql');
159+
const gqlConfig = loadGraphQLConfig({
160+
schema,
161+
filePath: resolve(__dirname, 'mocks/using-config/test.graphql'),
162+
});
163+
164+
const graphQLSchema = getSchema({ schema }, gqlConfig);
165+
expect(graphQLSchema).toBeInstanceOf(GraphQLSchema);
166+
const sdlString = printSchema(graphQLSchema);
167+
expect(sdlString.trim()).toMatchInlineSnapshot(`
168+
type Query {
169+
hello: String
170+
}
171+
`);
155172
});
156173
});

0 commit comments

Comments
 (0)