Skip to content

Commit

Permalink
fix(GraphQL): fixes wrong query parameter value for custom field URL (
Browse files Browse the repository at this point in the history
…#6074)

Fixes GRAPHQL-590.

This PR fixes wrong parameter value being supplied in the URL query parameters for `@custom` fields. Earlier, all the nodes used to get the same value for a query parameter, which has been fixed, and now every node gets the value corresponding to its dgraph fields in the `@custom` query parameters.
  • Loading branch information
abhimanyusinghgaur authored Jul 27, 2020
1 parent d82ecb4 commit 3f91f29
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
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 @@ -948,10 +948,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 @@ -965,7 +966,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

0 comments on commit 3f91f29

Please sign in to comment.