Skip to main content

GraphQL

GraphQL 是 API 的查询语言。它通常用作 RESTful API 的替代方案,但也可以用作现有 RESTful 服务之上的附加 "gateway" 层。

¥GraphQL is a query language for APIs. It is often used as an alternative to RESTful APIs, but can also be used as an additional "gateway" layer on top of existing RESTful services.

使用 Prisma ORM,你可以构建连接到数据库的 GraphQL 服务器。Prisma ORM 与你使用的 GraphQL 工具完全无关。构建 GraphQL 服务器时,你可以将 Prisma ORM 与 Apollo Server、GraphQL Yoga、TypeGraphQL、GraphQL.js 等工具或你在 GraphQL 服务器设置中使用的几乎任何工具或库结合起来。

¥With Prisma ORM, you can build GraphQL servers that connect to a database. Prisma ORM is completely agnostic to the GraphQL tools you use. When building a GraphQL server, you can combine Prisma ORM with tools like Apollo Server, GraphQL Yoga, TypeGraphQL, GraphQL.js, or pretty much any tool or library that you're using in your GraphQL server setup.

GraphQL 服务器的底层

¥GraphQL servers under the hood

GraphQL 服务器由两个主要组件组成:

¥A GraphQL server consists of two major components:

  • GraphQL 架构(类型定义 + 解析器)

    ¥GraphQL schema (type definitions + resolvers)

  • HTTP 服务器

    ¥HTTP server

请注意,GraphQL 架构可以采用代码优先或 SDL 优先的方式编写。查看此 article 以了解有关这两种方法的更多信息。如果你喜欢 SDL 优先方法,但仍希望使代码类型安全,请查看 GraphQL 代码生成器 以生成基于 SDL 的各种类型定义。

¥Note that a GraphQL schema can be written code-first or SDL-first. Check out this article to learn more about these two approaches. If you like the SDL-first approach but still want to make your code type-safe, check out GraphQL Code Generator to generate various type definitions based on SDL.

GraphQL 架构和 HTTP 服务器通常由单独的库处理。以下是当前 GraphQL 服务器工具及其用途的概述:

¥The GraphQL schema and HTTP server are typically handled by separate libraries. Here is an overview of current GraphQL server tools and their purpose:

库(npm 包)目的与 Prisma ORM 兼容Prisma 集成
graphqlGraphQL 架构(代码优先)是的
graphql-toolsGraphQL 架构(SDL 优先)是的
type-graphqlGraphQL 架构(代码优先)是的typegraphql-prisma
nexusGraphQL 架构(代码优先)是的nexus-prisma 早期预览
apollo-serverHTTP 服务器是的不适用
express-graphqlHTTP 服务器是的不适用
fastify-gqlHTTP 服务器是的不适用
graphql-yogaHTTP 服务器是的不适用

除了这些独立和单一用途的库之外,还有几个构建集成应用框架的项目:

¥In addition to these standalone and single-purpose libraries, there are several projects building integrated application frameworks:

框架由 __ 构建Prisma ORM描述
Redwood.js全栈汤姆·普雷斯顿-维尔纳构建于 Prisma ORM 之上将全栈引入 JAMstack。

注意:如果你发现列表中缺少任何 GraphQL 库/框架,请告诉我们。

¥Note: If you notice any GraphQL libraries/frameworks missing from the list, please let us know.

Prisma ORM 和 GraphQL 示例

¥Prisma ORM & GraphQL examples

在下一节中,我们将找到几个可立即运行的示例,展示如何将 Prisma ORM 与上表中提到的工具的不同组合结合使用。

¥In the following section will find several ready-to-run examples that showcase how to use Prisma ORM with different combinations of the tools mentioned in the table above.

示例HTTP 服务器GraphQL 架构描述
GraphQL API (Pothos)graphql-yogapothos基于 graphql-yoga 的 GraphQL 服务器
GraphQL API(SDL 优先)graphql-yoga不适用基于 SDL 优先方法的 GraphQL 服务器
GraphQL API - NestJs@nestjs/apollo不适用基于 NestJS 的 GraphQL 服务器
GraphQL API - NestJs(SDL 优先)@nestjs/apollo不适用基于 NestJS 的 GraphQL 服务器
GraphQL API (Nexus)@apollo/servernexus基于 @apollo/server 的 GraphQL 服务器
GraphQL API(类型 GraphQL)apollo-servertype-graphql基于 类型 GraphQL 代码优先方法的 GraphQL 服务器
GraphQL API(授权)apollo-servernexus具有电子邮件密码身份验证和权限的 GraphQL 服务器
全栈应用graphql-yogapothos带有 Next.js (React)、Apollo 客户端、GraphQL Yoga 和 Pothos 的全栈应用
GraphQL 订阅apollo-servernexusGraphQL 服务器实现实时 GraphQL 订阅
GraphQL API - Hapiapollo-server-hapinexus基于 Hapi 的 GraphQL 服务器
GraphQL API - Hapi(SDL 优先)apollo-server-hapigraphql-tools基于 Hapi 的 GraphQL 服务器
GraphQL API - Fastifyfastifymercurius不适用基于 FastifyMercurius 的 GraphQL 服务器
GraphQL API - Fastify(SDL 优先)fastifyNexus基于 FastifyMercurius 的 GraphQL 服务器

常见问题

¥FAQ

Prisma ORM 在 GraphQL 服务器中的作用是什么?

¥What is Prisma ORM's role in a GraphQL server?

无论你使用上述哪种 GraphQL 工具/库,Prisma ORM 都会在你的 GraphQL 解析器中使用来连接到你的数据库。它与任何其他 ORM 或 SQL 查询构建器在解析器中具有相同的角色。

¥No matter which of the above GraphQL tools/libraries you use, Prisma ORM is used inside your GraphQL resolvers to connect to your database. It has the same role that any other ORM or SQL query builder would have inside your resolvers.

在 GraphQL 查询的解析器中,Prisma ORM 通常从数据库读取数据并在 GraphQL 响应中返回数据。在 GraphQL 突变的解析器中,Prisma ORM 通常还会将数据写入数据库(例如创建新记录或更新现有记录)。

¥In the resolver of a GraphQL query, Prisma ORM typically reads data from the database to return it in the GraphQL response. In the resolver of a GraphQL mutation, Prisma ORM typically also writes data to the database (e.g. creating new or updating existing records).

其他 GraphQL 资源

¥Other GraphQL Resources

Prisma 策划了 GraphQL 周刊,这是一份时事通讯,重点介绍 GraphQL 社区的资源和更新。订阅以了解最新的 GraphQL 文章、视频、教程、库等。

¥Prisma curates GraphQL Weekly, a newsletter highlighting resources and updates from the GraphQL community. Subscribe to keep up-to-date with GraphQL articles, videos, tutorials, libraries, and more.