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

Import GraphQL files from other GraphQL files (closes #1477) #1892

Merged
merged 2 commits into from Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/assets/GraphqlAsset.js
@@ -1,15 +1,62 @@
const Asset = require('../Asset');
const localRequire = require('../utils/localRequire');
const Resolver = require('../Resolver');
const fs = require('../utils/fs');
Copy link
Member

Choose a reason for hiding this comment

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

Please use parcels fs util

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pretty sure I am?

Copy link
Member

Choose a reason for hiding this comment

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

Damn, not sure why I thought it wasn't

const os = require('os');

const IMPORT_RE = /^# *import +['"](.*)['"] *;? *$/;

class GraphqlAsset extends Asset {
constructor(name, options) {
super(name, options);
this.type = 'js';

this.gqlMap = new Map();
this.gqlResolver = new Resolver(
Object.assign({}, this.options, {
extensions: ['.gql', '.graphql']
})
);
}

async traverseImports(name, code) {
this.gqlMap.set(name, code);

await Promise.all(
code
.split(os.EOL)
Copy link
Member

Choose a reason for hiding this comment

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

Will this work in every case?
Seems like this would not work if a user clones a repo that is being used developed various OSes

Copy link
Contributor Author

@arianon arianon Aug 14, 2018

Choose a reason for hiding this comment

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

Good point, using a regex such as /\r?\n/ is probably more resilient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed it to use /\r\n?|\n which is pretty much the same as what the GraphQL specification defines as a line terminator

.map(line => line.match(IMPORT_RE))
.filter(match => !!match)
.map(async ([, importName]) => {
let {path: resolved} = await this.gqlResolver.resolve(
importName,
name
);

if (this.gqlMap.has(resolved)) {
return;
}

let code = await fs.readFile(resolved, 'utf8');
await this.traverseImports(resolved, code);
})
);
}

collectDependencies() {
for (let [path] of this.gqlMap) {
this.addDependency(path, {includedInParent: true});
}
}

async parse(code) {
let gql = await localRequire('graphql-tag', this.name);
return gql(code);

await this.traverseImports(this.name, code);

const allCodes = [...this.gqlMap.values()].join(os.EOL);

return gql(allCodes);
}

generate() {
Expand Down
39 changes: 39 additions & 0 deletions test/graphql.js
Expand Up @@ -35,4 +35,43 @@ describe('graphql', function() {
`.definitions
);
});

it('should support importing other graphql files from a graphql file', async function() {
let b = await bundle(__dirname + '/integration/graphql-import/index.js');

await assertBundleTree(b, {
name: 'index.js',
assets: ['index.js', 'local.graphql'],
childBundles: [
{
type: 'map'
}
]
});

let output = await run(b);
assert.equal(typeof output, 'function');
assert.deepEqual(
output().definitions,
gql`
{
user(id: 6) {
...UserFragment
...AnotherUserFragment
}
}

fragment UserFragment on User {
firstName
lastName
}

fragment AnotherUserFragment on User {
address
email
}

`.definitions
);
});
});
4 changes: 4 additions & 0 deletions test/integration/graphql-import/another.graphql
@@ -0,0 +1,4 @@
fragment AnotherUserFragment on User {
address
email
}
5 changes: 5 additions & 0 deletions test/integration/graphql-import/index.js
@@ -0,0 +1,5 @@
var local = require('./local.graphql');

module.exports = function () {
return local;
};
8 changes: 8 additions & 0 deletions test/integration/graphql-import/local.graphql
@@ -0,0 +1,8 @@
# import './other.graphql'

{
user(id: 6) {
...UserFragment
...AnotherUserFragment
}
}
6 changes: 6 additions & 0 deletions test/integration/graphql-import/other.graphql
@@ -0,0 +1,6 @@
# import './another.graphql'

fragment UserFragment on User {
firstName
lastName
}