Would love to see updated version of this based on Apollo client 3 and reactive vars
@benji93253 жыл бұрын
same here
@SaschaB2B5 жыл бұрын
I migrated all data fetching to apollo graphql hooks. 99% of the time I just fetched data, show it, mutate it and save it. The last 1% is mostly global states like "Is the menu open?" or "Which country/language is selected?"That's handled with context now. Global wrapper component to provide access everywhere
@iandaley22955 жыл бұрын
same here, exactly. useContext w/ useMemo/useCallback are so powerful. I think a lot of peopleare still leaning on Redux out of habit/comfort...
@MungeParty4 жыл бұрын
That may be fine for simple applications but if your business logic has interdependent data with complex relationships, context is not the best practice. Apollo 3 has more client state management features.
@joohimks4 жыл бұрын
For information such as country, language and login do you think Context replaces redux for a complex application?
@avi72783 жыл бұрын
Why do you use useContext when you could just use a local-only Apollo field for that last bit of global state?
@pannihto75884 жыл бұрын
It would be nice to see an overview of Apollo Client 3.0. It's got some interesting new features
@EricBishard4 жыл бұрын
Excellent video, I'm literally doing everything you explained already, I came to this video thinking I would get some magic bullet about how to use GraphQL client instead of ContextAPI or instead of useState , useReducer, or even Redux. But once explained it makes complete sense.
@tayloraucoin1684 жыл бұрын
Over the past year I think I've developed a pretty cleaver way to handle the balance between React, GraphQL and Redux. After a long stretch of trying to use Apollo-local-state (hate it) I decided to make the switch back to Redux for local-state. In doing so I decided to move all my 'page-initializer' fetches into a separate action library in my store. I trigger these initializers by setting up a router-history watcher that fires on history change. Upon reception I store the data in Redux directly before it trickles down to the components. The extra bonus is that when I make these fetch calls I am also storing a record of what api calls have been made and at what unix time. That way, I can pivot between pages and restrict additional api calls from being fetched that are not needed because I already have that in my state and do not need it again. The unix time is for any features I may add where I want fresh data and essentially have an expiry to when a new fetch is permitted on history change. I will look into graphql-request though! Since I have already minimized the apollo library to just a request agent, that seems like it may be a better solution. Good suggestion
@asdf35682 жыл бұрын
I hate both
@edwardslayd7816 Жыл бұрын
Great video! Exactly what I was looking for. I am having to use Redux to pass state between roots due to mapbox & React3Fiber needing a new root. However the project that I inherited already has Apollo and Reactjs context. I was looking at transferring the state all into Redux but I am no thinking all I really need is the ThreeJS context in Redux and all the rest can stay in apollo
@rahulsriram62954 жыл бұрын
What if you wanna pass the fetched { data, loading, error } from one component to its children components? All examples you took are single component examples. What if we want the fetched results to be passed to child components? For example you have a blog post, and you got an update button under it. When you click on update, you render a pre-filled update form to edit. For that will we pass the body of the blog post to the form's text-area....or re fetch the data in the updating form component again?
@exactzero4 жыл бұрын
Use props.
@rahulsriram62954 жыл бұрын
@@exactzero sending props to down the component tree? Till whatever level I need it? If we are using context or redux, we can fetch data in our reducers and use them in any component without passing through props. We can't do that here cz useQuery is a hook we can use it only inside functional components.
@exactzero4 жыл бұрын
@@rahulsriram6295 If it is not more than one or two levels deep, it's fine to pass it down. In your case, you can execute useQuery again in the form component and Apollo will fetch the post from the local cache without touching your server.
@rahulsriram62954 жыл бұрын
@@exactzero Yeah man I totally forgot we can fetch from the cache too without touching the server. Thanks
@migom64 жыл бұрын
what if I need to pass down user data to all my child components and the user data is fetched using graphql hooks? Can I use apollo graphql hooks inside reducers?
@charleshughes14172 жыл бұрын
How would one manage frontend only mutations of persisted data? If I have a form that doesn't call the backend until the "Submit" button is pressed, how would I manage the ephemeral state without useState?
@avi72783 жыл бұрын
I agree with not duplicating data, however if you're using apollo, what use do you have to keep the state of a sidebar in useContext? Apollo provides a global state store that's tied to the cache itself and that you can query for local fields the same as you would for network requests, and even mix and match the two with @client decorators on local only fields, and subscribe to updates to those fields with watchQuery. There's no situation that Apollo can't handle that another state management solution could, and in fact it's very much the opposite of that. You can then get every bit of data you need for any one render with a single query. At this point, if you're using a gql server and redux/mobx/etc, something's wrong.
@fev45 жыл бұрын
Could you sometime in the future take a deep dive into apollo-link-state? I find it confusing where the data is stored and how to access/manipulate it. I understand you don't like the approach but if you could explain this with examples and pros/cons for both apollo-link-state vs context/useState, that would be really awesome.
@devk68105 жыл бұрын
+100
@iandaley22955 жыл бұрын
The #1 con from my experiencewould be on performance. If you need to manipulate data in the cache with apollo-link-state, you're adding another apollo lib (or using the already very bloated apollo-boost lib) to call and manipulate your data tree (via the cache, think O(n)+ ). And for what? For data that your app's state tree really doesn't need to involve GQL with... Better, as Ben suggests, set up a memoized function and store the client-only call using useContext() & useMemo() (or useCallback(), as the case may be). In this case, you're leaving the GQL-managed piece of your app's state tree untouched and the optimized client-side code will be using ReactJS's own native library functions, rather than layering in Apollo's on top of everything to manipulate the same data...
@devk68105 жыл бұрын
Yet its better than making follow up request or subscription I guess
@iandaley22955 жыл бұрын
@@Nick-oh5jc yes, that's from the docs and its an improvement for Apollo, definitely, however it's still additional overhead compared with theuseContext() & useMemo()/useCallback() hooks, which are part of the React project itself.
@fev45 жыл бұрын
In general I'd really like a deep dive into global state management comparing Apollo vs useContext
@higochumbo89322 жыл бұрын
Is there any reason why you memoize the .map() instead of putting it inside an useEffect that only runs on component mount?
@liftingisfun23503 жыл бұрын
Hi Ben, what if I want to have an offline mode where users can still manipulate the data without a connection to the server?
@chiubaca3 жыл бұрын
so its ok to mutate the apollo cache directly as if it was like a redux store?
@devk68105 жыл бұрын
Hey ben, can you make a video on updating cache data, for, eg: let say in case of e-commerce, I fetched all my product orders from the server, then use cancels the order, so I need to update its status to CANCEL. for this what I would do is, first update on the server then refetch all orders so that, cancel status gets reflected in UI or I can use subscription, wouldn't it be better to first update on the server then updated local cache so that I don't need to make followup fetch request or subscription hope you got that please help me
@stefanfeederle44295 жыл бұрын
Hey, you can use a subscription to propagate changes to the frontend: subscription orderCancelled { orderCancelled { orderId state } } This can be fired from the backend everytime an order is cancelled. Apollo should automatically update the cache if you include the orders key and the updated field.
@alextjn5 жыл бұрын
when creating or deleting documents, you need to manual update the cache. When editing, you don't have to, just make sure the mutation returns the edited document's id and the edited field(s). www.apollographql.com/docs/react/data/mutations/#updating-the-cache-after-a-mutation
@ModdingMontagesHD5 жыл бұрын
just finished up a pretty advanced e commerce site using react+context, would be a good implement.
@bawad5 жыл бұрын
I agree with the suggestion to use a subscription here. updating cache data: kzbin.info/www/bejne/ooKapWVmnKuFZ5Y
@mirandaineslopez14913 жыл бұрын
Hey tks for sharing What would u do when the { data }fetched from apollo is global data u will need in many components? Im looking for the best way to implement graphql + redux at a considerably big app, where the graphql Querys I receive from BE have data which I need to implement through the hole app... Would then be justified to store the apollo data in the store 4 u? Regards
@EatMyHacks5 жыл бұрын
Totally agree with treating Apollo cache as source of truth. In my project there is a case where I store a kanban board in react state rather than pull directly from the cache. This is because when a user moves a card with react-beautiful-dnd, it returns to it's initial position before the state is updated by a subscription. I'd like to use the cache as the source of truth while also maintaining fluid ux. I have played around with cache update queries in my mutations, but haven't been able to get them to play well with subscriptions(duplicated data etc). Thoughts?
@bawad5 жыл бұрын
When you need the ui to update instantly, take a look at optimistic updates in Apollo
@bridlershoc65693 жыл бұрын
Hi guys, I am implementing this solution, but my Apollo keeps going to the first item in the list. How to prevent this?
@robhybustami77183 жыл бұрын
Thanks @Ben Awad great video. Could you give a couple examples of where the apollo for local/global state management can be painful. Doing some research before I commit to a framework/style for a new project
@sreekumarmenon5 жыл бұрын
Nice, we do the same in our project, Apollo for api data + mobX for shared state.
@exactzero4 жыл бұрын
Short and concise. Perfect.
@mrbiditY5 жыл бұрын
Hey! Nice niche but very important topic again! It would be nice to get more React Native content like how to set up a robust project and what are the difficulties or just general React Native specific topics like deep linking or linking native modules with cocoapods etc.
@md.akib51245 жыл бұрын
Ben, how do you find out libraries like graphql-request? what kind of search term do you put in the URL bar?
@wojciechkaras89974 жыл бұрын
For me, the most common use case for coping date from useQuery to useState is when it is used as initial data for a form that can be changed later on many times before saving. How do you approach this kind of situation without duplicating data?
@bawad4 жыл бұрын
I think that's fine, not really duplicating there because the data will change
@Kasukke4 жыл бұрын
Yeah I think form data can be treated as local state, like the todoCompleteMap in the video, as opposed to remote data
@andrestone4 жыл бұрын
Just beware of missing subscription support on graphql-request.
@pierrega34185 жыл бұрын
I agree that apollo local state management is verbose, but I'm sure we can create tool which can generate ORM-like layer above the apollo cache.
@brisketbaron5 жыл бұрын
I just use the Apollo cache as my state and manage it with the methods you get when you create a cache object (readQuery, writeQuery, ...etc) and normalize it and use it like a redux store. If it’s client side only I use the @client directive. Don’t need useState or useReducer at all.
@adamsinger18905 жыл бұрын
I would recommend making a subcomponent to maintain the extra-state rather than creating a map in the main component. You will have a memory leak or have to manage the removal of items from the state map. Plus its just cleaner with a subcomponent since you can just store your todoComplete directly in a variable and not have it indexed in an object
@bawad5 жыл бұрын
I'm not sure why you would have a memory leak, but I agree sticking the logic in a subcomponent or custom hook can be a good idea to split up concerns
@adamsinger18905 жыл бұрын
@@bawad If the data changes and an ID you had indexed no longer exists, it would still exist in state unless you are managing that. Its a minor leak
@bawad5 жыл бұрын
oh I see
@shawnradke4 жыл бұрын
Thanks Ben! I just needed to send a graphql query and fetch data normally
@ray-lee4 жыл бұрын
reactive var in apollo client 3 is a lot more easier than context or redux!
@kylehenson22594 жыл бұрын
how are you ben , you think context is good or redux
@TheRenaissanceTechnologist4 жыл бұрын
This is a great tutorial. Thank you. One question though.. in your "setTodoCompleteMap" function, you're setting the "!todo.complete" ... but 'todo' is sourced from the Apollo query, so maybe 2 problems... (1) We need to have a 'complete:false' already coming in from our data source; and (2) 'setTodoComplete' will always set to 'true' -- so no toggle if incorrectly marked as complete. I may have missed something, but would appreciate some thoughts because I'm diving deeper into Apollo cache and leveraging the Provider to access data further down the tree... and really like the strategy you're applying here. Thank you for this and all your tutorials. They've helped me tremendously.
@bawad4 жыл бұрын
If the field should be stored in the db, then you should do a mutation then update the value in the cache otherwise storing whether its complete or not in useState is fine 2. I think it's fine to do that
@hamedhatami5185 жыл бұрын
I wish you had a Fullsrack tutorial of graphql-yoga/prisma/react/apollo
@bawad5 жыл бұрын
I'm waiting on Prisma until v2 is ready for prod
@ДенисМалышок3 жыл бұрын
Love u dude!
@alextjn5 жыл бұрын
Hi Ben, great insight. But for the "additional-field usecase", apollo-link-state offers an elegant solution as well. You can use @client directive, and write a local resolver for the additional field, very similar to the example provided in the docs here: www.apollographql.com/docs/react/data/local-state/#local-data-query-flow
@bawad5 жыл бұрын
imo it's not very elegant
@mariomerino97584 жыл бұрын
Awesome video ! Thanks man
@jorgebaraltquintero4 жыл бұрын
Really helpful videos, thanks for doing this!
@Kasukke4 жыл бұрын
Hey Ben, thanks for the video. I'm new to hooks, context, and GQL and this was very helpful. I've made every mistake in your list of bad examples haha. Architectural/design pattern question... If a piece of Apollo state is used by many components and components deep down the tree, how do you avoid the prop drilling issue? It's basically remote + global app data. Passing down as props is verbose, hard to read, and error prone to manage. We're currently stuffing this type of state and the mutation callbacks from useQuery into useContext. The extra level of indirection feels wrong. Do you have any advice? A video on architectural patterns would be cool.d
@artgurianov5 жыл бұрын
Exactly when I need it! Again! How? 👽👽👽
@luciano-lima5 жыл бұрын
What are you think to setup apollo server to use relay config?
@bawad5 жыл бұрын
don't have much thoughts on it, never tried it
@luciano-lima5 жыл бұрын
@@bawad oh see, thanks anyway for your good job with videos here
@zoachim4 жыл бұрын
This was helpful thank you
@chaddeveloper69902 жыл бұрын
Good video
@jdcb185 жыл бұрын
Can you do a video on best practices for useLazyQuery
@bawad5 жыл бұрын
You just use it whenever you don't want the query to be called right away
@ayazpathan26714 жыл бұрын
Hey Ben, I watched a lot of your videos, found helpful. I want to request you that can you make tutorials with GraphQl and Relay Modern?
@bawad4 жыл бұрын
when I try relay, sure
@apenet-4 жыл бұрын
Nice technique
@thomasini4 жыл бұрын
4:20 Interesting. This is the first time I've seen a ternary statement broken into multiple lines...
@avi72783 жыл бұрын
You don't use Prettier?
@dealloc5 жыл бұрын
I tend to separate state management for data fetching and state management for UI. I rarely use any state management library, but this depends on the complexity of the UI. For complex UI I use a FSM (Finite State Machine), XState being a great library for this. But most of the time I just stick with Context and hooks as they make for nice and reusable API for React apps.
@alisalahio5 жыл бұрын
Awesome vid! Your accent is really similar to Naval 🤔
@bawad5 жыл бұрын
interesting, you're the first one to say that
@alisalahio5 жыл бұрын
Ben Awad Maybe it’s the way of talking or explaining. Anyway, I like that way :D