From ee9ff064694c445a3a6972001ccbce2cc5b9c3f2 Mon Sep 17 00:00:00 2001
From: Ahmet Avci <ahmetabdullahavci07@gmail.com>
Date: Tue, 8 Nov 2022 15:14:14 +0300
Subject: [PATCH] graphql: add query timeout (#26116)

This PR adds a 60 second timeout to graphql queries.
---
 graphql/service.go | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/graphql/service.go b/graphql/service.go
index 6f6e58335..684fdc712 100644
--- a/graphql/service.go
+++ b/graphql/service.go
@@ -17,8 +17,10 @@
 package graphql
 
 import (
+	"context"
 	"encoding/json"
 	"net/http"
+	"time"
 
 	"github.com/ethereum/go-ethereum/eth/filters"
 	"github.com/ethereum/go-ethereum/internal/ethapi"
@@ -41,7 +43,10 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	response := h.Schema.Exec(r.Context(), params.Query, params.OperationName, params.Variables)
+	ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
+	defer cancel()
+
+	response := h.Schema.Exec(ctx, params.Query, params.OperationName, params.Variables)
 	responseJSON, err := json.Marshal(response)
 	if err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)