ChilliCream / graphql-platform

Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
https://chillicream.com
MIT License
5.25k stars 745 forks source link

StrawberryShake generator gets confused over raw json in schema #6257

Closed niles11 closed 11 months ago

niles11 commented 1 year ago

Is there an existing issue for this?

Product

Strawberry Shake

Describe the bug

Hi, i'm using StrawberryShake to connect to a ContentStack GraphQL interface. ContentStack is a CMS, which means you can add content models in the CMS and after doing that the schema will automatically update accordingly. The StrawberryShake experience so far has been good, even with man, many different content models which all have nested fields.

However, recently I started using a field type that we had not used before in any content model: the JSON RTE field. This field accepts markdown text but it will be converted into json. After running the command dotnet graphql update ... and doing a rebuild of the solution I get the following error:

An item with the same key has already been added. Key: JSON

I do see these changes in the schema.graphql file:

2023-06-12 11_32_02-Sourcetree

The changes seem to suggest there is support for 'raw' json within the graphql response. But the solution no longer compiles because of the error mentioned above. It should not be related to this StackOverflow issue, because without the JSON RTE field in the content model StrawberryShake immediately works perfectly fine.

Steps to reproduce

  1. Create a (in my case .net 6) project
  2. Setup StrawberryShake
  3. Connect to ContentStack CMS (sorry, you need an account for this, there is no ContentStack playground)
  4. Add model without JSON RTE field
  5. Update the schema.json with update command
  6. Notice it works
  7. Add JSON RTE field to the model which outputs 'raw' JSON
  8. Update the schema.json again with update command
  9. The solution no longer builds and throws the error defined below in the next question

Relevant log output

An item with the same key has already been added. Key: JSON

Additional Context?

No response

Version

13.2.0

ademchenko commented 1 year ago

This is a very serious issue which is easily reproduced on any schema having the definition of scalar JSON.

I have managed to make the reproducing example even for the Hotchocolate server.

Suppose we have the very basic schema:

schema {
  query: Query
}

type Query {
  persons: [Person!]!
}

type Person {
  data: JSON
}

scalar JSON

Or in terms of code first:


namespace Demo.Server;

public class Person
{
    public System.Text.Json.JsonElement? Data { get; set; }
}

public class Query
{
    public ICollection<Person> GetPersons() => Array.Empty<Person>();
}

public class QueryType : ObjectType<Query> { }

Then any attempt of generating the client (whether with dotnet build or with dotnet graphql generate ) causes the error "EXEC : error GQL: An item with the same key has already been added. Key: JSON"

I have prepared the solution with the full reproducing of the issue and attached here strawberryshake-6257.zip

Once the problem is incorrect processing of 'scalar JSON' in the schema the workaround is to remove (or comment out) in schema.graphql the scalar JSON. In that case, the client will be generated successfully but those fields that are of JSON type will have the type 'string' instead of JsonDocument/JsonElement.

chenglidev commented 1 year ago

I encountered the same issue. Is there any patch scheduled?

nightroman commented 1 year ago

Same issue. After reading the encouraging What's New in v13 about JSON - https://chillicream.com/blog/2023/02/08/new-in-hot-chocolate-13#json-scalar tried to engage in our project. Alas...

sfnmk commented 12 months ago

Any news on this? I cannot use the client as it is right now.

niles11 commented 11 months ago

It seems this is fixed since v13.8.0, which has been released yesterday. This exact issue is not resolved or mentioned in the releasenotes so I only noticed the change by coincidence. The releasenotes do mention these two fixes:

I have checked and I can verify that this solves my original issue 🥳