-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,62 @@ | ||
const Asset = require('../Asset'); | ||
const localRequire = require('../utils/localRequire'); | ||
const Resolver = require('../Resolver'); | ||
const fs = require('../utils/fs'); | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this work in every case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, using a regex such as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed it to use |
||
.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() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
fragment AnotherUserFragment on User { | ||
address | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var local = require('./local.graphql'); | ||
|
||
module.exports = function () { | ||
return local; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# import './other.graphql' | ||
|
||
{ | ||
user(id: 6) { | ||
...UserFragment | ||
...AnotherUserFragment | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# import './another.graphql' | ||
|
||
fragment UserFragment on User { | ||
firstName | ||
lastName | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure I am?
There was a problem hiding this comment.
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