如何在 apollo 服务器中实现 @include 或 @skip 指令
How do I implement the @include or @skip directive in apollo server
我正在试验一个大数据集。我正在尝试有条件地包含嵌套对象。
我的客户查询:
query getAllAlbums ($fetchPhotos: Boolean!){
albums{
id
title
tags{
...Tag
weight
}
carouselItems{
id,
mediaUrl
}
photos @include(if: $fetchPhotos){
...Photo
}
}
}
我第一次尝试在服务器端实现,我将它添加到架构中:
type Album @include{
id: String
title: String
tags: [Tag]
photos: [Photo] @include(if: fetchPhotos)
carouselItems: [Photo]
}
我收到验证错误:
Error: Directive "@include" may not be used on OBJECT.
Directive "@include" argument "if" of type "Boolean!" is required, but it was not provided.
Directive "@include" may not be used on FIELD_DEFINITION.
好吧,我不能在架构上使用它,那还剩下什么?解析器?它不在参数中...
我该如何实施? apollographql 文档说他们支持它,但没有给出这些示例。
@skip
和 @include
指令内置于 GraphQL specification itself 中。您无需在服务器端执行任何操作即可启用它们——它们将仅适用于任何符合规范的 GraphQL 服务器。
如果字段或片段上存在 @include
指令且 if
值为 false
,则 GraphQL 运行时将简单地处理该字段或片段,就好像它没有't 存在于首位(解析器不会被调用,字段(s)会从返回给客户端的结果中被忽略)。当 if
值为 true
.
时,这同样适用于 @skip
指令
我正在试验一个大数据集。我正在尝试有条件地包含嵌套对象。 我的客户查询:
query getAllAlbums ($fetchPhotos: Boolean!){
albums{
id
title
tags{
...Tag
weight
}
carouselItems{
id,
mediaUrl
}
photos @include(if: $fetchPhotos){
...Photo
}
}
}
我第一次尝试在服务器端实现,我将它添加到架构中:
type Album @include{
id: String
title: String
tags: [Tag]
photos: [Photo] @include(if: fetchPhotos)
carouselItems: [Photo]
}
我收到验证错误:
Error: Directive "@include" may not be used on OBJECT.
Directive "@include" argument "if" of type "Boolean!" is required, but it was not provided.
Directive "@include" may not be used on FIELD_DEFINITION.
好吧,我不能在架构上使用它,那还剩下什么?解析器?它不在参数中...
我该如何实施? apollographql 文档说他们支持它,但没有给出这些示例。
@skip
和 @include
指令内置于 GraphQL specification itself 中。您无需在服务器端执行任何操作即可启用它们——它们将仅适用于任何符合规范的 GraphQL 服务器。
如果字段或片段上存在 @include
指令且 if
值为 false
,则 GraphQL 运行时将简单地处理该字段或片段,就好像它没有't 存在于首位(解析器不会被调用,字段(s)会从返回给客户端的结果中被忽略)。当 if
值为 true
.
@skip
指令