代码之家  ›  专栏  ›  技术社区  ›  Alx

将网关上的联合类型转换为apollo服务器中服务图形中的标量类型

  •  1
  • Alx  · 技术社区  · 6 年前

    Device 在我的Prisma服务器上

    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 回复  |  直到 6 年前
        1
  •  0
  •   Errorname    6 年前

    我想你忘了用 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
    }