dillonkearns / elm-graphql

Autogenerate type-safe GraphQL queries in Elm.
https://package.elm-lang.org/packages/dillonkearns/elm-graphql/latest
BSD 3-Clause "New" or "Revised" License
778 stars 107 forks source link

elm-graphql generation fails if there are no columns selected (Hasura) #564

Closed dvekeman closed 2 years ago

dvekeman commented 3 years ago

Something I noticed it accidentally and really minor but wanted to share it here.

In Hasura, in my Permissions tab, I forgot to toggle the update columns

image

Upon generating the elm files for the user role I got

Fetching GraphQL schema...
Generating files...
Unable to run elm-format. Please ensure that elm-format is available on your PATH and try again. Or re-run using --skip-elm-format.

 null

Upon applying the suggestion (running with --skip-elm-format) I found a module containing invalid elm syntax:

{-| update columns of table "exercise_theory_code"
-}
type Exercise_theory_code_update_column
    = 
list : List Exercise_theory_code_update_column
list =
    []

Obviously the solution in the end was to enable some columns (I'm not even sure why hasura allows me to save the permissions without selecting any field) but maybe the generated code should've looked like

type Exercise_theory_code_update_column
    = None

or alternatively no Exercise_theory_code_update_column type and instead

list : List ()

or an explicit error that this is not supported.

dillonkearns commented 2 years ago

Hello @dvekeman, thank you for reporting that! Do you know what the resulting GraphQL Schema Definition Language (SDL) snippet would be?

dvekeman commented 2 years ago

Hi @dillonkearns

Sure, attached is a minimal example. Is this what you were looking for?

"""
columns and relationships of "external_user"
"""
type external_user

schema.graphql.txt

image
dillonkearns commented 2 years ago

Hey @dvekeman, thanks for the info, I'm not sure how to translate that to GraphQL types, though. Could you try sharing it with the GraphQL types in Schema Definition Language? Here's one reference that explains SDL: https://www.digitalocean.com/community/tutorials/graphql-graphql-sdl.

It looks like Hasura also has a guide on exporting SDL: https://hasura.io/docs/latest/graphql/core/guides/export-graphql-schema.html. You could try to export it from that setup, and then just share the relevant section of the exported SDL. That would be a big help if you don't mind!

dvekeman commented 2 years ago

Hi @dillonkearns

In my above comment there is a text file with a link (https://github.com/dillonkearns/elm-graphql/files/7297112/schema.graphql.txt). Maybe you overlooked it but I believe that's what you are looking for?

I generated the file using graphqurl as described on the Hasura documentation.

It is the schema for the (hasura) role 'test' which I created for this ticket. This role only has access to the external_user relation and given I didn't allow any permissions on any of the columns the type got generated as an empty type.

Let me know if you are looking for something else.

Regards Dieter

dillonkearns commented 2 years ago

Hello @dvekeman, sorry for the slow response on this. Yes, I see now that you had attached what I asked for earlier, thanks for clarifying!

So I did some experimentation and I found that I couldn't even get the end-to-end test to parse that schema because the SDL validator considers it invalid GraphQL schema.

Here's a relevant test case:

https://github.com/graphql/graphql-js/blob/73025aa009039c1e2e74f7bbd088ec6ab6e07e94/src/type/__tests__/validation-test.ts#L450-L454

And the relevant code:

https://github.com/graphql/graphql-js/blob/e289e6412ca59cd6298796d61efa19d78eeb945c/src/type/validate.ts#L266-L270

Maybe you could share the result of the introspection JSON query (https://github.com/dillonkearns/elm-graphql/blob/068056540eb566fdf888aa1620605173b7b96511/docs/introspection-query.gql)? Then I could make a test case for that to handle that case.

dvekeman commented 2 years ago

Hi @dillonkearns

Thanks a lot for the investigations. I think the conclusion is that the issue is on the Hasura side.

Our production instance is still running on Hasura v1.3.3 but in the mean time they have released Hasura v2.x which has some breaking changes but we are going to migrate to that version.

So I installed Hasura v2.x on my local environment and couldn't reproduce the problem. It seems like the schema that is generated is now correct and elm-graphql is able to generate the stubs for it.

Upon investigating a bit I think the main difference is

v1.3.3

"""
select columns of table "external_user"
"""
enum external_user_select_column

In v2.0.10 this enum is no longer present (I attached the full schemas for your information).

v1.3.3 schema_v1.3.3.graphql.txt

v2.0.10 schema_v2.0.10.graphql.txt

And in case it would be of any use, here is also the result from the introspection query (on v1.3.3 which had the problem):

hasura_v1.3.3_introspection.json.txt