Lists
In fuse
we offer two types of lists when building fields t.list
and t.connection
.
The difference between the two is their specialization, t.connection
is specialised towards
cursor-based pagination and t.list
is more general purpose.
List
When using the list
type as followed:
import { addQueryFields } from 'fuse'
addQueryFields((t) => ({
users: t.list({
type: UserNode,
nullable: false,
args: {
offset: t.arg.int(),
limit: t.arg.int(),
},
resolve: async (_, args, context) => {
const result = await getUsers({
skip: args.offset,
take: args.limit,
})
return {
// It's also possible to return a list of ids here in case your
// list endpoint doesn't have all the needed details of the user
// then the node will resolve the rest of the entity.
nodes: result.users,
totalCount: result.totalCount,
}
},
}),
}))
For the above, we get a GraphQL type like this:
type UserList {
nodes: [User]!
totalCount: Int
}
type Query {
users(limit: Int, offset: Int): UserList!
}
The nodes
will output the type you specify the list
to return, the totalCount
has been made optional
on purpose in case your datasource does not return this information.
Connection
When using the connection
type as followed:
import { addQueryFields } from 'fuse'
addQueryFields((t) => ({
users: t.connection({
type: UserNode,
nullable: false,
resolve: async (_, args, context) => {
const result = await getUsers({
skip: args.offset,
take: args.limit,
})
return {
edges: result.users.map(user => ({
node: user,
cursor: user.id,
})),
pageInfo: getPageInfo(result),
}
},
}),
}))
For the above, we get a GraphQL type like this:
type UserEdge {
cursor: String!
node: User!
}
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: String!
endCursor: String!
}
type UserConnection {
edges: [UserEdge]!
pageInfo: PageInfo!
}
type Query {
## Note that the arguments for cursor-based pagination are included automatically
users(after: String, before: String, first: Int, last: Int): QueryLaunchesConnection!
}
As you can see, this type is a lot more in-depth and gears towards cursor-based pagination. The edges
can
be customised to for instance carry relational attributes, this could for instance be the role
of the user in
relation to the parent where this connection is being queried from.