Indirect relations
Loading relations for fields that aren't t.relation
Use t.relation for a direct relation, including a renamed or filtered field.
Use t.field with a select when the field goes through a join model, wraps the rows in another
GraphQL type, or needs its own resolver logic.
A field's select function gets nestedSelection as its third argument. Use it as the value for
a relation, and the plugin loads that relation in the parent's query, with whatever the client
selects on the field's type:
const Post = builder.prismaObject('Post', {
fields: (t) => ({ title: t.exposeString('title') }),
});
builder.prismaObject('User', {
fields: (t) => ({
publishedPosts: t.field({
type: [Post],
select: (args, ctx, nestedSelection) => ({
posts: nestedSelection({ where: { published: 1 } }),
}),
resolve: (user) => user.posts,
}),
}),
});For this filtered field alone, prefer
publishedPosts: t.relation('posts', { query: { where: { published: 1 } } }).
With { posts: true } instead, the plugin only loads each post's columns. Anything selected
under the field, such as a post's author, needs an extra batched query. Use nestedSelection
whenever the field's type has relations or selects of its own.
nestedSelection takes an optional object with the same options as a
relation entry, plus path:
where,orderBy,limit,offset: the relation's query options. A to-one relation takes none.select: anything else to load for each row, on top of what the query selects.path: the path to a nested field to read the selection from. A segment can be written as{ name, type }to only read the field inside a fragment on that type.type: the type to read the selection as, when the field returns an interface or union. You can pass anything a field'stypetakes, or a model's name for its default type. The type's model must be the relation's target, and the query and rows are typed for that model.
The rows only include the primary key and the selected columns, not every column.
Selecting fields from a nested GraphQL field
When the field returns a plain object type that wraps the rows, pass the path to the field
inside it that returns the relation's type:
const PostPreview = builder.objectRef<typeof Post.$inferType>('PostPreview').implement({
fields: (t) => ({
post: t.field({ type: Post, resolve: (post) => post }),
}),
});
builder.prismaObject('User', {
fields: (t) => ({
postPreviews: t.field({
type: [PostPreview],
select: (args, ctx, nestedSelection) => ({
// Load the selection under `postPreviews.post` for the 2 newest posts
posts: nestedSelection({ orderBy: { createdAt: 'desc' }, limit: 2, path: ['post'] }),
}),
resolve: (user) => user.posts,
}),
}),
});Selecting as a specific type
When the field returns an interface or union, use type to pick the object type to read the
selection as. Fragments on other types are ignored:
const Comment = builder.prismaObject('Comment', {
// Loaded whenever a Comment is queried, so resolveType can read it
select: ['postId'],
fields: (t) => ({ body: t.exposeString('body') }),
});
const Activity = builder.unionType('Activity', {
types: [Post, Comment],
resolveType: (row) => ('postId' in row ? 'Comment' : 'Post'),
});
builder.prismaObject('User', {
fields: (t) => ({
recentActivity: t.field({
type: [Activity],
select: (args, ctx, nestedSelection) => ({
posts: nestedSelection({ limit: 5, type: Post }),
comments: nestedSelection({ limit: 5, type: Comment }),
}),
resolve: (user) => [...user.posts, ...user.comments],
}),
}),
});Join models
Relations defined with rel.manyToMany work with t.relation and nestedSelection like any
other to-many relation.
When the join table is its own model, give the relation to the join model a select, and use
nestedSelection inside it. Here comments join users to the posts they commented on:
builder.prismaObject('User', {
fields: (t) => ({
commentedPosts: t.field({
type: [Post],
select: (args, ctx, nestedSelection) => ({
comments: { orderBy: { createdAt: 'desc' }, select: { post: nestedSelection() } },
}),
resolve: (user) => user.comments.map((comment) => comment.post),
}),
}),
});Everything still loads in the parent's query. To load only one variant of a relation's model, add
type to the relation's entry, with the variant's ref or model name:
{ type: Bug, select: { reviewer: true } }.