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

Add support for module paths to require-import-fragment #2181

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fragment BazFields on Foo {
id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "./index.gql"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# import './baz-fragment'

query {
foo {
...BazFields
}
}
5 changes: 5 additions & 0 deletions packages/plugin/__tests__/require-import-fragment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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({
Expand Down
8 changes: 7 additions & 1 deletion packages/plugin/src/rules/require-import-fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down