在 apollo-server 中将网关上的联合类型转换为服务 GraphQL 中的标量类型

Posted

技术标签:

【中文标题】在 apollo-server 中将网关上的联合类型转换为服务 GraphQL 中的标量类型【英文标题】:Transform Union Type on Gateway to Scalar Type in Service GraphQL in apollo-server 【发布时间】:2019-05-04 19:52:51 【问题描述】:

我的 Prisma 服务器上有一个类型定义为 Device

type Device 
  id: ID! @unique
  type: DeviceType
  settings: Json

但在我的网关上,我想将字段设置解析为实际的联合类型

# import Node from './generated/prisma.graphql'

type MobileSettings 
  firmware: String


type DesktopSettings 
  os: String


union Settings = MobileSettings | DesktopSettings

type Device implements Node 
  id: ID!
  type: DeviceType
  settings: Json


type Query 
  devices: [Device]
  node(id: ID!): Node

如果我在 Device.settings 字段上添加自定义解析器,获取所有设备的初始查询仍会尝试检索设置字段。

如何将 Json 字段解析为实际的联合类型?

【问题讨论】:

【参考方案1】:

我认为您忘记在架构中使用 Settings 类型:

# import Node from './generated/prisma.graphql'

type MobileSettings 
  firmware: String


type DesktopSettings 
  os: String


union Settings = MobileSettings | DesktopSettings

type Device implements Node 
  id: ID!
  type: DeviceType
  settings: Settings # <-- Here


type Query 
  devices: [Device]
  node(id: ID!): Node

【讨论】:

以上是关于在 apollo-server 中将网关上的联合类型转换为服务 GraphQL 中的标量类型的主要内容,如果未能解决你的问题,请参考以下文章