The prismaConnection method on a field builder can be used to create a relay connection field
that also pre-loads all the data nested inside that connection.
type: the name of the prisma model being connected to
cursor: a @unique column of the model being connected to. This is used as the cursor option
passed to prisma.
defaultSize: (default: 20) The default page size to use if first and last are not provided.
maxSize: (default: 100) The maximum number of nodes returned for a connection.
resolve: Like the resolver for prismaField, the first argument is a query object that should
be spread into your prisma query. The resolve function should return an array of nodes for the
connection. The query will contain the correct take, skip, and cursor options based on the
connection arguments (before, after, first, last), along with include options for nested
selections.
totalCount: A function for loading the total count for the connection. This will add a
totalCount field to the connection object. The totalCount method will receive (connection,
args, context, info) as arguments. Note that this will not work when using a shared
connection object (see details below)
The created connection queries currently support the following combinations of connection arguments:
first, last, or before
first and before
last and after
Queries for other combinations are not as useful, and generally requiring loading all records
between 2 cursors, or between a cursor and the end of the set. Generating query options for these
cases is more complex and likely very inefficient, so they will currently throw an Error indicating
the argument combinations are not supported.
The maxSize and defaultSize can also be configured globally using maxConnectionSize and
defaultConnectionSize options in the prisma plugin options.
cursor: a @unique column of the model being connected to. This is used as the cursor option
passed to prisma.
defaultSize: (default: 20) The default page size to use if first and last are not provided.
maxSize: (default: 100) The maximum number of nodes returned for a connection.
query: A method that accepts the args and context for the connection field, and returns
filtering and sorting logic that will be merged into the query for the relation.
totalCount: when set to true, this will add a totalCount field to the connection object. see
relationCount above for more details. Note that this will not work when using a shared
connection object (see details below)
Creating connections from indirect relations is a little more involved, but can be achieved using
prismaConnectionHelpers with a normal t.connection field.
The above example assumes that you are paginating a relation to a join table, where the pagination
args are applied based on the relation to that join table, but the nodes themselves are nested
deeper.
prismaConnectionHelpers can also be used to manually create a connection where the edge and
connections share the same model, and pagination happens directly on a relation to nodes type (even
if that relation is nested).
To add arguments for a connection defined with a helper, it is often easiest to define the arguments
on the connection field rather than the connection helper. This allows connection helpers to be
shared between fields that may not share the same arguments:
Arguments, ordering and filtering can also be defined on the helpers themselves:
If you are set the totalCount: true on a prismaConnection or relatedConnection field, and are
using a custom connection object, you will need to manually add the totalCount field to the
connection object manually. The parent object on the connection will have a totalCount property
that is either a the totalCount, or a function that will return the totalCount.
If you want to add a global totalCount field, you can do something similar using
builder.globalConnectionField:
These functions can be used to manually parse and format cursors that are compatible with prisma
connections.
Parsing a cursor will return the value from the column used for the cursor (often the id), this
value may be an array or object when a compound index is used as the cursor. Similarly, to format a
cursor, you must provide the column(s) that make up the cursor.