Multiple return types for a field
See original GitHub issueI’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
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Define a Function with multiple return Types in TypeScript
Use a union type to define a function with multiple return types in TypeScript, e.g. `function getValue(num: number): string | number {}`.
Read more >How to Return Multiple Values From a Java Method - Baeldung
In this tutorial, we'll learn different ways to return multiple values from a Java method. First, we'll return arrays and collections.
Read more >Returning Multiple Values from a Function - JavaScript Tutorial
Summary: in this tutorial, you will learn to define JavaScript functions that return multiple values. JavaScript functions can return a single value.
Read more >Return different type of data from a method in java?
I create various return types using enum. It doesn't defined automatically. That implementation look like factory pattern.
Read more >5 ways to return multiple values from a method in Java
This is the most commonly used method to return multiple values from a method in Java. The idea is to return an instance...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Please provide a minimum reproduction repository.
https://github.com/graphql/graphql-spec/issues/488