GiraphQL
Search…
Prisma
This plugin provides tighter integration with prisma, making it easier to define prisma bioased object types, and helps solve n+1 queries for relations. It also has integrations for the relay plugin to make defining nodes and connections easy and efficient.

Disclaimers

This plugin is experimental, and will have some breaking changes in the future. DO NOT USE this plugin unless you are willing to deal with breaking changes in upcoming versions. This plugin may introduce BREAKING changes in minor versions until it's major version has been increased above 0.
This plugin is NOT required to build graphs backed by prisma models, and I would not recommend using it unless you have a solid understanding of how it will construct queries.
This plugin will allow common queries to be resolved through a single prisma query (prisma may still turn this into multiple SQL queries), and provides reasonable, predictable and safe fallbacks for more complex queries and edge cases. That being said, graphql APIs are complex, and it is important to understand the queries your API is capable of executing.
The way this plugin resolves queries is designed to be efficient, while still being predictable and easy to understand. Tools that try to automatically generate queries are often hard to understand and reason about, so this plugin tries to make things as clear as possible by providing query options to resolvers and a loading user code to initiate the actual queries. The options generally only contain includes for nested relations (connection fields provide more complex query options). The exception to this, is that we provide a default resolver for relations that can handle querying for a relation if data was not pre-loaded by a parent field. This query used by this resolver is simple, and described in detail below.
With this simple approach, we get an API that is easy to understand, but still provides a lot of value and functionality.

Example

Here is a quick example of what an API using this plugin might look like. There is a more thorough breakdown of what the methods and options used in the example below.
If you are looking for an example integrated with the relay plugin, see the Relay integration section below.
1
builder.prismaObject('User', {
2
include: {
3
profile: true,
4
},
5
findUnique: (user) => ({ id: user.id }),
6
fields: (t) => ({
7
id: t.exposeID('id'),
8
email: t.exposeString('email'),
9
bio: t.string({
10
resolve: user => user.profile.bio,
11
}),
12
posts: t.relation('posts', {
13
args: {
14
oldestFirst: t.arg.boolean(),
15
},
16
query: (args, context) => ({
17
orderBy: {
18
createdAt: args.oldestFirst ? 'asc' : 'desc',
19
},
20
}),
21
}),
22
}),
23
});
24
25
builder.prismaObject('Post', {
26
findUnique: (post) => ({ id: post.id }),
27
fields: (t) => ({
28
id: t.exposeID('id'),
29
title: t.exposeString('title'),
30
author: t.relation('author'),
31
}),
32
});
33
34
builder.queryType({
35
fields: (t) => ({
36
me: t.prismaField({
37
type: 'User',
38
resolve: async (query, root, args, ctx, info) =>
39
prisma.user.findUnique({
40
...query,
41
rejectOnNotFound: true,
42
where: { id: ctx.userId },
43
}),
44
}),
45
}),
46
});
Copied!
Given this schema, you would be able to resolve a query like the following with a single prisma query (which will still result in a few optimized SQL queries).
1
query {
2
me {
3
email
4
posts {
5
title
6
author {
7
id
8
}
9
}
10
}
11
}
Copied!
A query like
1
query {
2
me {
3
email
4
posts {
5
title
6
author {
7
id
8
}
9
}
10
oldPosts: posts(oldestFirst: true) {
11
title
12
author {
13
id
14
}
15
}
16
}
17
}
Copied!
Will result in 2 calls to prisma, one to resolve everything except oldPosts, and a second to resolve everything inside oldPosts. Prisma can only resolve each relation once in a single query, so we need a separate to handle the second posts relation. This may seem slightly magical, but should be predictable and hopefully easy to understand after reading the documentation below.

GiraphQL + Prisma without a plugin

If you just want learn about the plugin, feel free to skip this section, but understanding how to use prisma without a plugin may be useful for evaluating if this plugin is a good fit for your use case.
Using prisma without a plugin is relatively straight forward using the builder.objectRef method.
The easiest way to create types backed by prisma looks something like:
1
import { Post, PrismaClient, User } from '@prisma/client';
2
3
const db = new PrismaClient();
4
const UserObject = builder.objectRef<User>('User');
5
const PostObject = builder.objectRef<Post>('Post');
6
7
UserObject.implement({
8
fields: (t) => ({
9
id: t.exposeID('id'),
10
email: t.exposeString('email'),
11
posts: t.field({
12
type: [PostObject],
13
resolve: (user) =>
14
db.post.findMany({
15
where: { authorId: user.id },
16
}),
17
}),
18
}),
19
});
20
21
PostObject.implement({
22
fields: (t) => ({
23
id: t.exposeID('id'),
24
title: t.exposeString('title'),
25
author: t.field({
26
type: UserObject,
27
resolve: (post) =>
28
db.user.findUnique({ rejectOnNotFound: true, where: { id: post.authorId } }),
29
}),
30
}),
31
});
32
33
builder.queryType({
34
fields: (t) => ({
35
me: t.field({
36
type: UserObject,
37
resolve: (root, args, ctx) =>
38
db.user.findUnique({ rejectOnNotFound: true, where: { id: ctx.userId } }),
39
}),
40
}),
41
});
Copied!
This sets up User, and Post objects with a few fields, and a me query that returns the current user. There are a few things to note in this setup:
  1. 1.
    We split up the builder.objectRef and the implement calls, rather than calling builder.objectRef(...).implement(...). This prevents typescript from getting tripped up by the circular references between posts and users.
  2. 2.
    We use rejectOnNotFound with our findUnique calls because those fields are not nullable. Without this option, prisma will return a null if the object is not found. An alternative is to mark these fields as nullable.
  3. 3.
    The refs to our object types are called UserObject and PostObject, this is because User and Post are the names of the types imported from prisma. We could instead alias the types when we import them so we can name the refs to our GraphQL types after the models.
This setup is fairly simple, but it is easy to see the n+1 issues we might run into. Prisma helps with this by batching queries together, but there are also things we can do in our implementation to improve things.
One thing we could do if we know we will usually be loading the author any time we load a post is to make the author part of shape required for a post:
1
const UserObject = builder.objectRef<User>('User');
2
// We add the author here in the objectRef
3
const PostObject = builder.objectRef<Post & { author: User }>('Post');
4
5
UserObject.implement({
6
fields: (t) => ({
7
id: t.exposeID('id'),
8
email: t.exposeString('email'),
9
posts: t.field({
10
type: [PostObject],
11
resolve: (user) =>
12
db.post.findMany({
13
// We now need to include the author when we query for posts
14
include: {
15
author: true,
16
},
17
where: { authorId: user.id },
18
}),
19
}),
20
}),
21
});
22
23
PostObject.implement({
24
fields: (t) => ({
25
id: t.exposeID('id'),
26
title: t.exposeString('title'),
27
author: t.field({
28
type: UserObject,
29
// Now we can just return the author from the post instead of querying for it
30
resolve: (post) => post.author,
31
}),
32
}),
33
});
Copied!
We may not always want to query for the author though, so we could make the author optional and fall back to using a query if it was not provided by the parent resolver:
1
const PostObject = builder.objectRef<Post & { author?: User }>('Post');
2
3
PostObject.implement({
4
fields: (t) => ({
5
id: t.exposeID('id'),
6
title: t.exposeString('title'),
7
author: t.field({
8
type: UserObject,
9
resolve: (post) =>
10
post.author ?? db.user.findUnique({ rejectOnNotFound: true, where: { id: post.authorId } }),
11
}),
12
}),
13
});
Copied!
With this setup, a parent resolver has the option to include the author, but we have a fallback incase it does not.
There are other patterns like dataloaders than can be used to reduce n+1 issues, and make your graph more efficient, but they are too complex to describe here.

Usage

Install

1
yarn add @giraphql/plugin-prisma
Copied!

Setup

This plugin requires a little more setup than other plugins because it integrates with the prisma to generate some types that help the plugin better understand your prisma schema. Previous versions of this plugin used to infer all required types from the prisma client itself, but this resulted in a poor dev experience because the complex types slowed down editors, and some more advanced use cases could not be typed correctly.

Add a the giraphql generator to your prisma schema

1
generator giraphql {
2
provider = "prisma-giraphql-types"
3
}
Copied!
Now the types giraphql uses will be generated whenever you re-generate your prisma client. Run the following command to re-generate the client and create the new types:
1
npx prisma generate
Copied!
additional options:
  • clientOutput: Where the generated code will import the PrismaClient from. The default is the full path of wherever the client is generated. If you are checking in the generated file, using @prisma/client is a good option.
  • output: Where to write the generated types
Example with more options:
1
generator giraphql {
2
provider = "prisma-giraphql-types"
3
clientOutput = "@prisma/client"
4
output = "./giraphql-types.ts"
5
}
Copied!

Set up the builder

1
import SchemaBuilder from '@giraphql/core';
2
import { PrismaClient } from '@prisma/client';
3
import PrismaPlugin from '@giraphql/plugin-prisma';
4
// This is the default location for the generator, but this can be customized as described above
5
// Using a type only import will help avoid issues with undeclared exports in esm mode
6
import type PrismaTypes from '@giraphql/plugin-prisma/generated';
7
8
const prisma = new PrismaClient({});
9
10
const builder = new SchemaBuilder<{
11
PrismaTypes: PrismaTypes;
12
}>({
13
plugins: [PrismaPlugin],
14
prisma: {
15
client: prisma,
16
},
17
});
Copied!
It is strongly recommended NOT to put your prisma client into Context. This will result in slower type-checking and a laggy developer experience in VSCode. See https://github.com/microsoft/TypeScript/issues/45405 for more details.

Creating some types with builder.prismaObject

builder.prismaObject takes 2 arguments:
  1. 1.
    name: The name of the prisma model this new type represents
  2. 2.
    options: options for the type being created, this is very similar to the options for any other object type
1
builder.prismaObject('User', {
2
// Optional name for the object, defaults to the name of the prisma model
3
name: 'PostAuthor',
4
findUnique: null,
5
fields: (t) => ({
6
id: t.exposeID('id'),
7
email: t.exposeString('email'),
8
}),
9
});
10
11
builder.prismaObject('Post', {
12
findUnique: null,
13
fields: (t) => ({
14
id: t.exposeID('id'),
15
title: t.exposeString('title'),
16
}),
17
});
Copied!
So far, this is just creating some simple object types. They work just like any other object type in GiraphQL. They main advantage of this is that we get the type information without using object refs, or needing imports from prisma client.
The findUnique option is described more below.

Adding prisma fields to non-prisma objects (including Query and Mutation)

There is a new t.prismaField method which can be used to define fields that resolve to your prisma types:
1
builder.queryType({
2
fields: (t) => ({
3
me: t.prismaField({
4
type: 'User',
5
resolve: async (query, root, args, ctx, info) =>
6
prisma.user.findUnique({
7
...query,
8
rejectOnNotFound: true,
9
where: { id: ctx.userId },
10
}),
11
}),
12
}),
13
});
Copied!
This method works just like th normal t.field method with a couple of differences:
  1. 1.
    The type option must contain the name of the prisma model (eg. User or [User] for a list field).
  2. 2.
    The resolve function has a new first argument query which should be spread into query prisma query. This will be used to load data for nested relationships.
You do not need to use this method, and the builder.prismaObject method returns an object ref than can be used like any other object ref (with t.field), but using t.prismaField will allow you to take advantage of more efficient queries.
The query object will contain an include object to pre-load data needed to resolve nested parts of the current query. This is based on fields defined with t.relation described below.
If there are no fields using t.relation in your query, everything is resolved exactly as it would be without this plugin.

Adding relations

You can add fields for relations using the t.relation method:
1
builder.prismaObject('User', {
2
findUnique: null,
3
fields: (t) => ({
4
id: t.exposeID('id'),
5
email: t.exposeString('email'),
6
posts: t.relation('posts', {
7
resolve: (query, user) =>
8
db.post.findMany({
9
...query,
10
where: { authorId: user.id },
11
}),
12
}),
13
}),
14
});
15
16
builder.prismaObject('User', {
17
findUnique: null,
18
fields: (t) => ({
19
id: t.exposeID('id'),
20
title: t.exposeString('title'),
21
author: t.relation('author', {
22
resolve: (query, post) =>
23
db.user.findUnique({ ...query, rejectOnNotFound: true, where: { id: post.authorId } }),
24
}),
25
}),
26
});
Copied!
t.relation defines a field that can be pre-loaded by a parent resolver, and most of the time, the resolve function will NOT be called. This is VERY IMPORTANT to understand, because it is the biggest place where you can introduce inconsistencies into your API with this plugin. The resolve function is used to load the relationship if parent resolver did not pre-load the data needed to resolve the field. This happens for a number of reasons:
  1. 1.
    The parent object was not loaded through a field defined with t.prismaField, or t.relation
  2. 2.
    The query object for the parent field was not spread into the query
  3. 3.
    The graphql query requested multiple fields that depended on the same relationship (described more below)
These are all okay, and expected situations. Graphql APIs are very flexible, and magically pushing everything into a single query is impossible for arbitrary queries. This is why we have a resolve function than can load the relation IF it was not already loaded by the parent.
Like t.prismaField, the resolve function now as a new first argument that is a query that should be spread into the query, and is used to load nested relationships.

Find Unique

Because the resolve function is only used as a fallback, it is harder to test, and if done incorrectly can introduce inconsistencies. While it shouldn't be too hard to get right, it might be better to avoid it entirely. To do this, we can let the Prisma plugin generate these resolve functions for you in a consistent and predictable way. We can do this by providing a findUnique option for our object type. Defining a findUnique that is not null, will make resolve optional.
1
builder.prismaObject('User', {
2
findUnique: (user) => ({ id: user.id }),
3
fields: (t) => ({
4
id: t.exposeID('id'),
5
email: t.exposeString('email'),
6
posts: t.relation('posts'),
7
}),
8
});
9
10
builder.prismaObject('Post', {
11
findUnique: (post) => ({ id: post.id }),
12
fields: (t) => ({
13
id: t.exposeID('id'),
14
title: t.exposeString('title'),
15
author: t.relation('author'),
16
}),
17
});
Copied!
This greatly simplifies our object types. In these cases, the fallback resolve functions will re-load the current object using the findUnique as the where clause, and then include the relation for the current field. This can produce a slightly less efficient query than a manual implementation because the parent object is re-loaded first, but it will batch multiple relationships into one query, and the findUnique queries should be very fast.
For example, if a User was loaded without pre-loading, and both a posts and a profile relation where requested, the generated prisma call would be something like:
1
prisma.user.findUnique({
2
rejectOnNotFound: true,
3
where: { id: user.id },
4
include: {
5
posts: true,
6
profile: true,
7
},
8
});
Copied!

Filters, Sorting, and arguments

So far we have been describing very simple queries without any arguments, filtering, or sorting. For t.prismaField definitions, you can add arguments to your field like normal, and pass them into your prisma query as needed. For t.relation the flow is slightly different because we need to make sure we are loading the right data if we are pre-loading data in a parent resolver. We do this by adding a query option to our field options.
1
builder.prismaObject('User', {
2
findUnique: (user) => ({ id: user.id }),
3
fields: (t) => ({
4
id: t.exposeID('id'),
5
email: t.exposeString('email'),
6
posts: t.relation('posts', {
7
// We can define arguments like any other field
8
args: {
9
oldestFirst: t.arg.boolean(),
10
},
11
// Then we can generate our query conditions based on the arguments
12
query: (args, context) => ({
13
orderBy: {
14
createdAt: args.oldestFirst ? 'asc' : 'desc',
15
},
16
}),
17
}),
18
}),
19
});
Copied!
This query will be part of the query that gets passed into the first argument of resolve function for t.relation and t.prismaField based fields, and include things like where, skip, take, orderBy, etc. The query function will be passed the arguments for the field, and the context for the current request. Because it is used for pre-loading data, and solving n+1 issues, it can not be passed the parent object because it may not be loaded yet.
If your field has a resolve method the generated query will be passed in as part of the first arg to your resolve function
1
builder.prismaObject('User', {
2
findUnique: null,
3
fields: (t) => ({
4
id: t.exposeID('id'),
5
email: t.exposeString('email'),
6
posts: t.relation('posts', {
7
// We can define arguments like any other field
8
args: {
9
oldestFirst: t.arg.boolean(),
10
},
11
// Then we can generate our query conditions based on the arguments
12
query: (args, context) => ({
13
orderBy: {
14
createdAt: args.oldestFirst ? 'asc' : 'desc',
15
},
16
}),
17
// query here will contain the orderBy (and any other properties returned by the query method)
18
resolve: (query, post) => db.post.findMany({ ...query, where: { id: post.authorId } }),
19
}),
20
}),
21
});
Copied!
It is VERY IMPORTANT to put all your filtering and sorting into the query method rather than your resolver because the resolver is only used as fallback, and any filtering that does not exist in the query method will not be applied correctly. If you have a where in both your query and your resolver, you will need to ensure these are merged correctly. It is generally better NOT to use a custom resolver.

Includes on types

In some cases, you may want to always pre-load certain relations. This can be helpful for defining fields directly on type where the underlying data may come from a related table.
1
builder.prismaObject('User', {
2
// This will always include the profile when a user object is loaded. Deeply nested relations can
3
// also be included this way.
4
include: {
5
profile: true,
6
},
7
findUnique: (user) => ({ id: user.id }),
8
fields: (t) => ({
9
id: t.exposeID('id'),
10
email: t.exposeString('email'),
11
bio: t.string({
12
// The profile relation will always be loaded, and user will now be typed to include the
13
// profile field so you can return the bio from the nested profile relation.
14
resolve: (user) => user.profile.bio,
15
}),
16
}),
17
});
Copied!

relationCount

Prisma supports querying for relation counts which allow including counts for relations along side other includes. This does not currently support any filters on the counts, but can give a total count for a relation.
1
builder.prismaObject('User', {
2
findUnique: (user) => ({ id: user.id }),
3
fields: (t) => ({
4
id: t.exposeID('id'),
5
postCount: t.relationCount('posts'),
6
}),
7
});
Copied!

Relay integration

This plugin has extensive integration with the relay plugin, which makes creating nodes and connections very easy.

Example

The following example is similar to the one above with a few changes:
  • the User and Post objects are now relay nodes
  • the posts field on the User type is now a relay connection using cursor based pagination
  • there is a new users query that is also a relay connection
Everything in this schema is still queryable via a single prisma query. The relay connections handles pre-loading like all the other fields.
1
builder.prismaNode('User', {
2
findUnique: (id) => ({ id }),
3
id: { resolve: (user) => user.id },
4
fields: (t) => ({
5
email: t.exposeString('email'),
6
posts: t.relatedConnection('posts', {
7
cursor: 'id',
8
args: {
9
oldestFirst: t.arg.boolean(),
10
},
11
query: (args, context) => ({
12
orderBy: {
13
createdAt: args.oldestFirst ? 'asc' : 'desc',
14
},
15
}),
16
}),
17
}),
18
});
19
20
builder.prismaNode('Post', {
21
findUnique: (id) => ({ id }),
22
id: { resolve: (post) => post.id },
23
fields: (t) => ({
24
title: t.exposeString('title'),
25
author: t.relation('author'),
26
}),
27
});
28
29
builder.queryType({
30
fields: (t) => ({
31
me: t.prismaField({
32
type: 'User',
33
resolve: async (query, root, args, ctx, info) =>
34
prisma.user.findUnique({
35
...query,
36
rejectOnNotFound: true,
37
where: { id: ctx.userId },
38
}),
39
}),
40
posts: t.prismaConnection({
41
type: 'Post',
42
cursor: 'id',
43
resolve: (query) => prisma.post.findMany(query),
44
}),
45
}),
46
});
Copied!

prismaNode

The prismaNode method works just like the prismaObject method with a couple of small differences:
  • the findUnique function now only receives an id. This is to support relays ability to load nodes by id.
  • there is a new id option that mirrors the id option from node method of the relay plugin, and must contain a resolve function that returns the id from an instance of the node.
1
builder.prismaNode('Post', {
2
// This is used to load the node by id
3
findUnique: (id) => ({ id }),
4
// This is used to get the id from a node
5
id: { resolve: (post) => post.id },
6
// fields work just like they do for builder.prismaObject
7
fields: (t) => ({
8
title: t.exposeString('title'),
9
author: t.relation('author'),
10
}),
11
});
Copied!

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.
1
builder.queryType({
2
fields: (t) => ({
3
posts: t.prismaConnection(
4
{
5
type: 'Post',
6
cursor: 'id',
7
resolve: (query, parent, args, context, info) => prisma.post.findMany({ ...query }),
8
}),
9
{}, // optional options for the Connection type
10
{}, // optional options for the Edge type),
11
),
12
}),
13
});
Copied!

options

  • 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.
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.

relatedConnection

The relatedConnection method can be used to create a relay connection field based on a relation of the current model.
1
builder.prismaNode('User', {
2
findUnique: (id) => ({ id }),
3
id: { resolve: (user) => user.id },
4
fields: (t) => ({
5
// Connections can be very simple to define
6
simplePosts: t.relatedConnection('posts', {
7
cursor: 'id',
8
}),
9
// Or they can include custom arguments, and other options
10
posts: t.relatedConnection(
11
'posts',
12
{
13
cursor: 'id',
14
args: {
15
oldestFirst: t.arg.boolean(),
16
},
17
query: (args, context) => ({
18
orderBy: {
19
createdAt: args.oldestFirst ? 'asc' : 'desc',
20
},
21
}),
22
},
23
{}, // optional options for the Connection type
24
{}, // optional options for the Edge type),
25
),
26
}),
27
});
Copied!

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.
  • resolve: (optional) Used as a fallback when a connection is not pre-loaded. It is optional, and generally should NOT be defined manually. If used it works like a combination of the resolve method of relation and prismaConnection. The default will use the findUnique of the current model, with an include for the current relation. It is also batched together with other relationships to improve query efficiency.
  • totalCount: when set to true, this will add a totalCount field to the connection object. see relationCount above for more details.
Last modified 4d ago