MCPcopy
hub / github.com/dgraph-io/dgraph / queryHandler

Function queryHandler

dgraph/cmd/alpha/http.go:184–343  ·  view source on GitHub ↗

This method should just build the request and proxy it to the Query method of dgraph.Server. It can then encode the response as appropriate before sending it back to the user.

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

182// This method should just build the request and proxy it to the Query method of dgraph.Server.
183// It can then encode the response as appropriate before sending it back to the user.
184func queryHandler(w http.ResponseWriter, r *http.Request) {
185 if commonHandler(w, r) {
186 return
187 }
188
189 isDebugMode, err := parseBool(r, "debug")
190 if err != nil {
191 x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
192 return
193 }
194 queryTimeout, err := parseDuration(r, "timeout")
195 if err != nil {
196 x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
197 return
198 }
199 startTs, err := parseUint64(r, "startTs")
200 hash := r.URL.Query().Get("hash")
201 if err != nil {
202 x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
203 return
204 }
205
206 body := readRequest(w, r)
207 if body == nil {
208 return
209 }
210
211 var params struct {
212 Query string `json:"query"`
213 Variables map[string]string `json:"variables"`
214 }
215
216 contentType := r.Header.Get("Content-Type")
217 mediaType, contentTypeParams, err := mime.ParseMediaType(contentType)
218 if err != nil {
219 x.SetStatus(w, x.ErrorInvalidRequest, "Invalid Content-Type")
220 }
221 if charset, ok := contentTypeParams["charset"]; ok && strings.ToLower(charset) != "utf-8" {
222 x.SetStatus(w, x.ErrorInvalidRequest, "Unsupported charset. "+
223 "Supported charset is UTF-8")
224 return
225 }
226
227 switch mediaType {
228 case "application/json":
229 if err := json.Unmarshal(body, &params); err != nil {
230 jsonErr := convertJSONError(string(body), err)
231 x.SetStatus(w, x.ErrorInvalidRequest, jsonErr.Error())
232 return
233 }
234 case "application/graphql+-", "application/dql":
235 params.Query = string(body)
236 default:
237 x.SetStatus(w, x.ErrorInvalidRequest, "Unsupported Content-Type. "+
238 "Supported content types are application/json, application/graphql+-,application/dql")
239 return
240 }
241

Callers

nothing calls this directly

Calls 15

SetStatusFunction · 0.92
AttachAccessJwtFunction · 0.92
AttachRemoteIPFunction · 0.92
SetStatusWithDataFunction · 0.92
Check2Function · 0.92
CheckFunction · 0.92
WriteResponseFunction · 0.92
commonHandlerFunction · 0.85
parseBoolFunction · 0.85
parseDurationFunction · 0.85
parseUint64Function · 0.85
readRequestFunction · 0.85

Tested by

no test coverage detected