首页 > 解决方案 > graphql 输入不接受接口和联合的最佳解决方法

问题描述

我有一个突变,它可以做这个人为的例子:

mutation {
  updateShelter(body: {
   addedAnimals: [],
   updatedAnimals: [{type: 'DOG', somethingSpecificWithDogType: 'value'}, {type: 'CAT', somethingSpecificWithCatType: 'value'}]
  }) {
      # some fields I want returned
  }
}

我的类型大致如下:

enum AnimalTypes {
 DOG
 CAT
}
interface Animal {
 type: AnimalTypes!
}

input Dog implements Animal {
  type: AnimalTypes!
  somethingSpecificWithDogType: String
}

input Cat implements Animal {
  type: AnimalTypes!
  somethingSpecificWithCatType: String
}

input UpdateShelterInput {
  addedDogs: Animal
  updatedDogs: Animal
}

extend type Mutation {
    updateShelter(body: UpdateShelterInput): SomeResponse
}

我知道输入必须引用其他输入,所以下面的类型不起作用,因为输入是指一个接口

input UpdateShelterInput {
      addedDogs: Animal
      updatedDogs: Animal
}

有一个 RFC 多年来一直悬而未决,以实现联合/接口类型作为输入,但与此同时,克服这一限制的最佳方法是什么。

一个想法是将正文作为 JSON 字符串/将正文中的 updatedAnimals 设为 JSON 字符串,然后 JSON 解析输入,但这似乎不是最优雅的方法,因为类型安全是使用 graphql 的全部要点。

有什么想法值得用来克服这个限制吗?

标签: graphqlapolloapollo-clientapollo-server

解决方案


推荐阅读