Skip to content

Commit

Permalink
Start drafting new handler interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Sep 30, 2019
1 parent 4c35356 commit 249b602
Show file tree
Hide file tree
Showing 7 changed files with 574 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/gorilla/websocket v1.2.0
github.com/hashicorp/golang-lru v0.5.0
github.com/kr/pretty v0.1.0 // indirect
github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd // indirect
github.com/mitchellh/mapstructure v0.0.0-20180203102830-a4e142e9c047
github.com/opentracing/basictracer-go v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.0.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd h1:HvFwW+cm9bCbZ/+vuGNq7CRWXql8c0y8nGeYpqmpvmk=
github.com/matryer/moq v0.0.0-20190312154309-6cfb0558e1bd/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ=
github.com/mitchellh/mapstructure v0.0.0-20180203102830-a4e142e9c047 h1:zCoDWFD5nrJJVjbXiDZcVhOBSzKn3o9LgRLLMRNuru8=
github.com/mitchellh/mapstructure v0.0.0-20180203102830-a4e142e9c047/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/opentracing/basictracer-go v1.0.0 h1:YyUAhaEfjoWXclZVJ9sGoNct7j4TVk7lZWlQw5UXuoo=
Expand Down
279 changes: 279 additions & 0 deletions graphql/esmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions graphql/exec.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:generate go run github.com/matryer/moq -out esmock.go . ExecutableSchema

package graphql

import (
Expand Down
55 changes: 55 additions & 0 deletions graphql/handler/http_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package handler

import (
"encoding/json"
"io"
"net/http"
"strings"

"github.com/99designs/gqlgen/graphql"
)

type HTTPGet struct{}

func (H HTTPGet) Supports(r *http.Request) bool {
if r.Header.Get("Upgrade") != "" {
return false
}

return r.Method == "GET"
}

func (H HTTPGet) Do(w http.ResponseWriter, r *http.Request) (*graphql.RequestContext, Writer) {
var reqParams graphql.RequestContext

reqParams.RawQuery = r.URL.Query().Get("query")
reqParams.OperationName = r.URL.Query().Get("operationName")

if variables := r.URL.Query().Get("variables"); variables != "" {
if err := jsonDecode(strings.NewReader(variables), &reqParams.Variables); err != nil {
sendErrorf(w, http.StatusBadRequest, "variables could not be decoded")
return nil, nil
}
}

if extensions := r.URL.Query().Get("extensions"); extensions != "" {
if err := jsonDecode(strings.NewReader(extensions), &reqParams.Extensions); err != nil {
sendErrorf(w, http.StatusBadRequest, "extensions could not be decoded")
return nil, nil
}
}

return &reqParams, func(response *graphql.Response) {
b, err := json.Marshal(response)
if err != nil {
panic(err)
}
w.Write(b)
}
}

func jsonDecode(r io.Reader, val interface{}) error {
dec := json.NewDecoder(r)
dec.UseNumber()
return dec.Decode(val)
}

0 comments on commit 249b602

Please sign in to comment.