Type-graphql: Union types for ArgsType?

Created on 24 Jul 2019  路  7Comments  路  Source: MichalLytek/type-graphql

Is it possible to use union types for ArgsType() ?

@InputType()
class TestA {
  @Field() testa: string;
}

@InputType()
class TestB {
  @Field() testb: string;
}

@InputType()
class TestC {
  @Field() testc: string;
}

const UnionType = createUnionType({
  name: "UnionType",
  types: [TestA,TestB, TestC];
})

@ArgsType()
class UpdateArgs {
  @Field(() => [UnionType], {nullable: true})
  tests: Array<TestA|TestB|TestC>;
}

i tried it this way but i receive an "cannot determine graphql input type for tests"

Thank you :-)

Question Wontfix

Most helpful comment

For anyone who comes to this issue looking for Union Input Types, there is an RFC in the GraphQL spec at the moment, and the issue has been discussed several times there.

All 7 comments

No, this is not supported by GraphQL spec. Unions are output types and can't be used in args or inputs.

Hmmm... but how would you accept args/inputs that are an array of different types? Any recommendation?

input MyInput {
  one: OneInput
  two: TwoInput
  three: ThreeInput
}

And in runtime you check which field is filled.

No my goal is:

Array

so the array consists of different types with an unknown amount.

one possible way would be:

input Input {
  string: StringInput[],
  date: DateInput[],
  time: TimeInpiut[]
}

But this would break my structure because i don't have the origin sorting anymore...

So you have to make an RFC to the GraphQL spec with a proposed change or switch to REST. Sorry 馃槙

Ok i change my structure to this:

inputs: {
  string: [{_id: 123, }, ...],
  date: [....],
}

sorting: [
  "123",
  ....
]

Thank you for your help

For anyone who comes to this issue looking for Union Input Types, there is an RFC in the GraphQL spec at the moment, and the issue has been discussed several times there.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

glentakahashi picture glentakahashi  路  3Comments

memark picture memark  路  3Comments

MichalLytek picture MichalLytek  路  3Comments

reilem picture reilem  路  3Comments

tongtwist picture tongtwist  路  3Comments