{"id":18770830,"url":"https://github.com/gamote/nestjs-graphql-union-mrr","last_synced_at":"2025-12-12T02:30:10.888Z","repository":{"id":66175519,"uuid":"488496383","full_name":"Gamote/nestjs-graphql-union-mrr","owner":"Gamote","description":null,"archived":false,"fork":false,"pushed_at":"2022-05-04T07:43:49.000Z","size":111,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-29T07:42:44.930Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Gamote.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-04T07:43:38.000Z","updated_at":"2022-05-04T07:43:52.000Z","dependencies_parsed_at":"2023-02-20T23:16:07.810Z","dependency_job_id":null,"html_url":"https://github.com/Gamote/nestjs-graphql-union-mrr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gamote%2Fnestjs-graphql-union-mrr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gamote%2Fnestjs-graphql-union-mrr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gamote%2Fnestjs-graphql-union-mrr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Gamote%2Fnestjs-graphql-union-mrr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Gamote","download_url":"https://codeload.github.com/Gamote/nestjs-graphql-union-mrr/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239678008,"owners_count":19679181,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T19:21:47.686Z","updated_at":"2025-12-12T02:30:10.817Z","avatar_url":"https://github.com/Gamote.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nest.js GraphQL Union Issue Minimum Reproduction Repo\n\nTech used\n- Nest.js `v8.0.0`\n- Nest.js GraphQL `v10.0.10` (Gateway + Federation)\n- Mercurius `v9.4.0` (with Fastify)\n\n## Issue description\n\nTL;DR: 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 all it works as expected.\n\nAs you know, when working with GraphQL Federation one service can return a type reference (e.g. `{ __typename: 'Song', id: '1' }`) that can later be resolved by another service.\nIn this service we have created a union type called `FavoriteItemUnion` that contains the `Song` model (extension of the `Song` type defined in the `songs` service) and the `Book` model (extension of the `Book` type defined in the `books` service).\n\n### Working example\nWhen `FavoriteItemUnion` is used as return type for a Query, GraphQL is able to retrieve the data from the appropriate service.\n\n```typescript\n// Query resolver defined in the `users` service\n@Query(() =\u003e FavoriteItemUnion)\nasync topItem() {\n  return { __typename: 'Song', id: 3 };\n}\n```\n\n```graphql\n# Query to retrieve the top item\nquery TopItemQuery {\n  topItem {\n    ... on Song {\n      id\n      title\n    }\n  }\n}\n```\n\n```json\n// Query result\n{\n  \"data\": {\n    \"topItem\": {\n      \"id\": 3,\n      \"title\": \"Song 3\"\n    }\n  }\n}\n```\n\n### Issue example\nWhen `FavoriteItemUnion` is used as return type for a field in an Object type.\n\n```typescript\n// Field definition in the `users` service\n@ObjectType()\n@Directive('@key(fields: \"id\")')\nexport class User {\n  @Field(() =\u003e Int)\n  id: number;\n\n  @Field()\n  firstName: string;\n\n  @Field()\n  lastName: string;\n  \n  // This field returns a union type\n  @Field(() =\u003e FavoriteItemUnion)\n  favoriteItem: typeof FavoriteItemUnion;\n}\n```\n\n```typescript\n// Query resolver defined in the `users` service\n@Query(() =\u003e User)\nasync getUserById(@Args('id') id: number) {\n  return this.usersService.getById(id);\n  /*\n    ^ For `userId=1` will return the following:\n    {\n      \"id\": 1,\n      \"firstName\": \"John\",\n      \"lastName\": \"Doe\",\n      \"favoriteItem\": {\n        \"__typename\": \"Song\",\n        \"id\": 1\n      }\n    }\n  */\n}\n```\n\n```graphql\n# Query to retrieve the user + his favorite item\nquery GetUserById {\n  getUserById(id: 1) {\n    id\n    firstName\n    lastName\n    favoriteItem {\n      ... on Song {\n        id\n        title\n      }\n    }\n  }\n}\n```\n\n```json\n{\n  \"errors\": [\n    {\n      \"message\": \"Cannot return null for non-nullable field Song.title.\",\n      \"locations\": [\n        {\n          \"line\": 9,\n          \"column\": 9\n        }\n      ],\n      \"path\": [\n        \"getUserById\",\n        \"favoriteItem\",\n        \"title\"\n      ]\n    }\n  ],\n  \"data\": null\n}\n```\n\nIn 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.\n\n## How to run\n\n1. Install dependencies\n    ```bash\n    $ yarn\n    ```\n   \n2. Start the federated services\n    ```bash\n    # Users service\n    $ yarn start users\n   \n    # Songs service\n    $ yarn start songs\n   \n    # Books service\n    $ yarn start books\n    ```\n\n3. Start the gateway\n    ```bash\n    $ yarn start\n    ```\n\nNow you can visit the gateway at `http://localhost:3010/graphql` and use the queries described above.\n``","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgamote%2Fnestjs-graphql-union-mrr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgamote%2Fnestjs-graphql-union-mrr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgamote%2Fnestjs-graphql-union-mrr/lists"}