【问题标题】:How do I implement the @include or @skip directive in apollo server如何在 apollo 服务器中实现 @include 或 @skip 指令
【发布时间】:2021-03-08 22:29:51
【问题描述】:

我有一个正在试验的大型数据集。我正在尝试有条件地包含嵌套对象。 我的客户查询:

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 文档说他们支持它,但没有给出这些示例。

【问题讨论】:

    标签: express graphql apollo


    【解决方案1】:

    @skip@include 指令内置在 GraphQL specification itself 中。您无需在服务器端执行任何操作即可启用它们——它们只适用于任何符合规范的 GraphQL 服务器。

    如果@include 指令存在于字段或片段上并且if 值为false,那么 GraphQL 运行时将简单地将该字段或片段视为一开始就不存在(解析器不会被调用,并且字段将从返回给客户端的结果中省略)。当if 的值为true 时,@skip 指令也是如此。

    【讨论】:

    • 哎呀。因此,客户端没有发送布尔值似乎是一个问题......我将打开另一个问题。谢谢!
    猜你喜欢
    • 2021-11-01
    • 2019-05-28
    • 2011-12-14
    • 1970-01-01
    • 2019-05-09
    • 2019-04-13
    • 2011-07-04
    • 1970-01-01
    相关资源
    最近更新 更多