40

I have read number of articles/docs based on redux and relay but still I am confused how this two libraries are different?
What are the advantages and downsides of this this two libraries?
What is the exact role of GraphQL in relay?
Which is the library more suitable for enterprise data driven applications like CRM/ERP?

jonathancardoso
  • 9,319
  • 6
  • 49
  • 63
Sachin
  • 2,121
  • 9
  • 26
  • 49

1 Answers1

72

Both are Flux implementations, a Facebook framework for managing application's state.

  • Redux: general javascript library which helps handle state management in your application. Redux is not react-dependent and can be used with any library. The react-redux library is used to easily integrate react with redux. In redux the application state is located in a single store, each component can access the state, and can also change the state by dispatching actions. Redux doesn't handle data fetching out of the box, though it can be done manually: simply create an action that fetches the data from the server into the store.

  • Relay: Created by facebook for react, and also used internally there. Relay is similar to redux in that they both use a single store. The main difference is that relay only manages state originated from the server, and all access to the state is used via GraphQL querys (for reading data) and mutations (for changing data). Relay caches the data for you and optimizes data fetching for you, by fetching only changed data and nothing more. Relay also supports optimistic updates, i.e. changing the state before the server's result arrives.

GraphQL is a web service framework and protocol using declarative and composable queries, and solves problem like over fetching and under fetching, it is believed to be a valid candidate to replace REST.
GraphQL is not relay dependent, other way around, relay depends on graphql. Graphql can be used in redux same way every other data fetching is done.

As you can see the main advantage of relay over redux is that data fetching is already taken care of, and very optimized for that.
On the other hand, it can't manage client's specific state, but that is rarely needed.

Also, IMO Relay is harder to learn and implement, but the end result is better and more optimized, but for small applications I'd go with redux.

Gershon Papi
  • 4,268
  • 3
  • 18
  • 45
  • : Which is the library more suitable for enterprise data driven applications like CRM/ERP? – Sachin Jun 26 '16 at 09:22
  • 4
    Relay, since it handles the tricky parts of data fetching and managing for you. Also I'd say relay has already the "enterprise" view in mine, while redux intends to be lighter. These are only opinions, though. – Gershon Papi Jun 26 '16 at 09:27
  • @GershonPapi for smaller apps you would go with relay? I think you meant redux. – Timmerz Sep 29 '16 at 20:20
  • @Timmerz Right, my bad. – Gershon Papi Sep 30 '16 at 07:38
  • What you mean by "client's specific state?" – Andy Jul 16 '17 at 03:47
  • @Andy State that is only relevant and required on the client. For example, an app that for some reason includes a counter. There are many ways to store the counter's state, obviously, but for the sake of the comparison between redux and relay: Using relay the state of the counter would have to be stored and managed server side, to be managed by relay, while redux doesn't have this limitation. – Gershon Papi Jul 16 '17 at 17:44
  • 2
    Why you say "that is rarely needed?" For modern SPA apps, it's needed all over the place. – Andy Jul 16 '17 at 21:03
  • 1
    @Andy When it's needed it's usually needed over a single component tree (meaning, state that is not shared between multiple trees), in which case, the component state + props can be used. Perhaps I exaggerated when I said "rarely needed", but I managed to use Relay for a farely not simple app. – Gershon Papi Jul 17 '17 at 19:14
  • Thanks for the awesome reply – Pramod Dutta Aug 30 '17 at 09:25