Best workaround for graphql input not accepting interfaces and unions

Viewed 482

I have a mutation that does something like this contrived example:

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

My types loosely look like:

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
}

I understand that inputs have to refer to other inputs so the below type doesn't work since input is referring to an interface

input UpdateShelterInput {
      addedDogs: Animal
      updatedDogs: Animal
}

There is an RFC that has been pending for years to implement union/interface types as inputs but in the meantime what would be the best approach to get over this limitation.

One idea is to just make the body as JSON string / make updatedAnimals inside body a JSON string and then JSON parse the input, but it doesn't seem to be the most elegant approach since type safety is the whole point of using graphql.

Are there any ideas worth using to get over this limitation?

0 Answers
Related