Hi Nelanka,
Edge is generally has some meaning inside of a connection: it's a cursor + node (the entity inside of a collection). Outside of a connection cursor (and as a direct consequence of it - `Edge`) does not have much meaning. You mention that you already have used `Connection`. Was it a `sangria.relay.Connection` or have you defined `Connection` GraphQL type on your own?
Connection is generally a collection of nodes (entities like `User` or `Product`). Relay wraps every node in `Edge` in order to provide a cursor for every node in a collection. This cursor information can be used to make consequent queries and fetch more elements of this collection that come after the node with this cursor. For instance you can make queries like this:
query FriendsQuery {
user {
friends(first: 2, after: "YXJyYXljb25uZWN0aW9uOjE=") {
totalCount
edges {
# ...
}
}
}
}
`"
YXJyYXljb25uZWN0aW9uOjE="` in this case is a cursor. sangria-relay provides some helper functions to handle scala's `Seq` as a collection. They encode an index of a node inside of a collection in a cursor. But it's just one of possible implementations: cursor can be anything.
sangria-relay provides you with a set of helper functions to define connections with edges. Here is one of the examples (sorry for referencing starwars example once again :) ):
Here is the code:
val ConnectionDefinition(edgeType, shipConnection) = Connection.definition[ShipRepo, Connection, Option[Ship]]("Ship", OptionType(ShipType))
This will define edge GraphQL type (which in this case would be called `
ShipEdge`) and a connection type for a ship. As you will notice later, in this example we are operating on the whole ships list, since it's held in memory:
Field("ships", OptionType(shipConnection), arguments = Connection.Args.All,
resolve = ctx ⇒ Connection.connectionFromSeq(ctx.value.ships map ctx.ctx.getShip, ConnectionArgs(ctx)))))
In this case we can use `
connectionFromSeq` to provide pagination for the ships collection. `
connectionFromSeq` will also transparently create edges with index-based cursors for you. If the full collection can't be held in memory (e.g. only one page of the collection comes from the database), then you can either use overloaded version of `
connectionFromSeq` with `SliceInfo` argument, or you need to implement your own `Collection` and `Edge` types which better fit your scenario.
Hope this explanation helped a bit and haven't made the whole thing even more confusing :) It something is not clear yet or you have further questions, please let me know.
Cheers,
Oleg