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(graphql): handle parentheses in fragment import file paths. #1746

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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brackets in filenames weren't actually breaking fragment imports, but I added this for extra confidence. Let me know if you'd like this to be removed.

}
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');
});
Loading