question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-generated mutation payload incorrectly set to null when mutation returns null

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

We use .AddMutationConventions(applyToAllMutations: true) to automatically apply conventions to all our mutations. However, when null is returned from the mutation, it seems to return null for the entire auto-generated __payload type.

For example:

// mutation code
public async Task<GuardianProfile> DeleteProfileAndRelationships

// generated schema
deleteProfileAndRelationships: DeleteProfileAndRelationshipsPayload!

// error when DeleteProfileAndRelationships returns null GuardianProfile
{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field.",
      "locations": [
        {
          "line": 4,
          "column": 3
        }
      ],
      "path": [
        "deleteProfileAndRelationships"
      ],
      "extensions": {
        "code": "HC0018"
      }
    }
  ]
}

Expected: Valid DeleteProfileAndRelationshipsPayload returned with a guardianProfile: null field

Actual: Nullability error thrown

Steps to reproduce

  1. Use .AddMutationConventions(applyToAllMutations: true)
  2. Create a simple mutation that returns a reference type
  3. Observe that a non-nullable ___payload type is auto-generated in the schema
  4. Return null from mutation
  5. Observe that a nullability error is thrown
  6. Now, use [UseMutationConvention(Disable = true)] to NOT use mutation convention
  7. Observe that a nullable return type is specified in the schema, as expected
  8. Return null from mutation
  9. Observe that it works as expected, null is returned

Relevant log output

No response

Additional Context?

No response

Product

Hot Chocolate

Version

12.7.0

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
nancyl-olcommented, Oct 10, 2022

But why does it work without convention. It should also fail

GuardianProfile is a nullable type since it’s a reference type and we don’t have nullable reference types enabled (https://chillicream.com/docs/hotchocolate/v12/defining-a-schema/non-null#implicit-nullability). So without the convention, the mutation is allowed to return null.

With the convention enabled, the auto-generated mutation return type DeleteProfileAndRelationshipsPayload is non-nullable, which makes sense. However, when the mutation returns a null GuardianProfile, I am expecting

{
  "data": {
    "deleteProfileAndRelationships": {
      "guardianProfile": null
    }
  }
}

Instead, I think it is returning null for the entire DeleteProfileAndRelationships**Payload**, which causes the nullability error.

Can you create a minimal Repro?

Sure, working on creating one

1reaction
michaelstaibcommented, Oct 11, 2022

@nancyl-ol tanks for the repro. The issue here seems to be that this mutation has now input. Its a bug … By trying to repro your issue I found another related bug. I will fix both with 12.16.0

I will add some test cases first and you can expect a fix by the end of the week.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Graphql mutation returning null, but database is updated
The problem is that you are not returning any value in resolve function, the lineitem.save(); returns the value inside the callBack.
Read more >
Subscription returns null for deep elements. ( ...
Hi everyone, I'm currently using subscriptions to update the state of my "manager" in order to spread the update through useContext.
Read more >
Returning null from a GraphQL mutation in type-graphql
All GraphQL types are nullable by default so you can just return a bool in the schema but return void from the implementation....
Read more >
GraphQL permits null fields on mutation
The gist of my question is why do the auto-generated mutations not enforce the presence of fields marked non-null on types. The details:....
Read more >
GraphQL Nullability - Episode #42
This mutation has a non-null input argument CreateUserInput! , and the createUser mutation returns non-null User! . The input type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found