prisma orm_Prisma与Node.js-第2部分:Prisma绑定

news/2024/7/3 4:45:37

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为我们生成的所有超能力。

先决条件 (Prerequisites)

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.

我将假设您已经对使用突变和订阅以及创建模式和解析器已经感到满意。

查询 (Queries)

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上访问它。 您提出的每个请求都将需要两个参数,即查询参数(如datawhere )和用户所需的属性。 如果只需要一个,则可以在其位置传递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的请求将失败:

resolvers.js
resolvers.js
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.

传递相似的wheredata ,但不必在字符串中。

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

schema.js
schema.js
type Query {
  users(id: ID): [User!]!
  user(id: ID!): User!
}

type User {
  id: ID! 
  name: String!
}

变异 (Mutations)

First you’re going to need to add our exported mutations to our server’s resolvers in index.js.

首先,您需要在index.js中将导出的变异添加到服务器的解析器中。

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方法。

resolvers.js
resolvers.js
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
  }
}
schema.graphql
schema.graphql
type Mutation {
  addUser(data: CreateUserInput): User!
}

input CreateUserInput {
  name: String!
}

订阅内容 (Subscriptions)

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方法,用于对其进行测试。

index.js
index.js
import { Query, Mutation, Subscription } from './resolvers'

const server = new GraphQLServer({
  typeDefs: './schema.graphql',
  resolvers: {
    Query,
    Mutation,
    Subscription
  },
  context: { prisma }
})
schema.graphql
schema.graphql
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的用户。

resolvers.js
resolvers.js
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)
  }
}

总结思想 (Closing Thoughts)

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


http://www.niftyadmin.cn/n/3649533.html

相关文章

华为云鲲鹏云服务器安装Nginx

▣ 博主主站地址:微笑涛声 【www.cztcms.cn】 ▣ 博主其他平台: CSDN 简书 开源中国 思否 华为云博客 华为云鲲鹏云服务器搭载的是华为鲲鹏处理器(916/920),华为鲲鹏处理器是基于ARM架构的处理器,不同于传统的X86架构…

几种常见的设计模式

--什么是设计模式?设计模式有哪几种分类?每类模式重点解决什么问题? 设计模式:是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。 …

使用Visual Studio Code调试Go代码

This article will go over how to debug your Go code, so it goes without saying that a prerequisite for this article is that you understand the basics of writing Go. 本文将介绍如何调试Go代码,因此无需多说,本文的前提条件是您了解编写Go的基…

[Wap]command和selectionList冲突的分析

[Wap]command和selectionList冲突的分析编写者日期关键词郑昀ultrapower2005-7-14Wap ASP.NET Opera Openwave M3Gate MME3.0现象我们的Wap页面由dotNET 编写而成,一个页面上使用了mobile:command和mobile:selectionlist控件。当使用Opera、Microsoft Mobile Explor…

建站分享:调整WordPress自带标签云参数

▣ 博主主站地址:微笑涛声 【www.cztcms.cn】 ▣ 博主其他平台: CSDN 简书 开源中国 思否 华为云博客 WordPress 自带的标签云是一个很实用的小工具。站长可以通过标签对具有相同关健词的文章进行检索分类,利于访客查找相关文章。 WordPress标签默认显…

golang定义一个方法_在Go中定义方法

golang定义一个方法介绍 (Introduction) Functions allow you to organize logic into repeatable procedures that can use different arguments each time they run. In the course of defining functions, you’ll often find that multiple functions might operate on the…

vue中的突变方法在哪_了解GraphQL中的突变

vue中的突变方法在哪This is a continuation of a series on GraphQL Actions. The previous tutorial explored GraphQL Queries. This time, we will take a closer look at Mutations. There’s a lot of similarities in the way GraphQL Queries and Mutations work, howe…

Android:ViewPager制作幻灯片

最近在项目中用到图片轮播&#xff0c;试了Gallery&#xff0c;ViewFlipper&#xff0c;ViewPager,感觉Gallery最符合需求&#xff0c;但是Gallery的系统边框很难看&#xff0c;项目中要求用自己的背景图片。接下来用viewpager来做幻灯片效果。 <?xml version"1.0&quo…