Skip to content

Commit 52eb17f

Browse files
andimarekleebyron
authored andcommitted
Bugfix getArgumentValues: DefaultValue not used
When no argument was provided, the defaultValue should be used as argument value
1 parent 8727a08 commit 52eb17f

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/executor/__tests__/variables.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ var TestType = new GraphQLObjectType({
5151
args: { input: { type: new GraphQLNonNull(GraphQLString) } },
5252
resolve: (_, { input }) => input && JSON.stringify(input)
5353
},
54+
fieldWithDefaultArgumentValue: {
55+
type: GraphQLString,
56+
args: { input: { type: GraphQLString, defaultValue: 'Hello World' } },
57+
resolve: (_, { input }) => input && JSON.stringify(input)
58+
},
5459
list: {
5560
type: GraphQLString,
5661
args: { input: { type: new GraphQLList(GraphQLString) } },
@@ -720,4 +725,45 @@ describe('Execute: Handles inputs', () => {
720725
});
721726

722727
});
728+
729+
describe('Execute: Uses argument default values', () => {
730+
731+
it('when no argument provided', async () => {
732+
var ast = parse(`{
733+
fieldWithDefaultArgumentValue
734+
}`);
735+
736+
return expect(await execute(schema, ast)).to.deep.equal({
737+
data: {
738+
fieldWithDefaultArgumentValue: '"Hello World"'
739+
}
740+
});
741+
});
742+
743+
it('when nullable variable provided', async () => {
744+
var ast = parse(`query optionalVariable($optional: String) {
745+
fieldWithDefaultArgumentValue(input: $optional)
746+
}`);
747+
748+
return expect(await execute(schema, ast)).to.deep.equal({
749+
data: {
750+
fieldWithDefaultArgumentValue: '"Hello World"'
751+
}
752+
});
753+
});
754+
755+
it('when argument provided cannot be coerced', async () => {
756+
var ast = parse(`{
757+
fieldWithDefaultArgumentValue(input: WRONG_TYPE)
758+
}`);
759+
760+
return expect(await execute(schema, ast)).to.deep.equal({
761+
data: {
762+
fieldWithDefaultArgumentValue: '"Hello World"'
763+
}
764+
});
765+
});
766+
767+
});
768+
723769
});

src/executor/values.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ export function getArgumentValues(
6464
var argASTMap = argASTs ? keyMap(argASTs, arg => arg.name.value) : {};
6565
return argDefs.reduce((result, argDef) => {
6666
var name = argDef.name;
67-
var valueAST = argASTMap[name] && argASTMap[name].value;
68-
result[name] = coerceValueAST(argDef.type, valueAST, variables);
67+
var valueAST = argASTMap[name] ? argASTMap[name].value : null;
68+
var value = coerceValueAST(argDef.type, valueAST, variables);
69+
if (isNullish(value) && !isNullish(argDef.defaultValue)) {
70+
value = argDef.defaultValue;
71+
}
72+
result[name] = value;
6973
return result;
7074
}, {});
7175
}

0 commit comments

Comments
 (0)