Prisma ORM unlocks a new level of developer experience when working with databases thanks to its intuitive data model, automated migrations, type-safety & auto-completion.
@gqloom/prisma
provides integration of GQLoom with Prisma:
You can find more information about installation in Prisma documentation.
Define your Prisma Schema in the prisma/schema.prisma
file:
generator
accepts the following arguments:
Arguments | Description | Default value |
---|---|---|
gqloomPath | Path to the GQLoom package. | @gqloom/prisma |
clientOutput | Path to the Prisma client. | node_modules/@prisma/client |
output | Folder path to the generated files. | node_modules/@gqloom/prisma/generated |
commonjsFile | File name for the CommonJS file. Use "" to disable. | index.cjs |
moduleFile | File name for the ES module file. Use "" to disable. | index.js |
typesFiles | File names for the TypeScript declaration files. Use [] to disable. | ["index.d.ts"] |
After generating the silk, we can use it in resolver
:
@gqloom/prisma
will expose all fields by default. If you wish to hide some fields, you can use field.hidden
:
In the code above, we have hidden the authorId
field, which means it will not appear in the generated GraphQL Schema.
@gqloom/prisma
provides PrismaModelBobbin
to help you create Bobbins.
Using Bobbins, you can quickly define commonly used queries, mutations, and fields. Bobbins also come preconfigured with input types for common operational inputs, and using Bobbins can greatly reduce sample code, which is very useful during quick development.
In the code above, we have created User
and Post
models for the Bobbin. The PrismaModelBobbin
accepts two parameters, the first being the model that will be used as the silk and the second being the PrismaClient
instance.
Bobbin provides the relationField
method to define a relationship field:
In the above code, we have used userBobbin.relationField('posts')
and postBobbin.relationField('author')
to define the relation fields.
The relationField
method accepts a string argument representing the name of the relationship field.
Bobbin is preset with common queries and you can use them directly:
In the above code, we use userBobbin.findUniqueQuery()
to define the user
query. Bobbin will automatically create the input type and resolving function.
Bobbin is preset with the following queries:
Bobbins are preset with common mutations and you can use them directly:
In the code above, we use postBobbin.createMutation()
to define the createPost
mutation. Bobbin will automatically create the input type and parser function.
The Bobbin is preset with the following mutations:
Bobbin preset queries and mutations support custom inputs, you can define the input type with the input
option:
In the code above, we used valibotSilk
to define the input type, v.object({ id: v.number( })
defines the type of the input object.
v.object({ id: v.number() })
defines the type of the input object.
asInputArgs()
to label this object as an input parameter for GraphQL.
v.transform(({ id }) => ({ where: { id } }))
transforms the input arguments into Prisma's query parameters.
Bobbin preconfigured queries, mutations and fields support adding middleware, which you can define with the middlewares
option:
In the above code, we define the middleware using the middlewares
option, the
async (next) => { ... }
defines a middleware.
useLoggedUser()
is a custom function to get the currently logged in user.
If the user is not logged in, an error is thrown, otherwise next()
is called to continue execution.
You can create a Resolver directly from a Bobbin:
In the above code, we have used userBobbin.resolver()
to create a Resolver.
This Resolver will contain all the queries, mutations and fields in the Bobbin.
To accommodate more Prisma types, we can extend GQLoom to add more type mappings.
First we use PrismaWeaver.config
to define the type mapping configuration. Here we import GraphQLDateTime
from graphql-scalars and when we encounter a DateTime
type, we map it to the corresponding GraphQL scalar.
Configurations are passed into the weave function when weaving the GraphQL Schema:
The following table lists the default mappings between Prisma types and GraphQL types in GQLoom:
Prisma types | GraphQL types |
---|---|
Int @id | GraphQLID |
String @id | GraphQLID |
BigInt | GraphQLInt |
Int | GraphQLInt |
Decimal | GraphQLFloat |
Float | GraphQLFloat |
Boolean | GraphQLBoolean |
DateTime | GraphQLString |
String | GraphQLString |