prisma orm
Over in Part 1 we discussed how to connect our deployed Prisma API to our Node.js-based GraphQL server. Now we’re going to look into using the prisma-bindings library to start using all the superpowers that Prisma has generated for us.
在第1部分中,我们讨论了如何将已部署的Prisma API连接到基于Node.js的GraphQL服务器。 现在,我们将研究使用prisma-bindings库来开始使用Prisma为我们生成的所有超能力。
Obviously you’re going to need a basic Prisma API that’s connected to a database and your Node.js server. You can learn about setting up Prisma with a Postgres database here.
显然,您将需要一个连接数据库和Node.js服务器的基本Prisma API。 您可以在此处了解有关使用Postgres数据库设置Prisma的信息。
If you skipped part 1 and want to jump into the implementation, you can copy this boilerplate to get started, just remember to change the endpoints to your Docker container.
如果您跳过了第1部分,并且想跳到实现中,则可以复制此样板来开始使用,只记得将端点更改为Docker容器。
I will be assuming that you’re already comfortable with using mutations and subscriptions and creating schemas and resolvers.
我将假设您已经对使用突变和订阅以及创建模式和解析器已经感到满意。
Since we passed our prisma
object over to our server’s context in index.js
we can access it in our resolver context, which we’ll just destructure out.
由于我们将prisma
对象传递给index.js
中的服务器上下文,因此我们可以在解析器上下文中访问它,我们将对其进行分解。
Everything that is available to you in our Prisma container’s documentation is now available on our prisma
object. If we want to make a request for users
we can access it on prisma.query
. Every request you make will require two arguments, the query arguments, like data
or where
, and the properties that your user wants. If you only need one you can just pass null
in its place.
Prisma容器的文档中提供给您的所有内容现在都可以在prisma
对象上找到。 如果我们要向users
发出请求,则可以在prisma.query
上访问它。 您提出的每个请求都将需要两个参数,即查询参数(如data
或where
)和用户所需的属性。 如果只需要一个,则可以在其位置传递null
。
Everything we pass as the second argument, which needs to be an object in a string, will control what the user is allowed to query for. In this case name
will be available, but a request for their id
will fail:
我们作为第二个参数传递的所有内容(必须是字符串中的一个对象)都将控制允许用户查询的内容。 在这种情况下, name
将可用,但其id
的请求将失败:
const Query = {
users(parent, args, { prisma }, info) {
const users = prisma.query.users(null, '{ name }');
return users;
},
};
To make everything available to the user, we can pass-in info
since that contains the user’s full query.
为了使所有内容对用户可用,我们可以传递info
因为其中包含用户的完整查询。
Passing in where
or data
is similar, but it doesn’t need to be in a string.
传递相似的where
或data
,但不必在字符串中。
const Query = {
users(parent, args, { prisma }, info) {
const users = prisma.query.users({ where: { id: args.id } }, info)
return users
},
user(parent, args, { prisma }, info) {
return prisma.query.user({ where: { id: args.id } }, info)
}
};
Since we’re working with filters on our queries, we need to update our schema to allow for an id
to be passed-in.
由于我们正在使用查询过滤器,因此我们需要更新架构以允许传入id
。
type Query {
users(id: ID): [User!]!
user(id: ID!): User!
}
type User {
id: ID!
name: String!
}
First you’re going to need to add our exported mutations to our server’s resolvers in index.js
.
首先,您需要在index.js
中将导出的变异添加到服务器的解析器中。
import { Query, Mutation } from './resolvers'
const server = new GraphQLServer({
typeDefs: './schema.graphql',
resolvers: {
Query,
Mutation
},
context: { prisma }
})
Mutations are just as easy as queries, just access the method you want on prisma
and pass the arguments and what you want returned to the user. This time, let’s try chaining mutations together to create a user then update their name. For this we can use async/await and pass the id
from the new user to the update method.
变异就像查询一样容易,只需访问所需的prisma
分析方法并将参数和想要返回的内容传递给用户即可。 这次,让我们尝试将突变链接在一起以创建用户,然后更新其名称。 为此,我们可以使用async / await并将id
从新用户传递到update方法。
const Mutation = {
async addUser(parent, args, { prisma }, info) {
const newUser = await prisma.mutation.createUser({ data: args.data }, info)
const updatedUser = await prisma.mutation.updateUser({
where: { id: newUser.id },
data: { name: "Overridden" }
}, info)
return updatedUser
}
}
type Mutation {
addUser(data: CreateUserInput): User!
}
input CreateUserInput {
name: String!
}
For GraphQL subscriptions using Prisma we don’t even need to worry about pubsub or opening sockets, we can just use prisma
to use a subscription in one line.
对于使用Prisma的GraphQL订阅,我们甚至不必担心pubsub或打开套接字,我们可以只使用prisma
在一行中使用订阅。
First let’s add our Subscriptions
to our resolvers and setup our schema to support them. We’ll also be adding a smaller updateUser
method that we’ll use to test it with.
首先,让我们将Subscriptions
添加到解析器,并设置架构以支持它们。 我们还将添加一个较小的updateUser
方法,用于对其进行测试。
import { Query, Mutation, Subscription } from './resolvers'
const server = new GraphQLServer({
typeDefs: './schema.graphql',
resolvers: {
Query,
Mutation,
Subscription
},
context: { prisma }
})
type Mutation {
addUser(data: CreateUserInput): User!
updateUser(id: ID!, data: CreateUserInput): User!
}
enum MutationType {
CREATED
UPDATED
DELETED
}
type Subscription {
user(id: ID!): UserSubscriptionPayload!
}
type UserSubscriptionPayload {
mutation: MutationType!
node: User
}
Just like before, we can access a user subscription on prisma.subscription.user
, and set it to watch the user whose id
we’re going to pass to it.
像以前一样,我们可以在prisma.subscription.user
上访问用户订阅,并将其设置为监视我们将传递给其id
的用户。
const Subscription = {
user: {
subscribe(parent, args, { prisma }, info) {
return prisma.subscription.user({ where: { node: { id: args.id } } }, info)
}
}
}
const Mutation = {
async addUser(){...},
updateUser(parent, args, { prisma }, info) {
return prisma.mutation.updateUser({
where: { id: args.id },
data: args.data
}, info)
}
}
As you can see, Prisma made what was before a lot of menial tasks that had to be done every time we wanted to add a new type, and simplified them into many one line functions that are incredibly easy to use. While it still gives us the freedom to do what we need in between getting a request and working with the database.
如您所见,Prisma完成了每次我们要添加新类型时都必须完成的繁琐任务之前的工作,并将它们简化为许多易于使用的单行函数。 尽管它仍然使我们能够自由地在获取请求和使用数据库之间做所需的事情。
翻译自: https://www.digitalocean.com/community/tutorials/graphql-prisma-bindings
prisma orm