Search code examples
graphqlgraphql-jsrelayjs

How to Handle List inside Connection in Graphql Relay


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 type SectionRowConnection to have an edges { 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?


Solution

  • 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!
    }