MCPcopy
hub / github.com/dosco/graphjin / apiV1GraphQL

Method apiV1GraphQL

serv/http.go:116–241  ·  view source on GitHub ↗

apiV1GraphQLHandler handles the GraphQL API requests

(ns *string, ah auth.HandlerFunc)

Source from the content-addressed store, hash-verified

114
115// apiV1GraphQLHandler handles the GraphQL API requests
116func (s1 *HttpService) apiV1GraphQL(ns *string, ah auth.HandlerFunc) http.Handler {
117 dtrace := otel.GetTextMapPropagator()
118
119 h := func(w http.ResponseWriter, r *http.Request) {
120 var err error
121
122 start := time.Now()
123 s := s1.Load().(*graphjinService)
124
125 w.Header().Set("Content-Type", "application/json")
126
127 if isWebSocketUpgrade(r) {
128 s.apiV1Ws(w, r, ah)
129 return
130 }
131
132 var req gqlReq
133
134 ctx, opts := newDTrace(dtrace, r)
135 ctx, span := s.spanStart(ctx, "GraphQL Request", opts...)
136 defer span.End()
137
138 switch r.Method {
139 case "POST":
140 if isMultipartRequest(r) {
141 if !s.conf.Uploads.Enabled {
142 err = errMultipartDisabled
143 break
144 }
145 var backend fstable.Backend
146 if name := s.conf.Uploads.Storage; name != "" {
147 b, ok := s.gj.FilesystemBackend(name)
148 if !ok {
149 err = fmt.Errorf("uploads: storage filesystem %q not configured", name)
150 break
151 }
152 backend = b
153 }
154 req, err = parseMultipartGraphQL(r, s.conf.Uploads, backend)
155 break
156 }
157 var b []byte
158 b, err = io.ReadAll(io.LimitReader(r.Body, maxReadBytes))
159 if err == nil {
160 defer r.Body.Close() //nolint:errcheck
161 err = json.Unmarshal(b, &req)
162 }
163
164 case "GET":
165 q := r.URL.Query()
166 req.Query = q.Get("query")
167 req.OpName = q.Get("operationName")
168 req.Vars = json.RawMessage(q.Get("variables"))
169
170 if ext := q.Get("extensions"); ext != "" {
171 err = json.Unmarshal([]byte(ext), &req.Ext)
172 }
173 }

Callers 1

apiHandlerMethod · 0.95

Calls 15

apqEnabledMethod · 0.95
SetNamespaceMethod · 0.95
newDTraceFunction · 0.85
isMultipartRequestFunction · 0.85
parseMultipartGraphQLFunction · 0.85
spanErrorFunction · 0.85
renderErrFunction · 0.85
isSSERequestFunction · 0.85
apiV1WsMethod · 0.80
FilesystemBackendMethod · 0.80
setHeaderVarsMethod · 0.80
applyIdentityContextMethod · 0.80

Tested by

no test coverage detected