From 8dc17b470d339f5fcd6e1cf9a3d14dec2df4067e Mon Sep 17 00:00:00 2001 From: DBL-Lee Date: Fri, 31 May 2019 15:30:08 -0700 Subject: [PATCH] support GET for apq --- handler/graphql.go | 7 +++++++ handler/graphql_test.go | 22 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/handler/graphql.go b/handler/graphql.go index 78deb6fb485..6f965f86000 100644 --- a/handler/graphql.go +++ b/handler/graphql.go @@ -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 { diff --git a/handler/graphql_test.go b/handler/graphql_test.go index a2627c77197..93a257706de 100644 --- a/handler/graphql_test.go +++ b/handler/graphql_test.go @@ -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) @@ -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()) + }) }