...
1 package transport
2
3 import (
4 "net/http"
5 "strings"
6
7 "github.com/99designs/gqlgen/graphql"
8 )
9
10
11 type Options struct {
12
13 AllowedMethods []string
14 }
15
16 var _ graphql.Transport = Options{}
17
18 func (o Options) Supports(r *http.Request) bool {
19 return r.Method == "HEAD" || r.Method == "OPTIONS"
20 }
21
22 func (o Options) Do(w http.ResponseWriter, r *http.Request, exec graphql.GraphExecutor) {
23 switch r.Method {
24 case http.MethodOptions:
25 w.Header().Set("Allow", o.allowedMethods())
26 w.WriteHeader(http.StatusOK)
27 case http.MethodHead:
28 w.WriteHeader(http.StatusMethodNotAllowed)
29 }
30 }
31
32 func (o Options) allowedMethods() string {
33 if len(o.AllowedMethods) == 0 {
34 return "OPTIONS, GET, POST"
35 }
36 return strings.Join(o.AllowedMethods, ", ")
37 }
38
View as plain text