GraphQL query timeout and complexity management

An overview of solutions to prevent expensive GraphQL queries from impacting your API performance.

Image for post
Image for post

A single GraphQL query can potentially generate thousands of database operations.

Persisted queries

This solution is well-suited when you manage both backend and frontend development.

Request size limit and request timeout

Query complexity analysis

But one of the main GraphQL advantages is to allow this kind of “deep” query! It’s like limiting the number of joins in a SQL query.

Cost analysis

The cost unit definition can be based on the number of generated SQL queries or subsequent REST API calls or the resolver function execution average duration.

Resolver function timeout

Image for post
Image for post

Resolver function operations count

Request: {
requester(request, args, context) {
context.incrementResolverCount();
return context.loaders.users.load(request.requesterId);
},

GraphQL query timeout

request.incrementResolverCount =  function () {
var runTime = Date.now() - startTime;
if (runTime > config.graphql.queryTimeout) {
if (request.logTimeoutError) {
logger('ERROR', 'Request ' + request.uuid + ' query execution timeout');
}
request.logTimeoutError = false;
throw 'Query execution has timeout. Field resolution aborted';
}
this.resolverCount++;
};

Conclusion

The inherent backend complexity and performance management of some GraphQL queries should be transparent for frontend developers to make API usage as smooth as possible with the highest level of flexibility.

Written by

Creator of WorkflowGen, Advantys co-founder and CTO.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store