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

Concat document definitions #105

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 16 additions & 3 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,23 @@ function expandImports(source, doc) {

module.exports = function(source) {
this.cacheable();
const doc = gql`${source}`;
const doc = gql(source, true);

if (doc.definitions) {
for (var i = 0; i < doc.definitions.length; i++) {
if (doc.definitions[i].loc) {
doc.definitions[i].loc = {
start: doc.definitions[i].loc.start,
end: doc.definitions[i].loc.end,
source: doc.definitions[i].loc.source
}
}
}
}

const outputCode = `
var doc = ${JSON.stringify(doc)};
doc.loc.source = ${JSON.stringify(doc.loc.source)};
var doc = ${JSON.stringify(doc, null, 4)};
doc.loc.source = ${JSON.stringify(doc.loc.source, null, 4)};
`;
const importOutputCode = expandImports(source, doc);

Expand Down
29 changes: 26 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function stripLoc(doc, removeLocAtThisLevel) {
return doc;
}

function parseDocument(doc) {
function parseDocument(doc, asImport) {
var cacheKey = normalize(doc);

if (docCache[cacheKey]) {
Expand All @@ -143,7 +143,12 @@ function parseDocument(doc) {
// check that all "new" fragments inside the documents are consistent with
// existing fragments of the same name
parsed = processFragments(parsed);
parsed = stripLoc(parsed, false);
if (asImport) {
parsed.definitions = stripLoc(parsed.definitions, false);
}
else {
parsed = stripLoc(parsed, false);
}
docCache[cacheKey] = parsed;

return parsed;
Expand All @@ -152,23 +157,41 @@ function parseDocument(doc) {
// XXX This should eventually disallow arbitrary string interpolation, like Relay does
function gql(/* arguments */) {
var args = Array.prototype.slice.call(arguments);
var asImport = args[args.length - 1] === true;
if (asImport) {
args = args.slice(0, -1);
}

var literals = args[0];

// We always get literals[0] and then matching post literals for each arg given
var result = (typeof(literals) === "string") ? literals : literals[0];
var definitions = [];

for (var i = 1; i < args.length; i++) {
if (args[i] && args[i].kind && args[i].kind === 'Document') {
result += args[i].loc.source.body;
definitions = definitions.concat(args[i].definitions)
} else {
result += args[i];
}

result += literals[i];
}

return parseDocument(result);
definitions = definitions.filter(function (def) {
return def.kind === 'FragmentDefinition' && def.loc
})

var doc = parseDocument(result, asImport || definitions.length);


if (definitions.length > 0) {
doc.definitions = doc.definitions.concat(definitions)
doc = processFragments(doc)
}

return doc;
}

// Support typescript, which isn't as nice as Babel about default exports
Expand Down
32 changes: 32 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ const assert = require('chai').assert;
assert.equal(definitions[1].kind, 'FragmentDefinition');
});

it('correctly interpolates imports of other files through the webpack loader', () => {
const query =`#import "./fragment_definition.graphql"

fragment BooksAuthor on Book {
author {
...AuthorDetails
}
}
`;
const jsSource = loader.call({ cacheable() {} }, query);

const oldRequire = require;
const module = { exports: undefined };
const require = (path) => {
assert.equal(path, './fragment_definition.graphql');
return gql(`
fragment AuthorDetails on Author {
firstName
lastName
}`, true);
};

eval(jsSource);

const document = gql`query { ...BooksAuthor } ${module.exports}`;
assert.equal(document.kind, 'Document');
assert.equal(document.definitions.length, 3);
assert.equal(document.definitions[0].kind, 'OperationDefinition');
assert.equal(document.definitions[1].kind, 'FragmentDefinition');
assert.equal(document.definitions[2].kind, 'FragmentDefinition');
});

it('does not complain when presented with normal comments', (done) => {
assert.doesNotThrow(() => {
const query = `#normal comment
Expand Down