Skip to content

Commit

Permalink
Merge pull request #31 from benjie/patch-1
Browse files Browse the repository at this point in the history
Add support for variables in JSON subfields
  • Loading branch information
taion authored Apr 13, 2018
2 parents 6e45ae4 + 96f706a commit 6f45099
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function identity(value) {
return value;
}

function parseLiteral(ast) {
function parseLiteral(ast, variables) {
switch (ast.kind) {
case Kind.STRING:
case Kind.BOOLEAN:
Expand All @@ -15,16 +15,20 @@ function parseLiteral(ast) {
return parseFloat(ast.value);
case Kind.OBJECT: {
const value = Object.create(null);
ast.fields.forEach((field) => {
value[field.name.value] = parseLiteral(field.value);
ast.fields.forEach(field => {
value[field.name.value] = parseLiteral(field.value, variables);
});

return value;
}
case Kind.LIST:
return ast.values.map(parseLiteral);
return ast.values.map(n => parseLiteral(n, variables));
case Kind.NULL:
return null;
case Kind.VARIABLE: {
const name = ast.name.value;
return variables ? variables[name] : undefined;
}
default:
return undefined;
}
Expand Down
13 changes: 7 additions & 6 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { graphql, GraphQLObjectType, GraphQLSchema } from 'graphql';
import { graphql, GraphQLInt, GraphQLObjectType, GraphQLSchema } from 'graphql';

import GraphQLJSON from '../src';

Expand Down Expand Up @@ -46,6 +46,7 @@ describe('GraphQLJSON', () => {
},
},
}),
types: [GraphQLInt],
});
});

Expand All @@ -59,7 +60,7 @@ describe('GraphQLJSON', () => {
it('should support parsing values', () =>
graphql(
schema,
'query ($arg: JSON) { value(arg: $arg) }',
'query ($arg: JSON!) { value(arg: $arg) }',
null,
null,
{ arg: FIXTURE },
Expand All @@ -72,25 +73,25 @@ describe('GraphQLJSON', () => {
describe('parseLiteral', () => {
it('should support parsing literals', () =>
graphql(schema, `
{
query ($intValue: Int = 3) {
value(arg: {
string: "string",
int: 3,
int: $intValue,
float: 3.14,
true: true,
false: false,
null: null,
object: {
string: "string",
int: 3,
int: $intValue,
float: 3.14,
true: true,
false: false,
null: null,
},
array: [
"string",
3,
$intValue,
3.14,
true,
false,
Expand Down

0 comments on commit 6f45099

Please sign in to comment.