gqlparser icon indicating copy to clipboard operation
gqlparser copied to clipboard

Default values for input object fields are ignored when using inline input arguments

Open avobl opened this issue 1 year ago • 8 comments

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

avobl avatar Feb 07 '25 13:02 avobl

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.

StevenACoffman avatar Feb 07 '25 13:02 StevenACoffman

@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.

StevenACoffman avatar Feb 07 '25 13:02 StevenACoffman

Hey @StevenACoffman, no, it's not my initiative. I don't have a public repository yet, but I'll create one to help you.

avobl avatar Feb 07 '25 13:02 avobl

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)

avobl avatar Feb 18 '25 09:02 avobl

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.

avobl avatar Feb 20 '25 14:02 avobl

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.

StevenACoffman avatar Feb 20 '25 15:02 StevenACoffman

No problem, Steven. I suppose I'm not able to help with an MR that fixes the issue, sorry.

avobl avatar Feb 26 '25 08:02 avobl

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).

StevenACoffman avatar Feb 26 '25 13:02 StevenACoffman