-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add lazy function or explicitly explain how to deal with recursive decoders and encoders #24
Comments
That's an excellent suggestion for making the documentation more clear :) As for For what it's worth, I recommend having each query in its own module to make namespacing of the type aliases easier. Then most of the aliases would be like I will add a section about this stuff to the README and/or the |
Can you please add a code example of how to deal with complex arguments like filtering? I've spent a lot of time reading docs but didn't realized what to do ;( Here is an example I have such GraphQL schema:
... and want to fetch mutual followers of the user with the given slug:
How can I construct an argument for
|
Wow! I've made it! 🥇
It looks terrible and has even more terrible type signature, but it works! ;)) And it doesn't require to write any additional Elm types for it. |
Oh wow...yeah that’s a pretty complex situation, good job for figuring it out! One thing though – if you wanted to end up with a query that’s equivalent to this one you provided:
...then it looks like you originally wanted the slugVar =
Var.required "slug" .slug Var.string
mutualFollowersField =
aliasAs "mutualFollowers" <|
field "followers"
[ ( "filter"
, Arg.object
[ ( "AND"
, Arg.list
[ Arg.object
[ ( "followers_some"
, Arg.object
[ ( "slug", Arg.variable slugVar ) ]
)
, ( "following_some"
, Arg.object
[ ( "slug", Arg.variable slugVar ) ]
)
]
]
)
]
)
]
(list userBasicInfo) With the above code, the user would be able to supply |
First of all thank you very much for your awesome package! ))
In
Json.Decode
module there is a functionlazy
which helps with building recursive encoders. Please define in your moduleGraphQL.Request.Builder
the same function or explicitly explain in module docs how to deal with recursive decoding and encoding, because it confuses inexperienced Elm developers.An example
After reading the module documentation I've understood I should define types as they are written in GraphQL schema (now I understand that your explanation of example has another meaning, but I am sure many other users will make a conclusion like mine one). To evade compilation error because of mutually recursive types I've followed recomendations of the compiler error message and defined types
Tweets
andUsers
:Then I've tried to build GraphQL decoders as described in the example in
GraphQL.Request.Builder
docs:But as expected I've got a compilation error:
There is a link in that error explaining the bad recursion problem and advising how to solve it with Json decoders (with the help of
lazy
function), but it doesn't help with decoding recursive GraphQL decoders.My proposition on how to improve module docs
I've found a solution after researching the outdated
elm-graphql
module docs.First of all, make a clear statement in your docs that user should define types in Elm module not as they are present in GraphQL schema, but according to his GraphQL queries and mutations. Usually, people don't build deeply recursive queries like:
Much likely their GraphQL queries will be much less nested:
I am sure you see that the last query is not only much less nested but also ends (as every other valid GraphQL request) with a scalar value. But when you think you should implement in Elm types from your GraphQL schema it is not so obvious. Thus please make accent that users have to define their Elm types according to their GraphQL requests, but not according to GraphQL types in their schema:
Renaming types from
User
andPhoto
(like in your example) with the query- or mutation-related names will help to understand that types should reflect the query result, but not types in GraphQL schema! Please add the clear explanation that all query decoders should always end only with scalar decoders because otherwise they will be mutually recursive and will be rejected by the compiler.I've ended with such decoder which was accepted by the compiler:
Thanks again for your awesome package. I hope the explanation of my misunderstanding of the package docs will help other users successfully use your package and GraphQL.
The text was updated successfully, but these errors were encountered: