Hey.
I'd like to be able to create a connection where all the related fields are non-nullable.
For example the following...
connection :users, UserType
...will create a connection that can be queried thusly:
users {
edges {
node {
name
}
}
}
The problem is that each type along the line is nullable (that is, UserConection, [UserEdge] and User instead of UserConnection!, [UserEdge!]! and User!). It feels more correct, semantically, to have them all non-nullable, as we know that we always get those if we request them.
This becomes a problem if you try to interface the connection with the Apollo iOS library. When you use it, you define the queries you want and it generates the code necessary to interact with the server. All the nullable types map to Optional in Swift, which results in the client having to force-unwrap optionals all the time. (e.g. edges!.map { node in node.user!.name instead of edges.map { node in node.user.name }).
I've looked into the code, but I could not find an easy way to do it. Can you give me some hints on how I can implement this? Furthermore, are you willing to consider to implement some mechanism to get this out of the box?
Hi, thanks for the great writeup! I totally get your point here.
The default nullability is inspired by the relay spec: https://facebook.github.io/relay/graphql/connections.htm
However, there is a way to override the default, you can specify a custom connection type. There's no example, but you can override field :edges inside .define_connection. The same can be used for field :node inside .define_edge I suppose this will be verbose for so many connections, so maybe you could isolate the generation code in a helper method.
Yes, I think this would make a nice feature for the library!
Here's where the types are generated:
Would it suit you to change the global default nullability? If so, you could add a flag like the existing define_nodes_field flag which is used when defining connection types. What do you think of that?
I think I got the gist, thanks.
Would it suit you to change the global default nullability? If so, you could add a flag like the existing define_nodes_field flag which is used when defining connection types. What do you think of that?
Yep. That would totally solve my case.
(Although it might be nice to be able to define this on a per-connection basis, if somebody wants to migrate incrementally).
This is now customizable in an app by using class-based Relay types, see GraphQL::Types::Relay for inspiration 馃帀
Most helpful comment
I think I got the gist, thanks.
Yep. That would totally solve my case.
(Although it might be nice to be able to define this on a per-connection basis, if somebody wants to migrate incrementally).