graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

Union types: EF Core exception: The client projection contains a reference to a constant expression...

Open HaroldH76 opened this issue 8 months ago • 1 comments

Product

Hot Chocolate

Version

15.1.3

Link to minimal reproduction

https://github.com/HaroldH76/Hotchocolate-8252/

Steps to reproduce

The issue looks like the one of: https://github.com/ChilliCream/graphql-platform/issues/4318

But it might be slightly different. We are experiencing this issue with union types. I have downloaded the reproduction case of 4318 and upgraded it to the latest HotChocolate version 15.1.3. And that one worked ok.

Next I have updated it to use union types. And I have simplified the example and the database models and used some models of the HotChocolate documentation. And I have added support for in memory database.

What is expected?

When I query not the complete set of union types but a subset then I expect only that subset to be returned. For example when my union type consists of 2 types and I specify only one then only that one should be returned.

{
  contents {
    items {
      ... on TextContent {
        text
      }
    }
  }
}

What is actually happening?

Depending on the query and depending if I use the in memory database or a real database I get:

  • null as result or
  • Exception of EF Core:

The client projection contains a reference to a constant expression of 'Reproduction.ImageContent'. This could potentially cause a memory leak; consider assigning this constant to a local variable and using the variable in the query instead.

Additional context

Se the readme of the reproduction case for more information and more example graphql queries: https://github.com/HaroldH76/Hotchocolate-8252/

HaroldH76 avatar Apr 18 '25 11:04 HaroldH76

That you get null as a result is correct. You have two entries in your data source: [ImageContext, TextContext]. Therefore, for your selection the result is: [null, TextContext], since the resolver simply returns the data source.

However, your resolver is not nullable: public List<PostContent> Contents([Service] MyDatabase database, IResolverContext context)

Because of this, the null value in the selection result causes an error that bubbles up to the data field. If you change your resolver to be nullable: public List<PostContent?> Contents([Service] MyDatabase database, IResolverContext context) you get the result you probably expect:

{
  "data": {
    "contents": {
      "items": [
        null,
        {
          "text": "Hello World"
        }
      ]
    }
  }
}

As for the exception: It is caused by only selecting __typename without an acutal field of the entity/DTO as done in the query within the repo. I will raise a PR for that.

N-Olbert avatar Sep 16 '25 16:09 N-Olbert