Skip to content

Commit fd82f1c

Browse files
Peter Velkovkidroca
authored andcommitted
Loader: allow a space between # and import word in gql files
Match import statements in graphql files that are written like: - #import "./some/file.graphql" - # import "./some/file.graphql"
1 parent 8d28727 commit fd82f1c

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

loader.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ function expandImports(source, doc) {
2727
`;
2828

2929
lines.some((line) => {
30-
if (line[0] === '#' && line.slice(1).split(' ')[0] === 'import') {
31-
const importFile = line.slice(1).split(' ')[1];
30+
const result = line.match(/^#\s?import (.+)$/);
31+
if (result) {
32+
const importFile = result[1];
3233
const parseDocument = `require(${importFile})`;
3334
const appendDef = `doc.definitions = doc.definitions.concat(unique(${parseDocument}.definitions));`;
3435
outputCode += appendDef + os.EOL;

src/tests.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,31 @@ describe('gql', () => {
236236
assert.equal(definitions[1].kind, 'FragmentDefinition');
237237
});
238238

239+
it('importing files also works with a space between `#` and `import`', () => {
240+
const query = `# import "./fragment_definition.graphql"
241+
query {
242+
author {
243+
...authorDetails
244+
}
245+
}`;
246+
const jsSource = loader.call({ cacheable() {} }, query);
247+
const module = { exports: Object.create(null) };
248+
const require = (path: string) => {
249+
assert.equal(path, './fragment_definition.graphql');
250+
return gql`
251+
fragment authorDetails on Author {
252+
firstName
253+
lastName
254+
}`;
255+
};
256+
Function("module,require", jsSource)(module, require);
257+
assert.equal(module.exports.kind, 'Document');
258+
const definitions = module.exports.definitions;
259+
assert.equal(definitions.length, 2);
260+
assert.equal(definitions[0].kind, 'OperationDefinition');
261+
assert.equal(definitions[1].kind, 'FragmentDefinition');
262+
});
263+
239264
it('tracks fragment dependencies across fragments loaded via the webpack loader', () => {
240265
const query = `#import "./fragment_definition.graphql"
241266
fragment F111 on F {

0 commit comments

Comments
 (0)