-
Notifications
You must be signed in to change notification settings - Fork 42
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
Generate a serialize function that convert Config.t to Js.Json.t (opposite of parse function basically) #71
Comments
It’s a great idea. I found it problematic to writeQuery to apollo cache with enum field which is transpiled to poly variant this number in JavaScript. I’ll try to find some time to work on it ;) |
Giving such schema into consideration: interface User {
id: ID!
}
type AdminUser implements User {
id: ID!
name: String!
}
type AnonymousUser implements User {
id: ID!
anonymousId: Int!
}
type OtherUser implements User {
id: ID!
} and such query: query {
users {
id
... on AdminUser {
name
}
... on AnonymousUser {
anonymousId
}
}
} We can receive example JSON from the server: {
"users": [
{ "__typename": "AdminUser", "id": "1", "name": "bob" },
{ "__typename": "AnonymousUser", "id": "2", "anonymousId": 1},
{ "__typename": "OtherUser", "id": "3"}
]
} Our Reason/OCaml output will be presented as polymorphic variant handling most of the cases: type t = [
| `AdminUser({. "id": string, "name": string})
| `Anonymous({. "id": string, "anonymousId": string})
| `User({. "id": string })
] The default case is a I have basic implementation working in my Reason fork: https://github.com/baransu/graphql_ppx_re/tree/serialize_fn Having this implemented would allow us to have proper cache support in reason-apollo or other GraphQL clients. /cc @dawee @Gregoirevda |
I think it makes sense that the default variant keeps the typename. It might be useful if you want to log it: fun
| `User(typename, _) => Js.log("The user type " ++ typename ++ " is not managed yet") Because in most cases for us the variant with the interface name is the case we don't manage. |
Another challenge is Possible solutions I can see are:
|
When we want to write in the cache using writeQuery, writeFragment, fetchMore.updateQuery ... we need to send it in a JSON format.
It can work as is, if the query has no optional, no fragments in it. But if it does, then the parse function returns a type that includes one or more variants. Which is great, but it can't be reused as is as a JSON format.
Even if we try an unsafe cast it wouldn't work because Bucklescript will translate the variant as an array.
Dog(4)
=>[2534, 4]
.What would solve this would be a generated
serialize
function that do the opposite of what parse is doing:The text was updated successfully, but these errors were encountered: