2

I'm quite new with react-apollo, however wasn't sure how to approach this problem. I'm authenticating a user with a mutation but would like to access that same user object, that's returned in other components again (live navbar, to render button options, or a profile button once logged in). Should I just fetch the current logged in user everytime I need it? I.e.

query GetUser {
 id
 name
 role {
  name 
 }
 dob
}

Append this at the end of every component that needs it? I'm not sure if the best way might be to just cache it after logging in once versus this Even then how do you specifically cache it? I know inheritently it caches it as well, so its not like i have redundancy in fetching, however i might in code. What are some approaches y'all took.

Karan
  • 1,141
  • 2
  • 19
  • 42
  • You can use the graphql hoc or the query component to fire the query from any component. if the query is fired with the same input as before, apollo would return the data from the cache. – johnny peter Jun 12 '18 at 04:44
  • Makes sense after watching a lot of videos today as well, I guess I can create a fragment that is used in multiple components? That seems the easiest path i think. – Karan Jun 13 '18 at 04:32

1 Answers1

1

in react applications to save user data for authentication you have to save user data in storage like local storage and store to your global state of your application like redux and mobx to access from all over the application.

i recommend to you using redux.

note:

every time when start application you have to store data from storage to global state again.

also you can use apollo-cache-persist but i don't tried this!

Soroush Chehresa
  • 5,490
  • 1
  • 14
  • 29