Default values for input object fields are ignored when using inline input arguments
Description: When passing an input object inline with a variable, GraphQL does not apply the default values defined in the schema.
Schema Definition:
mutation:
addUser(
input: AddUserInput! = { type: GUEST }
) : AddUserResponse!
input AddUserInput {
type: UserType! = GUEST
}
Unexpected Behavior ❌ Does NOT Work (default value ignored when using inline arguments)
mutation AddUser_0($type: UserType) {
addUser(input: { type: $type }) {
__typename
}
}
Variables:
{}
🔴 Expected: type = GUEST 🔴 Actual: type = nil → Mutation fails
✅ Works as Expected (default value applied)
mutation AddUser_0($input: AddUserInput) {
addUser(input: $input) {
__typename
}
}
Variables:
{
"input": {}
}
🟢 Result: Default value GUEST is applied correctly.
Expected Behavior:
- Default values defined in the schema (type: UserType! = GUEST) should be applied even when using inline input objects.
- The parser should behave consistently, whether the input is provided inline or as a separate variable.
Steps to Reproduce:
- Define an input type with a required/not required (doesn't work for both) field that has a default value.
- Call a mutation using inline input arguments ({ type: $type }) without passing a variable.
- Observe that the default value is not applied, causing the mutation to fail.
- Compare this with passing $input as a separate variable, where the default value is applied correctly.
Actual Behavior:
- Default values are ignored when using inline input objects ({ type: $type }).
- Default values work only when passing the entire input object as a variable ($input).
Additional Note:
It works well with NodeJS Apollo library
Thanks for reporting this! Do you have a public repository that reproduces this issue in a simple way? If you could create one, that would be a big help!
I think it is more likely that this is actually a bug in gqlgen, rather than gqlparser, but I don't know what your setup looks like.
@avobl Did you invite the latta thing above? It looks pretty wrong for this (also it doesn't compile). I'm not opposed to using AI when it helps, but just don't want to leave it unattended.
Hey @StevenACoffman, no, it's not my initiative. I don't have a public repository yet, but I'll create one to help you.
Hi @StevenACoffman , there is the sample repository, where the mentioned issue can be reproduced. https://github.com/avobl/sample-gqlgen
Please reach out to me in case of any questions. (My responses might be delayed due to workload)
Hi @StevenACoffman , just to let you know, I've adjusted the schema to have a nullable value of a field, so now it exactly matches the conditions we have and the request isn't failing, but executes with a nil value instead of default one.
Ok, thanks for keeping me up to date. I would still love any PR (here or in gqlgen) that helps others with this, but I haven't yet had the time myself to dive into it and figure out what's going on.
No problem, Steven. I suppose I'm not able to help with an MR that fixes the issue, sorry.
Yeah, sorry, I've been tied up at work, and my volunteer open source time has been used up on a couple of urgent (but easy!) fixes, where this is a little more tricky to figure out. I plan on getting back to it, so your public repository that reproduces it will help me remember to do so (if you don't return to it before I do).