Skip to content

Commit

Permalink
fix: find graphqlrc files relative to linted file (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
lennyburdette committed Jan 20, 2022
1 parent 12b9fca commit 6f8c3b6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/relative-graphqlrc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': minor
---

fix: find graphqlrc files relative to linted file
3 changes: 3 additions & 0 deletions packages/plugin/src/graphql-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GraphQLConfig, GraphQLExtensionDeclaration, loadConfigSync, SchemaPointer } from 'graphql-config';
import { CodeFileLoader } from '@graphql-tools/code-file-loader';
import { ParserOptions } from './types';
import { dirname } from 'path';

let graphQLConfig: GraphQLConfig;

Expand All @@ -14,6 +15,8 @@ export function loadGraphQLConfig(options: ParserOptions): GraphQLConfig {
const onDiskConfig = options.skipGraphQLConfig
? null
: loadConfigSync({
// load config relative to the file being linted
rootDir: options.filePath ? dirname(options.filePath) : undefined,
throwOnEmpty: false,
throwOnMissing: false,
extensions: [addCodeFileLoaderExtension],
Expand Down
1 change: 1 addition & 0 deletions packages/plugin/tests/mocks/using-config/.graphqlrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
schema: ./schema-in-config.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Query {
hello: String
}
3 changes: 3 additions & 0 deletions packages/plugin/tests/mocks/using-config/test.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
query {
hello
}
21 changes: 19 additions & 2 deletions packages/plugin/tests/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,28 @@ describe('schema', () => {
expect(consoleError.mock.calls[0][2]).toMatch(
'Unable to find any GraphQL type definitions for the following pointers'
);
})
});

it('should not log second time error from same schema', () => {
expect(getSchema(undefined, gqlConfig)).toBe(null);
expect(consoleError).toHaveBeenCalledTimes(1);
})
});
});

it('should load the graphql-config rc file relative to the linted file', () => {
const schema = resolve(__dirname, 'mocks/using-config/schema.graphql');
const gqlConfig = loadGraphQLConfig({
schema,
filePath: resolve(__dirname, 'mocks/using-config/test.graphql'),
});

const graphQLSchema = getSchema({ schema }, gqlConfig);
expect(graphQLSchema).toBeInstanceOf(GraphQLSchema);
const sdlString = printSchema(graphQLSchema);
expect(sdlString.trim()).toMatchInlineSnapshot(`
type Query {
hello: String
}
`);
});
});

0 comments on commit 6f8c3b6

Please sign in to comment.