Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turning off __typename consequences #2881

Closed
vladshcherbin opened this issue Jan 17, 2018 · 14 comments
Closed

Turning off __typename consequences #2881

vladshcherbin opened this issue Jan 17, 2018 · 14 comments

Comments

@vladshcherbin
Copy link

vladshcherbin commented Jan 17, 2018

Hey, there are a lot of issues about __typename added to data. There are use cases when you want to receive data exactly as it was asked. The advised solutions were to manually deeply remove __typename from data or to turn this off with addTypename: false.

What are the consequences of turning this off with addTypename: false, will it break cache somehow/make it slower/etc ?

@rosskevin
Copy link
Contributor

Look at how objects are cached, typically the default is __typename and id but you can customize this as well as remove __typename via the options.

See https://www.apollographql.com/docs/react/features/cache-updates.html#normalization

Also, watch this for more info: https://www.youtube.com/watch?v=zWhVAN4Tg6M&feature=youtu.be&t=690

@vladshcherbin
Copy link
Author

vladshcherbin commented Jan 17, 2018

@rosskevin yep, I checked the store using apollo dev tools. So, if I fetch users with type User, the store will have items (typename + id):

User:1
User:33

If I turn off addTypename, this will be changed to:

ROOT_QUERY.users.1
ROOT_QUERY.users.33

So the question is, are there any cons of this? For example, bad cache performance, duplication, nested types connection problems or smth else if __typename is turned off?

@rosskevin
Copy link
Contributor

The video discusses your question.

@vladshcherbin
Copy link
Author

@rosskevin I watched it and got the idea about typename. My guess is that without typename the store will duplicate same fetched items in store which is not a good thing, am I right?

@rosskevin
Copy link
Contributor

I believe (if I recall) as you have seen that it will index via query instead, so if your query differs then you will have cache misses - this is all from memory as I decided after watching that the __typename (default) approach fit me best.

@vladshcherbin
Copy link
Author

@rosskevin seems like I'll have to use defaults too and remove __typename later. Thanks, man!

@ikatun
Copy link

ikatun commented Jul 28, 2018

If you want to avoid having to manually remove __typename from every response, you can just require this module https://www.npmjs.com/package/typename-monkey-patch somewhere near the entry point of your app.
It monkey patches graphql HOC from react-apollo so that __typename is removed before sending data to your wrapped component.

@vladshcherbin
Copy link
Author

@ikatun hey, let's hope this feature will be somehow added in core. You can track the progress or comment here 😉

@faurehu
Copy link

faurehu commented Jan 11, 2019

@ikatun are you still doing this or are you aware of a better practice?

@pitops
Copy link

pitops commented Jan 17, 2019

interested in an update on this

@rosskevin
Copy link
Contributor

interested in an update on this

@pitops what kind of update are you interested in?

This issue discusses what it is, why it is there, links to a video, and links to a document describing what and why it exists, and how to take that process into your own hands.

@pitops
Copy link

pitops commented Jan 18, 2019

@rosskevin I am interested if there is a better practice for this, one that the apollo-client supports or will support in the near future.

@ikatun
Copy link

ikatun commented Jan 18, 2019

We dumped the react-apollo all together and switched to using our own library which exposes apollo through mobx. The library itself filters the extraneous __typename field.

Internally, it all just comes to this function

function removeTypename(data) {
  return typeof data === 'object' ? objectKeyFilter(data, ['__typename'], true) : data;
}

and then (for example in render function/method)

const cleanData = removeTypename(dataFromApollo);

objectKeyFilter being https://www.npmjs.com/package/object-key-filter

@riginoommen
Copy link

Hi @ALL,

Please check the below thread for this issue.

https://stackoverflow.com/questions/47211778/cleaning-unwanted-fields-from-graphql-responses/51380645#51380645

Regards
Rigin Oommen

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants