From 57f2287b7333e0417596cc48d7d57ac7b9f3697b Mon Sep 17 00:00:00 2001 From: Abhimanyu Singh Gaur <12651351+abhimanyusinghgaur@users.noreply.github.com> Date: Mon, 27 Jul 2020 11:57:41 +0530 Subject: [PATCH] fix bad URL for custom field --- graphql/e2e/custom_logic/cmd/main.go | 28 +++++++++---- graphql/e2e/custom_logic/custom_logic_test.go | 42 ++++++++++++------- graphql/resolve/resolver.go | 5 ++- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/graphql/e2e/custom_logic/cmd/main.go b/graphql/e2e/custom_logic/cmd/main.go index 53acfb40e7e..67c9d95185d 100644 --- a/graphql/e2e/custom_logic/cmd/main.go +++ b/graphql/e2e/custom_logic/cmd/main.go @@ -35,7 +35,8 @@ import ( ) type expectedRequest struct { - method string + method string + // Send urlSuffix as empty string to ignore comparison urlSuffix string body string // Send headers as nil to ignore comparing headers. @@ -172,7 +173,8 @@ func verifyRequest(r *http.Request, expectedRequest expectedRequest) error { return getError("Invalid HTTP method", r.Method) } - if !strings.HasSuffix(r.URL.String(), expectedRequest.urlSuffix) { + if expectedRequest.urlSuffix != "" && !strings.HasSuffix(r.URL.String(), + expectedRequest.urlSuffix) { return getError("Invalid URL", r.URL.String()) } @@ -317,15 +319,18 @@ func verifyCustomNameHeadersHandler(w http.ResponseWriter, r *http.Request) { func twitterFollwerHandler(w http.ResponseWriter, r *http.Request) { err := verifyRequest(r, expectedRequest{ - method: http.MethodGet, - urlSuffix: "/twitterfollowers?screen_name=manishrjain", - body: "", + method: http.MethodGet, + body: "", }) if err != nil { check2(w.Write([]byte(err.Error()))) return } - check2(w.Write([]byte(` + + var resp string + switch r.URL.Query().Get("screen_name") { + case "manishrjain": + resp = ` { "users": [{ "id": 1231723732206411776, @@ -337,7 +342,16 @@ func twitterFollwerHandler(w http.ResponseWriter, r *http.Request) { "friends_count": 117, "statuses_count": 0 }] - }`))) + }` + case "amazingPanda": + resp = ` + { + "users": [{ + "name": "twitter_bot" + }] + }` + } + check2(w.Write([]byte(resp))) } func favMoviesCreateHandler(w http.ResponseWriter, r *http.Request) { diff --git a/graphql/e2e/custom_logic/custom_logic_test.go b/graphql/e2e/custom_logic/custom_logic_test.go index 0e2c855681d..ba3a8bbcb97 100644 --- a/graphql/e2e/custom_logic/custom_logic_test.go +++ b/graphql/e2e/custom_logic/custom_logic_test.go @@ -2421,12 +2421,16 @@ func TestRestCustomLogicInDeepNestedField(t *testing.T) { params := &common.GraphQLParams{ Query: ` mutation{ - addUser(input:[{ - screen_name:"manishrjain", - tweets:[{ - text:"hello twitter" - }] - }]){ + addUser(input:[ + { + screen_name:"manishrjain", + tweets:[{text:"hello twitter"}] + } + { + screen_name:"amazingPanda", + tweets:[{text:"I love Kung fu."}] + } + ]){ numUids } }`, @@ -2456,16 +2460,24 @@ func TestRestCustomLogicInDeepNestedField(t *testing.T) { common.RequireNoGQLErrors(t, result) require.JSONEq(t, string(result.Data), ` { - "querySearchTweets": [{ - "text": "hello twitter", - "user": { - "screen_name": "manishrjain", - "followers": { - "users": [{ - "name": "hi_balaji" - }] + "querySearchTweets": [ + { + "text": "hello twitter", + "user": { + "screen_name": "manishrjain", + "followers": { + "users": [{"name": "hi_balaji"}] + } + } + },{ + "text": "I love Kung fu.", + "user": { + "screen_name": "amazingPanda", + "followers": { + "users": [{"name": "twitter_bot"}] + } } } - }] + ] }`) } diff --git a/graphql/resolve/resolver.go b/graphql/resolve/resolver.go index cc617e24d0c..af54059be34 100644 --- a/graphql/resolve/resolver.go +++ b/graphql/resolve/resolver.go @@ -949,10 +949,11 @@ func resolveCustomField(f schema.Field, vals []interface{}, mu *sync.RWMutex, er return } + url := fconf.URL if !graphql { // For REST requests, we'll have to substitute the variables used in the URL. mu.RLock() - fconf.URL, err = schema.SubstituteVarsInURL(fconf.URL, + url, err = schema.SubstituteVarsInURL(url, vals[idx].(map[string]interface{})) if err != nil { mu.RUnlock() @@ -966,7 +967,7 @@ func resolveCustomField(f schema.Field, vals []interface{}, mu *sync.RWMutex, er mu.RUnlock() } - b, err = makeRequest(nil, fconf.Method, fconf.URL, string(b), fconf.ForwardHeaders) + b, err = makeRequest(nil, fconf.Method, url, string(b), fconf.ForwardHeaders) if err != nil { errChan <- x.GqlErrorList{externalRequestError(err, f)} return