Skip to content

Commit 1ffc489

Browse files
committed
handle null values in valueFromAST
1 parent 005fe36 commit 1ffc489

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/execution/__tests__/variables-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,36 @@ describe('Execute: Handles inputs', () => {
159159
});
160160
});
161161

162+
it('properly parses null value to null', async () => {
163+
const doc = `
164+
{
165+
fieldWithObjectInput(input: {a: null, b: null, c: "C", d: null})
166+
}
167+
`;
168+
const ast = parse(doc);
169+
170+
return expect(await execute(schema, ast)).to.deep.equal({
171+
data: {
172+
fieldWithObjectInput: '{"a":null,"b":null,"c":"C","d":null}'
173+
}
174+
});
175+
});
176+
177+
it('properly parses null value in list', async () => {
178+
const doc = `
179+
{
180+
fieldWithObjectInput(input: {b: ["A",null,"C"], c: "C"})
181+
}
182+
`;
183+
const ast = parse(doc);
184+
185+
return expect(await execute(schema, ast)).to.deep.equal({
186+
data: {
187+
fieldWithObjectInput: '{"b":["A",null,"C"],"c":"C"}'
188+
}
189+
});
190+
});
191+
162192
it('does not use incorrect value', async () => {
163193
const doc = `
164194
{

src/utilities/valueFromAST.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import type {
4242
* | String | String |
4343
* | Int / Float | Number |
4444
* | Enum Value | Mixed |
45+
* | NullValue | null |
4546
*
4647
*/
4748
export function valueFromAST(
@@ -57,13 +58,17 @@ export function valueFromAST(
5758
}
5859

5960
if (!valueAST) {
61+
return;
62+
}
63+
64+
if (valueAST.kind === Kind.NULL) {
6065
return null;
6166
}
6267

6368
if (valueAST.kind === Kind.VARIABLE) {
6469
const variableName = (valueAST: Variable).name.value;
6570
if (!variables || !variables.hasOwnProperty(variableName)) {
66-
return null;
71+
return;
6772
}
6873
// Note: we're not doing any checking that this variable is correct. We're
6974
// assuming that this query has been validated and the variable usage here
@@ -83,7 +88,7 @@ export function valueFromAST(
8388

8489
if (type instanceof GraphQLInputObjectType) {
8590
if (valueAST.kind !== Kind.OBJECT) {
86-
return null;
91+
return;
8792
}
8893
const fields = type.getFields();
8994
const fieldASTs = keyMap(
@@ -93,13 +98,14 @@ export function valueFromAST(
9398
return Object.keys(fields).reduce((obj, fieldName) => {
9499
const field = fields[fieldName];
95100
const fieldAST = fieldASTs[fieldName];
96-
let fieldValue =
101+
const fieldValue =
97102
valueFromAST(fieldAST && fieldAST.value, field.type, variables);
98-
if (isNullish(fieldValue)) {
99-
fieldValue = field.defaultValue;
100-
}
101-
if (!isNullish(fieldValue)) {
103+
if (fieldValue === null) {
104+
obj[fieldName] = fieldValue;
105+
} else if (!isNullish(fieldValue)) {
102106
obj[fieldName] = fieldValue;
107+
} else if (fieldValue !== null) {
108+
obj[fieldName] = field.defaultValue;
103109
}
104110
return obj;
105111
}, {});
@@ -111,6 +117,7 @@ export function valueFromAST(
111117
);
112118

113119
const parsed = type.parseLiteral(valueAST);
120+
// TODO: Should be this condition ommited?
114121
if (!isNullish(parsed)) {
115122
return parsed;
116123
}

0 commit comments

Comments
 (0)