Zaid-Ajaj / Snowflaqe

A dotnet CLI to generate type-safe GraphQL clients for F# and Fable with automatic deserialization, static query verification and type checking
MIT License
154 stars 25 forks source link

Manage JSONB fields #87

Open gmlion opened 1 year ago

gmlion commented 1 year ago

Hi, we found that JSONB fields are generated as Option properties, and upon insertion they are saved a escaped JSON strings, such as "{\"name\":\"abc\"}" We're using Hasura as GraphQL backend. Are JSON fields unsupported?

Zaid-Ajaj commented 1 year ago

Hi @gmlion,

Can you please provide a subset of your schema or your full schema from which the types are generated? If they are generated as optional fields, it means the hasura backend annotated them as such.

In postgres databases, assuming that is what you are working with in hasura, jsonb fields are just strings so it makes sense that they are saved as escaped strings. Since the data of JSONB fields can be of any shape, they are always serialized as strings and you are expected to deserialize them on the client side yourself.

gmlion commented 1 year ago

This is the schema portion about a JSONB field

{
              "name": "customData",
              "description": null,
              "args": [
                {
                  "name": "path",
                  "description": "JSON select path",
                  "type": {
                    "kind": "SCALAR",
                    "name": "String",
                    "ofType": null
                  },
                  "defaultValue": null
                }
              ],
              "type": {
                "kind": "NON_NULL",
                "name": null,
                "ofType": {
                  "kind": "SCALAR",
                  "name": "jsonb",
                  "ofType": null
                }
              },
              "isDeprecated": false,
              "deprecationReason": null
            }

Our issue is that inserting values using Snowflaqe they are inserted as json strings, and stored as such. Using other means, we can insert a string representing a json object and have it stored as such. For example:

query {
  entity {
    json_data
  }
}

// response

{
  "data": {
    "entity": [
      {
        "json_data": {
          "mean": "not-snowflaqe"
        }
      },
      {
        "json_data": "{\"mean\":\"snowflaqe\"}"
      }
    ]
  }
}