Reindex
TeamBlogDocs

Connections

The connection model is Relay’s standardized way to express a collection of nodes and paginate through them.

Reindex supports relational data, with one-to-many and many-to-many relationships between nodes. Connection fields are used to access nodes in these relationships and also to fetch all nodes of a certain type.

Relay’s pagination using the connection model is supported out of the box and the necessary Connection Types and Edge Types are generated automatically for all the node types in the schema.

Connection types

type _XConnection {
  count: Int
  nodes: [X]
  edges: [_XEdge]
  pageInfo: PageInfo!
}

type _XEdge {
  cursor: Cursor!
  node: X
}

scalar Cursor

type PageInfo {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
}

Connection

  • count - the total number of elements in the connection.
  • edges - a list of Edges included in the connection
  • nodes - a plain list of nodes in the connection, without the edge wrapper object
  • pageInfo - information about if there are any more elements before or after the current slice

Edges

Edges are elements of edges list of Connections.

  • node - the object wrapped by this edge.
  • cursor - an opaque string-like object, that points to the current node. To be used with before and after arguments.

Connection arguments

All connections fields provide similar arguments. first, last, before and after are required by Relay. orderBy is a Reindex extension to support ordering.

connectionField(first, last: Int, before: Cursor, after: Cursor, orderBy: _SomeOrdering): _SomeConnection
  • first - number of edges to include from the beginning of the result.
  • last - number of edges to include from the end of the result.
  • before - only return edges before given cursor.
  • after - оnly return edges after given cursor.

orderBy

Each type can have a number of orderable fields that can be used as a sort order for the connections of that type. For example, if we have a Todo type with orderable field name, the allowed orderings are defined by an enum like this:

enum _TodoOrdering {
  NAME_ASC
  NAME_DESC
}

We can use the orderBy argument in a connection field like this:

{
  viewer {
    allTodos(orderBy: NAME_DESC) {
      nodes {
        name
      }
    }
  }
}

Filtering

Fields marked in schema as filterable, node fields and connections can all be used to filter connections. Filters are available both in viewer and in relationship connections. Argument name for filter is the same as the field itself and it accepts an object, where keys are filtering operator and value is the filter value.

For example:

{
  viewer {
    allTodos(isCompleted: { eq: true }) {
      nodes {
        name
      }
    }
  }
}

query($id: ID!) {
  userById(id: $id) {
    todos(createdAt: { gte: "2016-01-01", lt: "2016-02-01"}) {
      nodes {
        name
      }
    }
  }
}

query($id: ID!)  {
  viewer {
    allTodos(user: { eq: $id }) {
      nodes {
        name
      }
    }
  }
}

query($id: ID!)  {
  viewer {
    allUsers(microposts: { includes: $id }) {
      nodes {
        name
      }
    }
  }
}

When multiple operators are provided to a filter or multiple fields are filtered by, all conditions must be satisfied.

Available operators depend on the type of the field. Full list of possible operators:

  • eq - equals
  • neq - not equals
  • lt - less than, can be used also for DateTime fields
  • lte - less than or equal, can be used also for DateTime fields
  • gt - greater than, can be used also for DateTime fields
  • gte - greater than or equal, can be used also for DateTime fields
  • isNull - if passed value is true, returns all nodes where field is null, if false returns all nodes where fields is not null.
  • includes - for List fields of scalars, check if the passed scalar is member of the list. For Connection, accepts ID and checks if the object of such id is a member of the connection
  • excludes - as with includes, but checks if the item is not member of Conection or List
Topics:

Connections

The connection model is Relay’s standardized way to express a collection of nodes and paginate through them.

Reindex supports relational data, with one-to-many and many-to-many relationships between nodes. Connection fields are used to access nodes in these relationships and also to fetch all nodes of a certain type.

Relay’s pagination using the connection model is supported out of the box and the necessary Connection Types and Edge Types are generated automatically for all the node types in the schema.

Connection types

type _XConnection {
  count: Int
  nodes: [X]
  edges: [_XEdge]
  pageInfo: PageInfo!
}

type _XEdge {
  cursor: Cursor!
  node: X
}

scalar Cursor

type PageInfo {
  hasNextPage: Boolean!
  hasPreviousPage: Boolean!
}

Connection

  • count - the total number of elements in the connection.
  • edges - a list of Edges included in the connection
  • nodes - a plain list of nodes in the connection, without the edge wrapper object
  • pageInfo - information about if there are any more elements before or after the current slice

Edges

Edges are elements of edges list of Connections.

  • node - the object wrapped by this edge.
  • cursor - an opaque string-like object, that points to the current node. To be used with before and after arguments.

Connection arguments

All connections fields provide similar arguments. first, last, before and after are required by Relay. orderBy is a Reindex extension to support ordering.

connectionField(first, last: Int, before: Cursor, after: Cursor, orderBy: _SomeOrdering): _SomeConnection
  • first - number of edges to include from the beginning of the result.
  • last - number of edges to include from the end of the result.
  • before - only return edges before given cursor.
  • after - оnly return edges after given cursor.

orderBy

Each type can have a number of orderable fields that can be used as a sort order for the connections of that type. For example, if we have a Todo type with orderable field name, the allowed orderings are defined by an enum like this:

enum _TodoOrdering {
  NAME_ASC
  NAME_DESC
}

We can use the orderBy argument in a connection field like this:

{
  viewer {
    allTodos(orderBy: NAME_DESC) {
      nodes {
        name
      }
    }
  }
}

Filtering

Fields marked in schema as filterable, node fields and connections can all be used to filter connections. Filters are available both in viewer and in relationship connections. Argument name for filter is the same as the field itself and it accepts an object, where keys are filtering operator and value is the filter value.

For example:

{
  viewer {
    allTodos(isCompleted: { eq: true }) {
      nodes {
        name
      }
    }
  }
}

query($id: ID!) {
  userById(id: $id) {
    todos(createdAt: { gte: "2016-01-01", lt: "2016-02-01"}) {
      nodes {
        name
      }
    }
  }
}

query($id: ID!)  {
  viewer {
    allTodos(user: { eq: $id }) {
      nodes {
        name
      }
    }
  }
}

query($id: ID!)  {
  viewer {
    allUsers(microposts: { includes: $id }) {
      nodes {
        name
      }
    }
  }
}

When multiple operators are provided to a filter or multiple fields are filtered by, all conditions must be satisfied.

Available operators depend on the type of the field. Full list of possible operators:

  • eq - equals
  • neq - not equals
  • lt - less than, can be used also for DateTime fields
  • lte - less than or equal, can be used also for DateTime fields
  • gt - greater than, can be used also for DateTime fields
  • gte - greater than or equal, can be used also for DateTime fields
  • isNull - if passed value is true, returns all nodes where field is null, if false returns all nodes where fields is not null.
  • includes - for List fields of scalars, check if the passed scalar is member of the list. For Connection, accepts ID and checks if the object of such id is a member of the connection
  • excludes - as with includes, but checks if the item is not member of Conection or List