GraphQL 介绍
¥Introduction to GraphQL
了解 GraphQL、它的工作原理以及如何使用它。正在寻找有关如何构建 GraphQL 服务的文档?有一些库可以帮助你在 许多不同的语言 中实现 GraphQL。有关实用教程的深入学习体验,请参阅 可用的培训课程。
¥Learn about GraphQL, how it works, and how to use it. Looking for documentation on how to build a GraphQL service? There are libraries to help you implement GraphQL in many different languages. For an in-depth learning experience with practical tutorials, see the available training courses.
GraphQL 是用于 API 的查询语言,也是使用你为数据定义的类型系统执行查询的服务器端运行时。GraphQL 不依赖于任何特定的数据库或存储引擎,而是由现有代码和数据支持。
¥GraphQL is a query language for your API, and a server-side runtime for executing queries using a type system you define for your data. GraphQL isn’t tied to any specific database or storage engine and is instead backed by your existing code and data.
GraphQL 服务是通过定义类型和这些类型上的字段,然后为每种类型上的每个字段提供函数来创建的。例如,告诉你登录用户是谁 (me
) 以及该用户名的 GraphQL 服务可能如下所示:
¥A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type.
For example, a GraphQL service that tells you who the logged in user is (me
) as well as that user’s name might look
like this:
type Query {
me: User
}
type User {
id: ID
name: String
}
以及每种类型的每个字段的函数:
¥Along with functions for each field on each type:
function Query_me(request) {
return request.auth.user
}
function User_name(user) {
return user.getName()
}
GraphQL 服务运行后(通常在 Web 服务上的 URL 上),它可以接收 GraphQL 查询以进行验证和执行。该服务首先检查查询以确保它仅引用定义的类型和字段,然后运行提供的函数以生成结果。
¥After a GraphQL service is running (typically at a URL on a web service), it can receive GraphQL queries to validate and execute. The service first checks a query to ensure it only refers to the types and fields defined, and then runs the provided functions to produce a result.
例如,查询:
¥For example, the query:
{
me {
name
}
}
可以产生以下 JSON 结果:
¥Could produce the following JSON result:
{
"me": {
"name": "Luke Skywalker"
}
}