-
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.
Add GET query param support to handler
- Loading branch information
Showing
9 changed files
with
149 additions
and
50 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package graphql | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/vektah/graphql-go/errors" | ||
) | ||
|
||
type Resolver func(ctx context.Context, document string, operationName string, variables map[string]interface{}, w io.Writer) []*errors.QueryError | ||
|
||
type errorResponse struct { | ||
Errors []*errors.QueryError `json:"errors"` | ||
} | ||
|
||
func Handler(resolver Resolver) http.HandlerFunc { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
var params struct { | ||
Query string `json:"query"` | ||
OperationName string `json:"operationName"` | ||
Variables map[string]interface{} `json:"variables"` | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
|
||
if r.Method == "GET" { | ||
params.Query = r.URL.Query().Get("query") | ||
params.OperationName = r.URL.Query().Get("operationName") | ||
|
||
if variables := r.URL.Query().Get("variables"); variables != "" { | ||
if err := json.Unmarshal([]byte(variables), ¶ms.Variables); err != nil { | ||
sendError(w, http.StatusBadRequest, []*errors.QueryError{errors.Errorf("variables could not be decoded")}) | ||
return | ||
} | ||
} | ||
} else { | ||
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil { | ||
sendError(w, http.StatusBadRequest, []*errors.QueryError{errors.Errorf("json body could not be decoded")}) | ||
return | ||
} | ||
} | ||
|
||
errs := resolver(r.Context(), params.Query, params.OperationName, params.Variables, w) | ||
if errs != nil { | ||
sendError(w, http.StatusUnprocessableEntity, errs) | ||
} | ||
}) | ||
} | ||
|
||
func sendError(w http.ResponseWriter, code int, errs []*errors.QueryError) { | ||
w.WriteHeader(code) | ||
|
||
b, err := json.Marshal(errorResponse{Errors: errs}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
w.Write(b) | ||
} |
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,79 @@ | ||
package graphql | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/vektah/graphql-go/errors" | ||
) | ||
|
||
func TestHandlerPOST(t *testing.T) { | ||
h := Handler(func(ctx context.Context, document string, operationName string, variables map[string]interface{}, w io.Writer) []*errors.QueryError { | ||
if document == "error" { | ||
return []*errors.QueryError{errors.Errorf("error!")} | ||
} | ||
|
||
w.Write([]byte(`{"data":{}}`)) | ||
return nil | ||
}) | ||
|
||
t.Run("ok", func(t *testing.T) { | ||
resp := doRequest(h, "POST", "/graphql", `{"query":"me{id}"}`) | ||
assert.Equal(t, http.StatusOK, resp.Code) | ||
assert.Equal(t, `{"data":{}}`, resp.Body.String()) | ||
}) | ||
|
||
t.Run("decode failure", func(t *testing.T) { | ||
resp := doRequest(h, "POST", "/graphql", "notjson") | ||
assert.Equal(t, http.StatusBadRequest, resp.Code) | ||
assert.Equal(t, `{"errors":[{"message":"json body could not be decoded"}]}`, resp.Body.String()) | ||
}) | ||
|
||
t.Run("execution failure", func(t *testing.T) { | ||
resp := doRequest(h, "POST", "/graphql", `{"query":"error"}`) | ||
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code) | ||
assert.Equal(t, `{"errors":[{"message":"error!"}]}`, resp.Body.String()) | ||
}) | ||
} | ||
|
||
func TestHandlerGET(t *testing.T) { | ||
h := Handler(func(ctx context.Context, document string, operationName string, variables map[string]interface{}, w io.Writer) []*errors.QueryError { | ||
if document == "error" { | ||
return []*errors.QueryError{errors.Errorf("error!")} | ||
} | ||
|
||
w.Write([]byte(`{"data":{}}`)) | ||
return nil | ||
}) | ||
|
||
t.Run("just query", func(t *testing.T) { | ||
resp := doRequest(h, "GET", "/graphql?query=me{id}", ``) | ||
assert.Equal(t, http.StatusOK, resp.Code) | ||
assert.Equal(t, `{"data":{}}`, resp.Body.String()) | ||
}) | ||
|
||
t.Run("decode failure", func(t *testing.T) { | ||
resp := doRequest(h, "GET", "/graphql?query=me{id}&variables=notjson", "") | ||
assert.Equal(t, http.StatusBadRequest, resp.Code) | ||
assert.Equal(t, `{"errors":[{"message":"variables could not be decoded"}]}`, resp.Body.String()) | ||
}) | ||
|
||
t.Run("execution failure", func(t *testing.T) { | ||
resp := doRequest(h, "GET", "/graphql?query=error", "") | ||
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code) | ||
assert.Equal(t, `{"errors":[{"message":"error!"}]}`, resp.Body.String()) | ||
}) | ||
} | ||
|
||
func doRequest(handler http.Handler, method string, target string, body string) *httptest.ResponseRecorder { | ||
r := httptest.NewRequest(method, target, strings.NewReader(body)) | ||
w := httptest.NewRecorder() | ||
|
||
handler.ServeHTTP(w, r) | ||
return w | ||
} |
This file was deleted.
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