Skip to content

Commit

Permalink
support GET for apq
Browse files Browse the repository at this point in the history
  • Loading branch information
DBL-Lee committed May 31, 2019
1 parent d36932c commit 8dc17b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions handler/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ func (gh *graphqlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
}

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
}
}
case http.MethodPost:
mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
Expand Down
22 changes: 21 additions & 1 deletion handler/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ func TestBytesRead(t *testing.T) {

func TestAutomaticPersistedQuery(t *testing.T) {
h := GraphQL(&executableSchemaStub{}, APQCacheSize(1000))
t.Run("automatic persisted query", func(t *testing.T) {
t.Run("automatic persisted query POST", func(t *testing.T) {
// normal queries should be unaffected
resp := doRequest(h, "POST", "/graphql", `{"query":"{ me { name } }"}`)
assert.Equal(t, http.StatusOK, resp.Code)
Expand All @@ -786,4 +786,24 @@ func TestAutomaticPersistedQuery(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
})

t.Run("automatic persisted query GET", func(t *testing.T) {
// normal queries should be unaffected
resp := doRequest(h, "GET", "/graphql?query={me{name}}", "")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())

// first pass: optimistic hash without query string
resp = doRequest(h, "GET", `/graphql?extensions={"persistedQuery":{"version":1,"sha256Hash":"b58723c4fd7ce18043ae53635b304ba6cee765a67009645b04ca01e80ce1c065"}}`, "")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"errors":[{"message":"PersistedQueryNotFound"}],"data":null}`, resp.Body.String())
// second pass: query with query string and query hash
resp = doRequest(h, "GET", `/graphql?query={me{name}}&extensions={"persistedQuery":{"sha256Hash":"b58723c4fd7ce18043ae53635b304ba6cee765a67009645b04ca01e80ce1c065","version":1}}}`, "")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
// future requests without query string
resp = doRequest(h, "GET", `/graphql?extensions={"persistedQuery":{"version":1,"sha256Hash":"b58723c4fd7ce18043ae53635b304ba6cee765a67009645b04ca01e80ce1c065"}}`, "")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
})
}

0 comments on commit 8dc17b4

Please sign in to comment.