Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do GraphQL & Redux work together?

Tags:

I am wondering about the relationship between the two. I am quite confused since I see them both as ways to manage state almost, and there seems to be an overlap, so I am seeking a conceptual distinction I can apply in order to find out what information to keep where and how to make them work together. Any advice?

like image 885
janus Avatar asked Jan 29 '18 11:01

janus


People also ask

How GraphQL is working?

GraphQL is a query language for your API, and a server-side runtime for executing queries using a type system you define for your data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data.

Is GraphQL easy to learn?

A well-designed API is very easy to use and learn. It's also intuitive, a good point to keep in mind when you're starting to design your API. To solve these problems, Facebook created GraphQL. Today, I think GraphQL is the best way to build APIs.


2 Answers

You have to distinguish between view state (e.g. search field, popup, toggle) and data state (e.g. remote API). Whereas Apollo is mainly used for data state, Redux/MobX/React's Local State are used for view state when used in combination with Apollo Client. If not used with Apollo Client, these solutions can be used for the remote data state too.

  • If your application is purely remote data driven and uses a GraphQL backend, Apollo Client can be sufficient for your application.

  • If you have a few view states in your application, mix in React's local state management.

  • If you have several to a lot of view states, use Redux or MobX for your view state or try out apollo-link-state.

Maybe this article clarifies some things more in-depth: Combining Redux and Apollo.

Opinion: I feel like in the time of React Hooks, Redux and MobX are getting less relevant. Over here, you can find an in-depth article about all the state management options in React.

like image 86
Robin Wieruch Avatar answered Oct 03 '22 13:10

Robin Wieruch


GraphQL is just a way to tell an endpoint "this is the data I want". Redux is the way to store that data. Conceptually, they're entirely separate.

As for integrating them, though, we've had success using Apollo, (before they removed Redux, and rolled their own store). Take a look at this article to start you off, and then see where Apollo has gone from there,

like image 31
Adam Barnes Avatar answered Oct 03 '22 15:10

Adam Barnes