# How not to specify \_\_typename when creating nested normalized local data

**URL:** <https://community.apollographql.com/t/how-not-to-specify-typename-when-creating-nested-normalized-local-data/6643>\
**Category:** Other Apollo Topics\
**Created:** [August 27, 2023, 4:47pm UTC](https://community.apollographql.com/t/how-not-to-specify-typename-when-creating-nested-normalized-local-data/6643 "2023-08-27T16:47:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobiasBora](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/tobiasbora/32/4530_2.png) [@tobiasBora](https://community.apollographql.com/u/tobiasBora)\
**Post date:** [August 27, 2023, 4:47pm UTC](https://community.apollographql.com/t/how-not-to-specify-typename-when-creating-nested-normalized-local-data/6643/1 "2023-08-27T16:47:06Z")

</div>

If I want to insert nested local data to be normalized, I need to specify both `id` and `__typename` all around my json, for all entrees. But this is time consuming and error prone (e.g. if I forget it for one entry, the store will mix normalized and non-normalized data):

```auto
const IS_LOGGED_IN = gql`
  query IsUserLoggedIn {
    isLoggedIn @client
    cake @client {
      name @client
      ingredients @client {
        name @client,
        quantity @client
      }
    }
  }
`;

client.writeQuery({
  query: IS_LOGGED_IN,
  data: {
      isLoggedIn: false,
      cake: [
          {id: "1", __typename:"Cake", name: "Cookies", ingredients: [
              {id: "4", __typename:"Ingredients", name: "Sugar", quantity: "100g"},
              {id: "5", __typename:"Ingredients", name: "Chocolate", quantity: "250g"}
          ]},
          {id: "2", __typename:"Cake", name: "Choux", ingredients: [
              {id: "6", __typename:"Ingredients", name: "Eggs", quantity: "2"},
              {id: "8", __typename:"Ingredients", name: "Flour", quantity: "250g"}
          ]},
      ]
  },
});

```

Is it possible somehow to exploit the local schema to infer automatically the type of the data, so that I don’t need to add it myself?

---

<div class="post-metadata">

**Author:** ![JeffAuriemma](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jeffauriemma/32/2845_2.png) [@JeffAuriemma](https://community.apollographql.com/u/JeffAuriemma)\
**Post date:** [August 28, 2023, 3:23pm UTC](https://community.apollographql.com/t/how-not-to-specify-typename-when-creating-nested-normalized-local-data/6643/2 "2023-08-28T15:23:50Z")

</div>

Hi @tobiasBora - welcome to the forum! Type policies will get you part of the way there:

```ts
const cache = new InMemoryCache({
  typePolicies: {
    Ingredients: {
      keyFields: ["name", "quantity"],
    },
  },
});

```

Assuming the combination of `name` and `quantity` is sufficient for differentiating one `Ingredients` object from another, this will remove the need to assign an `id`.

However, ` __typename` is an essential part of normalization. You will still need to have objects with `__ typename: "Ingredients"` in order for that type policy to work. If you find yourself duplicating a lot of `__typename: "FooBar"` expressions in your codebase, consider creating a utility function to reduce the amount of duplication. I hope that helps, let me know 🙏

---

<div class="post-metadata">

**Author:** ![JeffAuriemma](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jeffauriemma/32/2845_2.png) [@JeffAuriemma](https://community.apollographql.com/u/JeffAuriemma)\
**Post date:** [August 28, 2023, 3:28pm UTC](https://community.apollographql.com/t/how-not-to-specify-typename-when-creating-nested-normalized-local-data/6643/3 "2023-08-28T15:28:58Z")

</div>

Also I believe you may be asking a similar question in our Discord, cross-linking @lenz’s response for context:

> **[Discord - A New Way to Chat with Friends & Communities](https://discord.com/channels/1022972389463687228/1144968441250467840/1145715646953431060)**
>
> Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.

I 100% agree with his response.

---

<div class="post-metadata">

**Author:** ![tobiasBora](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/tobiasbora/32/4530_2.png) [@tobiasBora](https://community.apollographql.com/u/tobiasBora)\
**Post date:** [August 28, 2023, 10:41pm UTC](https://community.apollographql.com/t/how-not-to-specify-typename-when-creating-nested-normalized-local-data/6643/4 "2023-08-28T22:41:11Z")

</div>

Thanks a lot for your answer, too bad apollo can’t deal with this better, especially when client schema could be used for this.

Regarding lenz answer that does not recommend Apollo client for local state management, I’m really confused now: using redux + normalizr seems much more complicated to maintain than graphql that I find to bequite elegant, especially when we addionally use graphql to deal with remote content anyway. So would you recommend redux + normalizr + appolo for instance in that scenario? If yes, what is wrong with appolo alone?

---

<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:48pm UTC](https://community.apollographql.com/t/how-not-to-specify-typename-when-creating-nested-normalized-local-data/6643/5 "2025-02-06T20:48:30Z")

</div>


