Connections
prismaConnection
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.
options
type: the name of the prisma model being connected tocursor: a@uniquecolumn of the model being connected to. This is used as thecursoroption passed to prisma.defaultSize: (default: 20) The default page size to use iffirstandlastare not provided.maxSize: (default: 100) The maximum number of nodes returned for a connection.resolve: Like the resolver forprismaField, the first argument is aqueryobject that should be spread into your prisma query. Theresolvefunction should return an array of nodes for the connection. Thequerywill contain the correcttake,skip, andcursoroptions based on the connection arguments (before,after,first,last), along withincludeoptions for nested selections.totalCount: A function for loading the total count for the connection. This will add atotalCountfield to the connection object. ThetotalCountmethod 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, orbeforefirstandbeforelastandafter
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.
relatedConnection
The relatedConnection method can be used to create a relay connection field based on a relation
of the current model.
options
cursor: a@uniquecolumn of the model being connected to. This is used as thecursoroption passed to prisma.defaultSize: (default: 20) The default page size to use iffirstandlastare not provided.maxSize: (default: 100) The maximum number of nodes returned for a connection.query: A method that accepts theargsandcontextfor 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 atotalCountfield to the connection object. seerelationCountabove for more details. Note that this will not work when using a shared connection object (see details below)
Indirect relations as connections
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:
Sharing Connections objects
You can create reusable connection objects by using builder.connectionObject.
These connection objects can be used with t.prismaConnection, t.relatedConnection, or
t.connection
Shared edges can also be created using t.edgeObject
Extending connection edges
In some cases you may want to expose some data from an indirect connection on the edge object.
Total count on shared connection objects
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:
parsePrismaCursor and formatPrismaCursor
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.