Reputation: 1416
I have a schema like this:
type SectionItem = {
a: String
}
type SectionRowConnection {
pageInfo: PageInfo!
edges: [SectionRowEdge]
}
type SectionRowEdge {
node: [SectionItem]
cursor: String!
}
I want to get a list in each node of connection, and when I run below query manually, everything works fine:
query {
sectionRows(type:someType){
edges{
node{
a
}
}
}
}
Now I'm using Relay in client, but I get this error trying to build the query using relay:
ERROR: Encountered 1 error(s): - @connection used on invalid field
sectionRows
. Expected the field typeSectionRowConnection
to have anedges { node }
field that returns an object, interface, or union.
As the Error suggests, I can not use List inside a relay connection, but I want to have a schema like this, any Ideas on how to use relay with this schema or recommend a workaround for this problem?
Upvotes: 0
Views: 958
Reputation: 1416
What I ended up doing was to wrap the result list inside an object, i.e. each node is of SectionRow
type which is a graphQlObjectType
that has an items
field, the list is inside the items list. here is the resulting schema:
type SectionItem = {
a: String
}
type SectionRow = {
items: [SectionItem]
}
type SectionRowConnection {
pageInfo: PageInfo!
edges: [SectionRowEdge]
}
type SectionRowEdge {
node: SectionRow
cursor: String!
}
Upvotes: 0
Reputation: 84697
From the Relay spec:
An “Edge Type” must contain a field called node. This field must return either a Scalar, Enum, Object, Interface, Union, or a Non‐Null wrapper around one of those types. Notably, this field cannot return a list.
In other words, SectionRowConnection
does not meet Relay's requirements for a connection type because its node field is a List. There is no workaround for this other than modifying your schema so that node
's type is SectionItem!
instead of [SectionItem]
. If you want to use Relay, your schema must be compliant with the Relay specification.
The connection itself represents a collection of individual nodes (or "resources" if we borrow REST terminology), with each edge connecting the root node to an individual node in the collection.
For example, we can imagine a single User node that has a friends
field that returns a UserConnection. Each edge in the connection would represent the association between the original User node and one of the friend users. Each edge would have a single User node.
This collection of User nodes can naturally be sorted and filtered as needed. However, if we wanted to group them together according to some criteria (similar to GROUP BY
in SQL), we would create a separate UserGroup type and a UserGroupConnection. Each UserGroup node inside a UserGroupConnection would then itself have some field that is a UserConnection. Even in this scenario, each connection's edge still only has one node.
It's unclear from just your schema if you were trying to do just simple filtering or "grouping" your nodes as shown above. Either way, conceptually, there is no reason to ever have an edge return a List for its node
field.
Upvotes: 1