Detuned
Detuned

Reputation: 3758

Configure Apollo GraphQL hanging requests

I'd like to set an arbitrary timer on my graphQL requests. Say, I make a request and it takes longer than 10 seconds for Apollo to send back an error.

Thoughts?

Would I need to do this with the Apollo client and Apollo server (say additional service requests such as databases, etc.)?

Upvotes: 1

Views: 5387

Answers (1)

helfer
helfer

Reputation: 7172

There are three different places where timeouts might make sense:

1. For the connection to the server

To have a timeout for requests sent to the server, you could build a wrapper around the network interface, which would reject query promises after x seconds.

2. For the query resolution on the GraphQL server

To implement a per-query timeout on the server, you could put the query start time on the context at the start of the query, and wrap each resolve function with a function that either returns the promise from the resolver, or rejects when the timeout has elapsed.

3. For the connection between your GraphQL server and the backends

To implement timeouts for requests to the backend, you can simply make the fetch-requests to the backends time out after a certain amount of time.

PS: It's worth noting that the solutions above will cause queries or requests to time out, but they won't cancel them, which means that your server or backends will most likely continue doing work that is now wasted. However, cancelling is an entirely different problem, and it's also harder to address.

Upvotes: 2

Related Questions