Prisma objects
Define GraphQL object types from Prisma models with prismaObject and resolve to them with prismaField.
builder.prismaObject defines a GraphQL object type backed by a Prisma model. You pass the model name and a set of options that mirror any other object type. The difference is that Pothos already knows the shape from your generated PrismaTypes, so exposing columns and adding relations is fully typed without object refs or imports from the client.
builder.prismaObject('Team', {
fields: (t) => ({
id: t.exposeID('id'),
name: t.exposeString('name'),
}),
});
builder.prismaObject('Player', {
fields: (t) => ({
id: t.exposeID('id'),
name: t.exposeString('name'),
number: t.exposeInt('number'),
}),
});These behave like any Pothos object type. prismaObject returns an object ref you can use anywhere a ref is expected. Unlike a plain objectRef, the type information comes from your schema, and relation fields on it can be query-planned.
Naming the type
The GraphQL type takes the model's name by default. Pass name to call it something else, which is handy when one model backs more than one GraphQL type:
builder.prismaObject('Team', {
// The GraphQL type is `Roster`, still backed by the Team model.
name: 'Roster',
fields: (t) => ({
id: t.exposeID('id'),
name: t.exposeString('name'),
}),
});Resolving to a Prisma type
Use t.prismaField to add a field whose type is a Prisma model, most often on Query or Mutation:
builder.queryType({
fields: (t) => ({
myTeam: t.prismaField({
type: 'Team',
resolve: async (query, _root, _args, ctx) =>
prisma.team.findUniqueOrThrow({
...query,
where: { id: ctx.teamId },
}),
}),
}),
});t.prismaField works like t.field with two differences:
typeis a Prisma model name:'Team', or['Team']for a list field.resolvegets an extra first argument,query. Spread it into your Prisma call. It carries theinclude/selectthe plugin computed for the nested part of the request, so relations and selected columns load in the same query. Which fields end up in it depends on what the client selected and how you defined the fields and types involved.
You are not required to use t.prismaField (a prismaObject ref works with a plain t.field too), but only t.prismaField (and t.prismaConnection) gives you the query argument that makes the loading efficient.
Extending a Prisma object
The usual builder.objectField and builder.objectFields work on Prisma objects, but they can't use selections or expose fields outside the default selection. To add a field that pulls in extra columns or relations, use builder.prismaObjectField or builder.prismaObjectFields instead:
builder.prismaObjectField('Team', 'playerCount', (t) =>
t.relationCount('players'),
);