了解 GraphQL 指令:Zalando 的实际用例

In GraphQL, if you've used the syntax that starts with an @, for example, @foo, then you've used GraphQL directives. Directives provide a way to extend the language features of GraphQL using a supported syntax. Certain directives are built into GraphQL, like @skip, @include, @deprecated, and @specifiedBy, and are supported by all GraphQL engines.

在 GraphQL 中,如果您使用以 @ 开头的语法,例如 @foo,那么您就使用了 GraphQL 指令。指令提供了一种使用支持的语法扩展 GraphQL 语言特性的方法。某些指令内置于 GraphQL 中,例如 @skip@include@deprecated@specifiedBy,并且受到所有 GraphQL 引擎的支持。

If we look closer, we can see that two of these directives (@skip and @include) are used only in the queries, and the other two (@deprecated and @specifiedBy) are used only in the schema. This is because GraphQL directives are defined for two different categories of locations - TypeSystem and ExecutableDefinition. The TypeSystem directives are defined for the schema, and the ExecutableDefinition directives are defined for the queries. We will discuss this in detail in the next section.

如果我们仔细观察,就会发现这些指令中的两个 (@skip@include) 仅在查询中使用,而另外两个 (@deprecated@specifiedBy) 仅在模式中使用。这是因为 GraphQL 指令针对两个不同的位置类别进行定义 - TypeSystemExecutableDefinitionTypeSystem 指令是为模式定义的,而 ExecutableDefinition 指令是为查询定义的。我们将在下一节中详细讨论这一点。

The query directives are generally useful for clients to express certain types of metadata for the query. The schema directives are generally useful for declaratively specifying common server-side behaviors, for example, authorization requirements, marking sensitive data, etc.

查询指令通常用于客户端表达查询的某些元数据。模式指令通常用于声明性地指定常见的服务器端行为,例如授权要求、标记敏感数据等。

Part 1: Schema directives at Zalando

第1部分:Zalando的模式指令

The schema directives refer to the directives defined for the TypeSystem locations. The type system directives are available for the locations listed below. Consider @foo a directive for the location mentioned in the 1st column.

模式指令是指针对 TypeSystem 位置定义的指令。类型系统指令可用于下面列出的位置。将 @foo 视为第一列中提到的位置的指令。

directive @foo on LOCATION_IN_FIRST_COLUMN

directive @foo on LOCATION_IN_FIRST_COLUMN

...
Directive Location Example
开通本站会员,查看完整译文。

首页 - Wiki
Copyright © 2011-2024 iteam. Current version is 2.137.1. UTC+08:00, 2024-11-22 19:52
浙ICP备14020137号-1 $访客地图$