Skip to content

Commit

Permalink
fix(graphql): handle parentheses in fragment import file paths. (#1746)
Browse files Browse the repository at this point in the history
fix(graphql): handle parentheses in fragment import file paths
  • Loading branch information
grxy authored Sep 22, 2024
1 parent 68e50ca commit 1a734fc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
10 changes: 4 additions & 6 deletions packages/graphql/src/toESModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ function replaceRequires(source) {
let index = 0;

// replace a require statement with a variable
const replaceSource = source.replace(/require\(([^)]+)\)/gi, (match, path) => {
const replacePath = path.replace(/["']+/g, '');

if (!imports[replacePath]) {
const replaceSource = source.replace(/require\(["']([^"']+)["']\)/gi, (match, path) => {
if (!imports[path]) {
index += 1;
imports[replacePath] = `frgmt${index}`;
imports[path] = `frgmt${index}`;
}

return imports[replacePath];
return imports[path];
});

// prepare import statements
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fragment ParenthesesFragment on Parentheses {
id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fragment BracketsFragment on Brackets {
id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export { default as doc } from './query.graphql';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import "./(parentheses)/fragment.graphql"
#import "./[brackets]/fragment.graphql"

query Query {
...ParenthesesFragment
...BracketsFragment
}
12 changes: 12 additions & 0 deletions packages/graphql/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ test('should support graphqls schema files', async (t) => {
t.truthy('doc' in module.exports);
t.is(module.exports.doc.kind, 'Document');
});

test('should support fragment imports with brackets and parentheses in file paths', async (t) => {
const bundle = await rollup({
input: 'fixtures/fragments-with-special-characters/index.js',
plugins: [graphql()]
});

const { module } = await testBundle(t, bundle);

t.truthy('doc' in module.exports);
t.is(module.exports.doc.kind, 'Document');
});

0 comments on commit 1a734fc

Please sign in to comment.