Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: find graphqlrc files relative to linted file #900

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
`);
});
});