-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start drafting new handler interfaces
- Loading branch information
Showing
7 changed files
with
574 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.