Skip to content

Commit

Permalink
Added experimentalFragmentVariables compatibility. (#167)
Browse files Browse the repository at this point in the history
* Added experimentalFragmentVariables compatibility.

* Being explicit about key/value pair
  • Loading branch information
lucasconstantino authored and jnwng committed Apr 19, 2018
1 parent 5a2d70e commit bf2db45
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,15 @@ function stripLoc(doc, removeLocAtThisLevel) {
return doc;
}

var experimentalFragmentVariables = false;
function parseDocument(doc) {
var cacheKey = normalize(doc);

if (docCache[cacheKey]) {
return docCache[cacheKey];
}

var parsed = parse(doc);
var parsed = parse(doc, { experimentalFragmentVariables: experimentalFragmentVariables });
if (!parsed || parsed.kind !== 'Document') {
throw new Error('Not a valid GraphQL document.');
}
Expand All @@ -139,6 +140,14 @@ function parseDocument(doc) {
return parsed;
}

function enableExperimentalFragmentVariables() {
experimentalFragmentVariables = true;
}

function disableExperimentalFragmentVariables() {
experimentalFragmentVariables = false;
}

// XXX This should eventually disallow arbitrary string interpolation, like Relay does
function gql(/* arguments */) {
var args = Array.prototype.slice.call(arguments);
Expand All @@ -165,5 +174,7 @@ function gql(/* arguments */) {
gql.default = gql;
gql.resetCaches = resetCaches;
gql.disableFragmentWarnings = disableFragmentWarnings;
gql.enableExperimentalFragmentVariables = enableExperimentalFragmentVariables;
gql.disableExperimentalFragmentVariables = disableExperimentalFragmentVariables;

module.exports = gql;
10 changes: 10 additions & 0 deletions test/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ const assert = require('chai').assert;
assert.equal(module.exports.Q2.definitions.length, 1);
});

it('parses fragments with variable definitions', () => {
gql.enableExperimentalFragmentVariables();

const parsed = gql`fragment A ($arg: String!) on Type { testQuery }`;
assert.equal(parsed.kind, 'Document');
assert.exists(parsed.definitions[0].variableDefinitions);

gql.disableExperimentalFragmentVariables()
});

// see https://github.com/apollographql/graphql-tag/issues/168
it('does not nest queries needlessly in named exports', () => {
const jsSource = loader.call({ cacheable() {} }, `
Expand Down

0 comments on commit bf2db45

Please sign in to comment.