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

Multiple return types for a field #1633

Closed
NicolaeBasso opened this issue Jul 15, 2021 · 6 comments
Closed

Multiple return types for a field #1633

NicolaeBasso opened this issue Jul 15, 2021 · 6 comments

Comments

@NicolaeBasso
Copy link

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I've got a class with multiple fields, and the one which returns object of union type results in an error:
"Error: Cannot determine a GraphQL input type for the "users". Make sure your class is decorated with an appropriate decorator."

The issue is not about decorators from what I've researched, it is about the type I specify as a return type in @field decorator.

Expected behavior

I need for the "access" field to be able to return an object of one out of multiple types.

Minimal reproduction of the problem with instructions

Field "users" in WidgetAccess class is the questionable part

BaseWidgetType - top level class from which the issue needs to start be reviewed from, "access" field is 
the one with problems inside it:

baseWidgetType.ts:
@ObjectType('BaseWidget')
export class BaseWidgetType extends BaseType {
  @Field((type) => BaseWidgetConfig, { nullable: true })
  widgetConfig: BaseWidgetConfig;

  @Field((type) => WidgetAccess, { nullable: true })
  access: WidgetAccess;
}


widgetAccess.ts:
export const WidgetUserUnion = createUnionType({
  name: 'WidgetUserUnion',
  types: () => [WidgetSingleUser, WidgetGroup],
});


@ObjectType('WidgetAccess')
@InputType('WidgetAccessInput')
export class WidgetAccess {
  @Field({ nullable: false })
  public: boolean;

  @Field((type) => [WidgetUserUnion], { nullable: true })
  users: typeof WidgetUserUnion[];
}


widgetUser.ts:
@ObjectType('WidgetUser')
@InputType('WidgetUserInput')
export class WidgetUser {
  @Field({ nullable: false })
  userId: string;
}


widgetGroup.ts:
@ObjectType('WidgetGroup')
@InputType('WidgetGroupInput')
export class WidgetGroup {
  @Field({ nullable: false })
  groupId: string;
}

What is the motivation / use case for changing the behavior?

It would be nice to be able to split differently structured units by different types, to restrict client to enter logically disjoint sets of data.

Environment


Nest version: 7.6.15

 
For Tooling issues:
- Node version: 14.17.0  
- Platform:  Windows 
@kamilmysliwiec
Copy link
Member

Please provide a minimum reproduction repository.

@NicolaeBasso
Copy link
Author

Please provide a minimum reproduction repository.

Thanks!
Here's minimal error reproduction setup:
https://github.com/NickBasso/NestJs-GQL-Issue-min-reproduction

@NicolaeBasso
Copy link
Author

bump

@rodgetech
Copy link

Hi, did you ever find a solution to this?

@SergioSuarezDev
Copy link

same here, can't use a union in inputType, does anyone know of an alternative?

@kamilmysliwiec
Copy link
Member

graphql/graphql-spec#488

@nestjs nestjs locked and limited conversation to collaborators Oct 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants