Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Union types for ArgsType? #384

Closed
HendrikRoth opened this issue Jul 24, 2019 · 7 comments
Closed

Union types for ArgsType? #384

HendrikRoth opened this issue Jul 24, 2019 · 7 comments
Labels
Question ❔ Not future request, proposal or bug issue Wontfix ❌ This will not be worked on

Comments

@HendrikRoth
Copy link

HendrikRoth commented Jul 24, 2019

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 :-)

@MichalLytek
Copy link
Owner

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

@MichalLytek MichalLytek added Question ❔ Not future request, proposal or bug issue Wontfix ❌ This will not be worked on labels Jul 25, 2019
@HendrikRoth
Copy link
Author

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

@MichalLytek
Copy link
Owner

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

And in runtime you check which field is filled.

@HendrikRoth
Copy link
Author

No my goal is:

Array<TypeA, TypeB, TypeC>

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...

@MichalLytek
Copy link
Owner

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

@HendrikRoth
Copy link
Author

Ok i change my structure to this:

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

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

Thank you for your help

@bmamouri
Copy link

bmamouri commented Dec 6, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question ❔ Not future request, proposal or bug issue Wontfix ❌ This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants