# Generated code with union Query

**URL:** <https://community.apollographql.com/t/generated-code-with-union-query/6082>\
**Category:** Client SDKs\
**Tags:** client, mobile, kotlin\
**Created:** [April 6, 2023, 7:58am UTC](https://community.apollographql.com/t/generated-code-with-union-query/6082 "2023-04-06T07:58:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbanse](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jbanse/32/4152_2.png) [@jbanse](https://community.apollographql.com/u/jbanse)\
**Post date:** [April 6, 2023, 7:58am UTC](https://community.apollographql.com/t/generated-code-with-union-query/6082/1 "2023-04-06T07:58:55Z")

</div>

Hello,

I have an error when I query a `union` type.  
The code generator ask me to query each type of the union.

Example :

the type :

```auto
union Content = Article|Author|Recipe

```

the part of the query (I only want articles)

```auto
            edges {
                node {
                    ... on Article {
                        id
                    }
                }
            }

```

At compile time, I have this message :

> Key Field(s) ‘[id]’ are not queried on Author at…

Should I query all types or maybe `union` is misused ? Or a bug ?

Thanks

---

<div class="post-metadata">

**Author:** ![Benoit\_Lubek](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/benoit_lubek/32/2070_2.png) [@Benoit\_Lubek](https://community.apollographql.com/u/Benoit_Lubek)\
**Post date:** [April 6, 2023, 9:06am UTC](https://community.apollographql.com/t/generated-code-with-union-query/6082/2 "2023-04-06T09:06:10Z")

</div>

Hi!

The results of your query can still contain `Author`s even if they won’t contain any fields.

If you have configured the cache to use `id` as the key field for `Author`, then this field is needed and must be queried so the cache can use it. In most cases, id fields are automatically added to queries in a transparent way. But in cases like this one with unions, this cannot be done automatically.

One way to fix this is to add a case for `Author` too:

```graphql
            edges {
                node {
                    ... on Article {
                        id
                    }
                    ... on Author {
                        id
                    }
                }
            }

```

---

<div class="post-metadata">

**Author:** ![jbanse](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jbanse/32/4152_2.png) [@jbanse](https://community.apollographql.com/u/jbanse)\
**Post date:** [April 6, 2023, 9:56am UTC](https://community.apollographql.com/t/generated-code-with-union-query/6082/3 "2023-04-06T09:56:16Z")

</div>

I understand. Our query set a parameter to get only Article.

But the fix is, actually, not possible.  
We should use an alias for the id of the Author.  
And when we use the alias like this :

```auto
edges {
    node {
          ... on Article {
              id
          }
          ... on Author {
              authorId : id
          }
    }
}

```

The compiler print the same message :

> Key Field(s) ‘[id]’ are not queried on Author at Operation…

I feel stuck in a loop 😀

---

<div class="post-metadata">

**Author:** ![jbanse](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jbanse/32/4152_2.png) [@jbanse](https://community.apollographql.com/u/jbanse)\
**Post date:** [April 18, 2023, 1:17pm UTC](https://community.apollographql.com/t/generated-code-with-union-query/6082/4 "2023-04-18T13:17:10Z")

</div>

Hello,  
should I create an issue for usage of alias in `union` query ?  
Thanks

---

<div class="post-metadata">

**Author:** ![Benoit\_Lubek](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/benoit_lubek/32/2070_2.png) [@Benoit\_Lubek](https://community.apollographql.com/u/Benoit_Lubek)\
**Post date:** [April 19, 2023, 7:27am UTC](https://community.apollographql.com/t/generated-code-with-union-query/6082/5 "2023-04-19T07:27:55Z")

</div>

Hi! I’m sorry, I missed your previous message!

When using an alias like you do, the server will not reply with an `id` but with `authorId`, which means the `id` cannot be seen and used in the key, and therefore Apollo will still complain it is missing. Why can’t you use `id` directly?

---

<div class="post-metadata">

**Author:** ![jbanse](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jbanse/32/4152_2.png) [@jbanse](https://community.apollographql.com/u/jbanse)\
**Post date:** [April 20, 2023, 2:12pm UTC](https://community.apollographql.com/t/generated-code-with-union-query/6082/6 "2023-04-20T14:12:10Z")

</div>

Hello,  
You are right, it works.  
I have an issue with our schema, some id fields are nullable and some non nullable.  
I will update our schema to get consistent id non nullability.  
Thanks !

---

<div class="post-metadata">

**Author:** ![Justin\_Gonzalez](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/justin_gonzalez/32/5359_2.png) [@Justin\_Gonzalez](https://community.apollographql.com/u/Justin_Gonzalez)\
**Post date:** [February 6, 2025, 8:49pm UTC](https://community.apollographql.com/t/generated-code-with-union-query/6082/7 "2025-02-06T20:49:00Z")

</div>

Archived on February 6, 2025, 8:49pm UTC


