Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GraphQL): fixes wrong query parameter value for custom field URL #6074

Merged
merged 1 commit into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions graphql/e2e/custom_logic/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
42 changes: 27 additions & 15 deletions graphql/e2e/custom_logic/custom_logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}`,
Expand Down Expand Up @@ -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"}]
}
}
}
}]
]
}`)
}
5 changes: 3 additions & 2 deletions graphql/resolve/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down