rtk-incubator / rtk-query

Data fetching and caching addon for Redux Toolkit
https://redux-toolkit.js.org/rtk-query/overview
MIT License
626 stars 31 forks source link

Understanding GraphQL Support #228

Closed a-laughlin closed 3 years ago

a-laughlin commented 3 years ago

I may be able to contribute, so want to understand the state of things first.

The React with GraphQL example, contains an example of rtk-query with GraphQL queries.

The comparison page contains this image: image

Does that difference imply rtk-query does not officially support GraphQL?

If so:

phryneas commented 3 years ago

RTK-Q is a request cache, but completely independent of the transport protocol and data that is being transported. The goal is explicitly to have a "per request" cache and not a normalized cache. So it can be used for GraphQL, but not have all the benefits clients explicitly written for GraphQL have. So it's probably around the level of urql with their Document Caching, but will never go beyond that. And that's okay, after all, there are specialized tools there for a reason and this would become a monster if it would do everything perfectly.

As for subscriptions, I have opened a Pull Request against the new codebase that would enable that more comfortably.

It's essentially this API: https://github.com/rtk-incubator/rtk-query/blob/cdc1cacca6cd42a7b2bdd12c66540ac11c7d5d33/test/lifecycle.test.tsx#L76-L99

(You can scroll down further in that file to see that in combination with entityAdapter.

So with that, all building blocks are in place, our "graphql support" is as good (imho, with those lifecycles, better) than React-Query who "officially support" GraphQL, but I would not feel comfortable to really advertise it.
There are better tools and you would usually go for those except if you have the need to very tightly integrate with Redux, or if you already use RTK and don't need all the fancy extra stuff from Apollo.

That said, the next thing that would be great to add would be a plugin for graphql-codegen. We have something similar for OpenAPI so it would probably make sense to integrate that somehow over there - maybe transforming that whole repo there into a monorepo for all kinds of codegens.

a-laughlin commented 3 years ago

Thanks for the quick response and explanation @phryneas. Nice test examples.

Regarding graphql-codegen, the concept is to generate something like generated.ts from a GraphQL schema?

phryneas commented 3 years ago

Yes, but not from a full schema, but from queries and mutations in your codebase.

With graphql-codegen, you usually put some .graphql files (or .js/ts files with graphql-tag usage) into your codebase and it generates one central file with hooks for apollo or whatever plugin you use out of all those

a-laughlin commented 3 years ago

I'm comparing state management options with new business needs this week. Adding RTK-query to the list to evaluate, along with the need for schema codegen. Should be able to estimate how much time I can commit afterward.

phryneas commented 3 years ago

The next release of Redux Toolkit will have support for streaming updates which should cover subscriptions.

All further feature requests should be filed over at https://github.com/reduxjs/redux-toolkit/ - I'm closing this here now