Skip to content

Commit

Permalink
Merge pull request #458 from kidroca/kidroca/minor-import-improvement
Browse files Browse the repository at this point in the history
Loader: allow a space between `#` and `import` word in gql files
  • Loading branch information
hwillson committed Jul 23, 2022
2 parents 8d28727 + fd82f1c commit d9da9c0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ function expandImports(source, doc) {
`;

lines.some((line) => {
if (line[0] === '#' && line.slice(1).split(' ')[0] === 'import') {
const importFile = line.slice(1).split(' ')[1];
const result = line.match(/^#\s?import (.+)$/);
if (result) {
const importFile = result[1];
const parseDocument = `require(${importFile})`;
const appendDef = `doc.definitions = doc.definitions.concat(unique(${parseDocument}.definitions));`;
outputCode += appendDef + os.EOL;
Expand Down
25 changes: 25 additions & 0 deletions src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,31 @@ describe('gql', () => {
assert.equal(definitions[1].kind, 'FragmentDefinition');
});

it('importing files also works with a space between `#` and `import`', () => {
const query = `# import "./fragment_definition.graphql"
query {
author {
...authorDetails
}
}`;
const jsSource = loader.call({ cacheable() {} }, query);
const module = { exports: Object.create(null) };
const require = (path: string) => {
assert.equal(path, './fragment_definition.graphql');
return gql`
fragment authorDetails on Author {
firstName
lastName
}`;
};
Function("module,require", jsSource)(module, require);
assert.equal(module.exports.kind, 'Document');
const definitions = module.exports.definitions;
assert.equal(definitions.length, 2);
assert.equal(definitions[0].kind, 'OperationDefinition');
assert.equal(definitions[1].kind, 'FragmentDefinition');
});

it('tracks fragment dependencies across fragments loaded via the webpack loader', () => {
const query = `#import "./fragment_definition.graphql"
fragment F111 on F {
Expand Down

0 comments on commit d9da9c0

Please sign in to comment.