diff --git a/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/index.gql b/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/index.gql new file mode 100644 index 00000000000..b8d612d74aa --- /dev/null +++ b/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/index.gql @@ -0,0 +1,3 @@ +fragment BazFields on Foo { + id +} diff --git a/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/package.json b/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/package.json new file mode 100644 index 00000000000..c755cf07e39 --- /dev/null +++ b/packages/plugin/__tests__/mocks/import-fragments/baz-fragment/package.json @@ -0,0 +1,3 @@ +{ + "main": "./index.gql" +} diff --git a/packages/plugin/__tests__/mocks/import-fragments/module-import.gql b/packages/plugin/__tests__/mocks/import-fragments/module-import.gql new file mode 100644 index 00000000000..8a3f6f694c3 --- /dev/null +++ b/packages/plugin/__tests__/mocks/import-fragments/module-import.gql @@ -0,0 +1,7 @@ +# import './baz-fragment' + +query { + foo { + ...BazFields + } +} diff --git a/packages/plugin/__tests__/require-import-fragment.spec.ts b/packages/plugin/__tests__/require-import-fragment.spec.ts index b594e68d836..ab1a1e1b519 100644 --- a/packages/plugin/__tests__/require-import-fragment.spec.ts +++ b/packages/plugin/__tests__/require-import-fragment.spec.ts @@ -13,6 +13,7 @@ function withMocks({ name, filename, errors }: { name: string; filename: string; filename, join(__dirname, 'mocks/import-fragments/foo-fragment.gql'), join(__dirname, 'mocks/import-fragments/bar-fragment.gql'), + join(__dirname, 'mocks/import-fragments/baz-fragment/index.gql'), ], }, } satisfies ParserOptionsForTests, @@ -34,6 +35,10 @@ ruleTester.run('require-import-fragment', rule, { name: 'should not report fragments from the same file', filename: join(__dirname, 'mocks/import-fragments/same-file.gql'), }), + withMocks({ + name: 'should not report with module import', + filename: join(__dirname, 'mocks/import-fragments/module-import.gql'), + }), ], invalid: [ withMocks({ diff --git a/packages/plugin/src/rules/require-import-fragment.ts b/packages/plugin/src/rules/require-import-fragment.ts index d1579d86ff3..375d823e57b 100644 --- a/packages/plugin/src/rules/require-import-fragment.ts +++ b/packages/plugin/src/rules/require-import-fragment.ts @@ -91,7 +91,13 @@ export const rule: GraphQLESLintRule = { const extractedImportPath = comment.value.match(/(["'])((?:\1|.)*?)\1/)?.[2]; if (!extractedImportPath) continue; - const importPath = path.join(path.dirname(filePath), extractedImportPath); + let importPath: string; + try { + importPath = require.resolve(extractedImportPath, { paths: [path.dirname(filePath)] }); + } catch { + importPath = path.join(path.dirname(filePath), extractedImportPath); + } + const hasInSiblings = fragmentsFromSiblings.some( source => source.filePath === importPath, );