Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonb field with "path" arg applied can lead to json decode error #49

Open
john-kelly opened this issue Jun 19, 2024 · 0 comments
Open

Comments

@john-kelly
Copy link

john-kelly commented Jun 19, 2024

first off, incredible work!

relevant data to reproduce error

this example comes from the https://pokeapi.co/ graphql api

query:

query AllPokemonWithSprites {
  pokemon_v2_pokemonsprites {
    sprites(path: "front_default")
    pokemon_v2_pokemon {
      name
      id
    }
  }
}

elm-gql / elm-prefab config:

  "graphql": {
    "schema": "https://beta.pokeapi.co/graphql/v1beta",
    "header": []
  }

generated elm decoder:

decoder_ : Int -> Json.Decode.Decoder Response
decoder_ version_ =
    Json.Decode.succeed Response
        |> GraphQL.Decode.versionedField
            version_
            "pokemon_v2_pokemonsprites"
            (Json.Decode.list
                (Json.Decode.succeed Pokemon_v2_pokemonsprites
                    |> GraphQL.Decode.field "sprites" Api.jsonb.decoder
                    |> GraphQL.Decode.field
                        "pokemon_v2_pokemon"
                        (Json.Decode.nullable
                            (Json.Decode.succeed Pokemon_v2_pokemon
                                |> GraphQL.Decode.field
                                    "name"
                                    Json.Decode.string
                                |> GraphQL.Decode.field "id" Json.Decode.int
                            )
                        )
                )
            )

relevant sprites field part of the schema (in case you didnt want to ping schema endpoint):

{
    "name": "sprites",
    "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
}

i found some docs on this kind "json select path" arg in the hasura docs. i'm not sure if this feature is a typical graphql pattern! so apologies if the above link is unnecessary

experimentation

from my testing, it seems that this particular field can be null when you search for a path that isn't found!
i changed the generated code to just be a "nullable string" for this field and everything worked. seems the jsonb decoder is just a Json.Decode.string. not sure the right API change in this case but thought this info could be useful!

i imagine this could be an error with the graphql server/schema itself rather than the elm-gql library? i'm not familiar enough with graphql client vs server to make that claim. i was under the impression that the schema was always correct, but i'm not sure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant