stop pending requests with apollo client hooks - graphql

It looks like its possible to cancel pending requests via client.stop() but the documentation is not showing us a solution when we use apollo client hooks where we have no client.
How to stop pending requests using apollo client hooks ?

Struggled for days and made a proof of concept that finally works.
I have explained the code below and here is my POC - Github source code.
Explaination:
Step – 1:
Create a middleware that holds the logic to track and cancel duplicate request via ReactJS context API – cancelRequest.tsx (complete source code)
Step – 2:
Generate namespace UUID and pass it using requestTrackerId via query context as below.
context:{
requestTrackerId: uuidNameSpace('LOGIN', RequestNameSpace)
}
Refer source code - Line 32
Step – 3:
Finally, wiring all the middleware and setup it up as funnel layers using from API of Apollo GraphQL client and set queryDeduplication to false.
Mechanism of action:
When ever more than one request originates from the same mutation query, each query is tagged to its requestTrackerId which remains same to that particular query and different for other queries.
Using UUID library namespace is generated for each query (Read the code). With this ID the middleware associates each query to its namespace generated ID and stores in a cache object.
Subsequent incoming request are looked up using the cache object. If there’s an ongoing request which is not yet completed, it will be aborted immediately using AbortController javascript API and this request is replaced with new request.
Libraries used
UUID – Used to create unique request tracker ID and prevent namespace
collision for multiple request from same component.
ReactJS – No intro needed i guess?
Apollo GraphQL – Follow the link to know more..
Hope this answer helps. Happy coding

Related

Can't I create a rest API in the apollo-server?

It is currently being developed using mysql-prisma-apollo server-nexus, and it is necessary to receive row data post using the REST API, not the GrqphQL statement currently developed. You want to process raw data passed to the post in Path (for example,/api/data/status). Is there a way to create a RestAPI on the apollo-server?
The apollo-server runs in a node environment, so you are able to use any http client you want.
Example:
axios
node-fetch

How to perform a GET request after a POST request with web-sockets

I am currently trying to build a go API using gin for a web and mobile application. I am new to the world of WebSockets and Go so I was wondering how I would go about triggering a GET request from the client after a relevant POST request was made ie: the POST request contained the user's ID so the clients who require information regarding that user are properly updated. Currently, I have the POST and GET requests which do what I need them, but I'm a little lost about how to make the entire flow realtime using WebSockets.
I believe this example of server-sent-events should address the question. Once a POST handler has been called, send a flag to the GET endpoint via a channel and then send an event through there.

GraphQL endpoint for file download

Is it possible to trigger a file download in a browser from the GraphQL endpoint on an apollo-server-express application?
I have the endpoint written in a standard express app.get function (see below) but I would like to make use of the GraphQL context for file download and so I'm wondering if it's possible to cause a download from a GraphQL endpoint.
Here's a bare-bones example of what I have on the express end in the app.get function:
app.get('/download-batch/:batchId', async (req, res) => {
res.send(new Buffer('test'));
});
Any help would me much appreciated. Thanks!
Yes, but you would be required to create a custom endpoint for that. You can't use the existing endpoint which you are using for making requests.
Using the custom endpoint, you have to add a middleware and process the data into a buffer or whatever format you need. But it would not be recommended. That would again become one more endpoint instead which you can write an API to serve that.(After all graphql is built mainly on the focus of single endpoint).
Boštjan Cigan mentions here some solutions and gives details about using GraphQL as a proxy with Minio. The backend would ask Mino to generate a temporary link that can be sent back to the browser for direct access.
This is a valid solution for many use cases.

api.ai Fullfillment POST requests doesn't append the action in the POST URL

Currently all the Fulfilment requests originating from api.ai are, POST requests to the base url configured in api.ai Fulfilment section. But to be able to have proper routing (microservice style) set-up on the server side it would be more worthwhile to append the action in the POST URL.
For a substantially large project, there can be hundreds of fulfilment actions and managing all of them in single monolithic project is cumbersome. If the action comes in the URL, then we can configure and organise the actions into multiple cloudfunctions in case of firebase hosting / server side microservices.
Edit:
As answered by matthewayne, I can use my own proxy set-up to route the requests to achieve the goal. But I don't want to introduce any additional delay into the request processing. Because I am expecting huge number of webhooks being fired. This would be a very easy implementation for Google api.ai team to incorporate that allows for a greater flexibility! Hence expecting an answer from google team!
Currently this isn't possible with API.AI's webhook design. I'd recommend setting up a proxy service that unpacks the webhook requests from API.AI, inspects the action and sends the proper request to the proper microservice endpoint and then forwards the response back to API.AI once the microservice has returned its result:

Are database calls in cloud functions counted as regular request?

As per FAQs of parse.com,
How are requests made from Cloud Code treated under the request limit?
Calling a Cloud function will count itself as a single request. Save and delete triggers in Cloud Code are considered part of the original object save/delete request, and they will not be counted as an additional request. However, if your function or save/delete trigger uses the Parse JavaScript SDK to perform additional operations, these will be treated in the same way they would as if they were made by a regular client.
What does using JS sdk in cloud code exactly mean? If i make simple database call like query.find from a cloud function or trigger then will that be counted as regular client request?
Yes query.find will be a database request. Any query execution wil be.

Resources