Repository navigation
Closed
Labels
Description
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
References part of a union type can't be resolved by the GraphQL Gateway (federation) when is used as a type for a field. If the union type is used as return type for a Query or Mutation everything works as expected.
Minimum reproduction code
https://github.com/Gamote/nestjs-graphql-union-mrr
Steps to reproduce
Please check the minimum reproduction code for more info on the types.
When FavoriteItemUnion is used as return type for a field in an Object type.
// Field definition in the `users` service
@ObjectType()
@Directive('@key(fields: "id")')
export class User {
@Field(() => Int)
id: number;
@Field()
firstName: string;
@Field()
lastName: string;
// This field returns a union type
@Field(() => FavoriteItemUnion)
favoriteItem: typeof FavoriteItemUnion;
}// Query resolver defined in the `users` service
@Query(() => User)
async getUserById(@Args('id') id: number) {
return this.usersService.getById(id);
/*
^ For `userId=1` will return the following:
{
"id": 1,
"firstName": "John",
"lastName": "Doe",
"favoriteItem": {
"__typename": "Song",
"id": 1
}
}
*/
}# Query to retrieve the user + his favorite item
query GetUserById {
getUserById(id: 1) {
id
firstName
lastName
favoriteItem {
... on Song {
id
title
}
}
}
}{
"errors": [
{
"message": "Cannot return null for non-nullable field Song.title.",
"locations": [
{
"line": 9,
"column": 9
}
],
"path": [
"getUserById",
"favoriteItem",
"title"
]
}
],
"data": null
}In this case, if we remove the title from the query, we will get a response as GraphQL doesn't need to resolve any more data.
Expected behavior
I expect for this query:
query GetUserById {
getUserById(id: 1) {
id
firstName
lastName
favoriteItem {
... on Song {
id
title
}
}
}
}to return this data:
{
"data": {
"getUserById": {
"id": 1,
"firstName": "John",
"lastName": "Doe",
"favoriteItem": {
"id": 1,
"title": "Song 1"
}
}
}
}Package version
10.0.10
Graphql version
graphql: 16.4.0
@nestjs/mercurius: 10.0.9
@nestjs/platform-fastify: 8.4.4
mercurius: v9.4.0
NestJS version
8.0.0
Node.js version
v16.14.2
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
- I have tried to identify the problem in the source code but without success
- I have used the plain GraphQL and the query was successful so the functionality it is supported by GraphQL
Reacted by Mikkel Snitker, Daniel P. Shannon, Daniel Grathwol, Tiago Castro, nopice and max
Activity
rmagon commentedon Sep 5, 2022
In addition to this, I have also experienced that it is not possible to make the union fields as nullable. They are always resolved as mandatory fields.
This makes
favoruteItemas a mandatory field and not nullablec1ba commentedon Nov 1, 2022
I have the same issue as well still today.
The object type in question:
The query:
Query resolver:
The result:
rmagon commentedon Nov 1, 2022
My workaround was to create a separate Nullable type, smth like this:
and then use it in my union type, when we do resolveType, we always default to the
NullableType.Hope it helps!
c1ba commentedon Nov 3, 2022
I didn't totally helped me, but at least it helped me for debugging. I also work with mongoose. If I am to return a value like a string for the object I see that it has no problems, but I now see that it crashes when I am to return an object that needs populated. Still, thanks
ggepenyan commentedon Apr 21, 2023
Let's say I have this:
where User is ObjectType.
and later I do:
Which I'm not able to do because of an error (
const getObjectType = (item) => this.typeDefinitionsStorage.getObjectTypeByTarget(item).type;). Even if I use a custom Scalar for the Number.ggepenyan commentedon Apr 21, 2023
I'm able to do this without an error:
@Field((type) => [Int, User])But later in the schema.gql I get this:
defender: [Int!]!Which is not what expected.
xde013 commentedon Apr 28, 2023
Having the same issue here, for example:
Will result to en error:
Error: Cannot determine a GraphQL input type null for the "name". Make sure your class is decorated with an appropriate decorator.ahmad66617 commentedon Jun 22, 2023
The same problem-(((
moloti commentedon Dec 21, 2023
@kamilmysliwiec is there any update on this issue? This problem still exists. Unions would be super helpful to simplify API design, but with this bug there is very limited use of the union type. I understand that unions don't work as an input type (still not implemented in the standard), but a fix for this bug would be amazing.
B0ySetsF1re commentedon Feb 9, 2024
@kamilmysliwiec Looks like unions doesn't work at all, I tried mostly the same as guys did above and it doesn't working. Is it planned to be fixed any time soon? Thanks! :)
DeepaPrasanna commentedon Apr 9, 2024
I am also facing the same issue. I wanted object or array of objects
kamilmysliwiec commentedon Oct 26, 2026
This was a Mercurius gateway issue: its query planner didn't send the
_entitiesfetch for union members that live in another subgraph when the union is used as an object field type. It was fixed upstream in mercurius-js/mercurius-gateway#122, released in@mercuriusjs/gateway5.1.0. With 5.1.0+ the scenario from this issue resolves correctly (field-level and query-level unions), so closing. If you still see it on@mercuriusjs/gateway>= 5.1.0, please open a new issue with a reproduction.fix(mercurius): require the gateway release that resolves union fields (