Search code examples
reduxgraphqlapollo

How to use redux with graphql


I like graphql. Because it has a single endpoint and I can only pull out the data I need.

So I use apollo-server for the server and apollo-client for the client.

Starting with apollo-client 3.0, state-management is supported.

The reactive variable function of the apollo-client is very convenient, but I prefer redux.

Redux toolkit also shortened the length of code that needs to be written.

The question is this.

  1. Can't I use apollo-client 3.0 and redux together?

  2. Can't I just use graphql without an apollo-client on redux? then How?

check please!


Solution

  • It's important to understand that GraphQL is just a protocol for describing requests and responses. However, yes, "I'm using GraphQL" typically also implies using a smart client library like Apollo or Urql.

    It is certainly possible to use Apollo and Redux in the same app, but the question becomes what you're using each of them for. I've covered this in my posts When (and when not) to Reach for Redux and Redux - Not Dead Yet!. Summarizing:

    • You can use Redux for anything, including server state caching and client-side state
    • Apollo is purpose-built for caching server state with GraphQL requests
    • If the only thing you were using Redux for was caching server state, and you choose to use Apollo for that task, you really don't need Redux any more
    • But, you can use GraphQL for requests and put that data in Redux if you really want to

    It's worth noting that we have an "RTK Query" API for Redux Toolkit that provides a complete data fetching abstraction, and we even have a demo of using RTK Query to request and cache data with GraphQL. You may want to try that out.