GraphQL 的其他属性 - Amazon AppSync
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

GraphQL 的其他属性

GraphQL 由多种设计原则组成,以在大规模系统中保持简单性和稳健性。

声明性

GraphQL 是声明性的,这意味着用户仅声明他们希望查询的字段以描述数据(设置形状)。响应仅返回这些属性的数据。例如,以下操作检索 DynamoDB 表中的 Book 对象,其 ISBN 13 id 值为 9780199536061

{ getBook(id: "9780199536061") { name year author } }

响应返回负载中的字段(nameyearauthor),而不返回任何其他内容:

{ "data": { "getBook": { "name": "Anna Karenina", "year": "1878", "author": "Leo Tolstoy", } } }

由于这一设计原则,GraphQL 消除了 REST API 在复杂系统中经常遇到的过度获取和获取不足问题。这可以提高数据收集效率并提高网络性能。

分层

GraphQL 非常灵活,用户可以设置请求的数据形状以满足应用程序需求。请求的数据始终遵循 GraphQL API 中定义的属性的类型和语法。例如,以下代码片段显示具有名为 quotes 的新字段范围的 getBook 操作,该操作返回与 Book 9780199536061 关联的所有存储的引用字符串和页面:

{ getBook(id: "9780199536061") { name year author quotes { description page } } }

运行该查询将返回以下结果:

{ "data": { "getBook": { "name": "Anna Karenina", "year": "1878", "author": "Leo Tolstoy", "quotes": [ { "description": "The highest Petersburg society is essentially one: in it everyone knows everyone else, everyone even visits everyone else.", "page": 135 }, { "description": "Happy families are all alike; every unhappy family is unhappy in its own way.", "page": 1 }, { "description": "To Konstantin, the peasant was simply the chief partner in their common labor.", "page": 251 } ] } } }

正如您看到的一样,与请求的书籍关联的 quotes 字段以数组形式返回,其格式与查询描述的格式相同。尽管此处未显示,但 GraphQL 具有额外的优势,即不特别关注它检索的数据的位置。Booksquotes 可以是单独存储的,但只要存在关联,GraphQL 仍会检索该信息。这意味着,您的查询可以在单个请求中检索大量单独的数据。

内省

GraphQL 是自说明性的,或者说是内省的。它支持多种内置操作,以使用户能够查看架构中的基本类型和字段。例如,以下是具有 datedescription 字段的 Foo 类型:

type Foo { date: String description: String }

我们可以使用 _type 操作在架构下面查找类型元数据:

{ __type(name: "Foo") { name # returns the name of the type fields { # returns all fields in the type name # returns the name of each field type { # returns all types for each field name # returns the scalar type } } } }

这会返回一个响应:

{ "__type": { "name": "Foo", # The type name "fields": [ { "name": "date", # The date field "type": { "name": "String" } # The date's type }, { "name": "description", # The description field "type": { "name": "String" } # The description's type }, ] } }

该功能可用于找出特定 GraphQL 架构支持哪些类型和字段。GraphQL 支持各种这样的内省操作。有关更多信息,请参阅内省

强类型

GraphQL 通过其类型和字段系统支持强类型。在架构中定义某些内容时,它必须具有可以在运行时之前验证的类型。它还必须遵循 GraphQL 的语法规范。这个概念与其他语言的编程没有什么不同。例如,以下是以前的 Foo 类型:

type Foo { date: String description: String }

我们可以看到 Foo 是将创建的对象。在 Foo 实例中,具有一个 datedescription 字段,它们都是 String 基元类型(标量)。从语法上讲,我们看到已声明 Foo,并且它的字段位于其范围内。这种类型检查和逻辑语法的组合确保您的 GraphQL API 简洁且不言自明。可以在此处找到 GraphQL 的类型和语法规范。