Mutations to trigger a promise - graphql

I'm trying to figure out how I might be able to adopt Apollo in my React Native project compared to RXJS & Redux.
One of the special things about this app is that data mutations are not done through a traditional API but through Blockchain based transactions. Therefore I cannot simply call mutate in Apollo and expect it to eventually mutate something on the database as it's gonna go to a completely different several using a completely different library.
Would it be possible to have some kind of middleware which intercepted a mutation request and redirected it to another async library to get the job done? I'm wondering if apollo-link might be the way to do it.

Related

How to use Redux toolkit with Graphql query type subscriptions

I like the query types in Graphql, but I still need to control the local state. I am most comfortable doing this in Redux toolkit, in the future I will need subscriptions query.
From the information I have found here https://redux-toolkit.js.org/rtk-query/usage/customizing-queries but will it work with subscriptions request.
Is it possible to implement all this in next.js?

Is there a global way to cancel API calls when unmounting?

I'm using React and Redux-Saga there are many API calls in my project. Is there a way to have a global cancelToken/AbortController that can somehow be attached to the sagas middleware or added to axios globally?
I found many resources for cancelling a single API call, but my application has a huge amount of API calls and adding cancel functionality for all might be a hassle, so I'm looking for an alternate way.

Redux Middleware And GraphQL

Ok, As per my understanding we use middleware in redux for all the async calls now from Apollo documentation we can directly use queries and mutation inside our component.
I personally thought that writing all the business logic in a separate place using middleware is more intuitive as it helps in keeping business logic away from UI (where the data interchange could take place using redux)
Is there any way to keep my business logic separate using redux middleware with GraphQL rather than mixing it in my UI ?
Just like fetch or axios in redux-logic's create-logger.
Is there any way to keep my business logic separate using redux middleware with GraphQL rather than mixing it in my UI ?
You can achieve it by using apollo-fetch with redux-saga which is a Redux middleware.
When there is a need to fetch data, dispatch a simple sync Redux action. It gets intercepted by redux-saga that can use Apollo to fetch the data asynchronously, can perform other async actions in response if needed and then dispatch one or several sync actions to Redux store.
The business logic can then be split between async redux-saga functions called 'sagas' that could trigger other sagas to accomodate more complex logic and finally dispatch action(s) to Redux which remains the main 'source of truth' in your application.
Given that my team is already using graph/Apollo Client, we've recently been moving away from using Redux, and rather opting for Apollo Client's built in solution to manage local state. As noted in its docs:
Apollo Client (>= 2.5) has built-in local state handling capabilities
that allow you to store your local data inside the Apollo cache
alongside your remote data. To access your local data, just query it
with GraphQL. You can even request local and server data within the
same query!
More specifically, using the #client directive, you flag that you would like to fetch the field data locally using either a client-side resolver to execute local queries or mutations, or from the cache. You can take a look at the docs here. The ability to use this single tool has simplified our local state management. More importantly, this solution could meet your request to have more seperation between UI and business logic, as that logic should largely be encapsulated in your client-side resolvers, which can handle async requests, including requests over the network.

GraphQL is missing a service layer that

So I've been building my new app with GraphQL. It works great with modifying my apps models. But I find there's a serious hole in the way GraphQL works that REST API's handle better. For example, here's my use case:
When a form on my frontend is submitted, it creates a mutation: createFoo.
This alone works great. It creates a new Foo object in my database. But now I have this additional use case:
When the form is submitted, I want to hit a service that not only creates a Foo object in my database, but I also want to hit a third party API (some service I'm using) notifying me of the new creation.
How do I achieve this? With a REST API I would simply add that additional task as part of the API endpoint. With GraphQL there is no concept of a service layer that does other business logic. Should I do this in the mutation? It seems like a weird side effect to add some additional business logic to a mutation that creates an object..How are these things thought of in GraphQL world?

GraphQL Viewer for mutations

Is it a good practice to have a viewer for GraphQL mutations? Theoretically this makes sense to me as some mutation end points shouldn't be possible if you are not logged in, etc.
But when I see examples on the web, I only see implementation of GraphQL viewers for queries. For mutations, I do not see any implementation of viewers. For example, the GitHub API doesn't have a viewer on top of all their mutations.
The viewer field isn't a good practice, either for mutations or queries. It's a remnant of Facebook's legacy GraphQL platform from before it was open-sourced, which didn't allow arguments on root query fields. This meant that all of the fields needed to be moved one level down, below viewer.
The current way to do auth in GraphQL, at least in the JavaScript implementation, is by getting the user data based on something like an HTTP header and putting it on context, as mentioned here: http://graphql.org/learn/authorization/
Therefore, there is no reason to do viewer for mutations, or for queries. Most GraphQL clients don't mind, but one situation where it could make sense to have it in queries specifically is if you are using Relay 0.x, which has inherited some of Facebook's legacy GraphQL limitations. Hopefully a future version of Relay will remove this requirement.
For an additional source, read this comment from Lee Byron, one of the original creators of GraphQL.

Resources