将网关上的联合类型转换为阿波罗服务器中服务GraphQL中的标量类型

时间:2018-12-03 13:44:17

标签: graphql apollo apollo-server prisma

我在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 个答案:

答案 0 :(得分:0)

我认为您忘记在架构中使用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
}