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

feat(GraphQL): @custom HTTP body now supports hardcoded scalars #6157

Merged
merged 5 commits into from
Aug 12, 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
2 changes: 1 addition & 1 deletion graphql/e2e/custom_logic/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func postFavMoviesWithBodyHandler(w http.ResponseWriter, r *http.Request) {
err := verifyRequest(r, expectedRequest{
method: http.MethodPost,
urlSuffix: "/0x123?name=Author",
body: `{"id":"0x123","name":"Author"}`,
body: `{"id":"0x123","movie_type":"space","name":"Author"}`,
headers: nil,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion graphql/e2e/custom_logic/custom_logic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestCustomPostQueryWithBody(t *testing.T) {
type Query {
myFavoriteMoviesPost(id: ID!, name: String!, num: Int): [Movie] @custom(http: {
url: "http://mock:8888/favMoviesPostWithBody/$id?name=$name",
body:"{id:$id,name:$name,num:$num}"
body:"{id:$id,name:$name,num:$num,movie_type:\"space\"}"
method: "POST"
})
}`
Expand Down
8 changes: 1 addition & 7 deletions graphql/resolve/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,13 +852,7 @@ func resolveCustomField(f schema.Field, vals []interface{}, mu *sync.RWMutex, er
}

mu.RLock()
if err := schema.SubstituteVarsInBody(&temp, vals[i].(map[string]interface{})); err != nil {
errCh <- x.GqlErrorf("Evaluation of custom field failed while substituting "+
"variables into body for remote endpoint with an error: %s for field: %s "+
"within type: %s.", err, f.Name(), f.GetObjectName()).WithLocations(f.Location())
mu.RUnlock()
return
}
schema.SubstituteVarsInBody(&temp, vals[i].(map[string]interface{}))
mu.RUnlock()
inputs[i] = temp
}
Expand Down
22 changes: 19 additions & 3 deletions graphql/schema/gqlschema_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1175,11 +1175,11 @@ invalid_schemas:
getAuthor1(id: ID): Author! @custom(http: {
url: "http://google.com/",
method: "POST",
body: "{id: $id, name: \"name\"}",
body: "{id: $id, name: name}",
})
}
errlist: [
{"message": "Type Query; Field getAuthor1; body template inside @custom directive could not be parsed.",
{"message": "Type Query; Field getAuthor1; body template inside @custom directive could not be parsed: found unexpected value: name",
"locations":[{"line":10, "column":12}]},
]

Expand Down Expand Up @@ -2636,7 +2636,7 @@ valid_schemas:
}

-
name: "Query, Mutation in initial schema with @custom directive"
name: "initial schema with @custom directive"
input: |
type Author {
id: ID!
Expand All @@ -2645,9 +2645,25 @@ valid_schemas:
input AuthorUpdate {
id: ID!
}
type Country {
country_code: String! @id
authors: [Author] @custom(http: {
url: "http://blah.com",
method: "POST"
graphql: "query ($input: [AuthorInput]) { authors(input: $input) }"
body: "{country_code: $country_code, version: ONE}"
mode: BATCH
skipIntrospection: true
})
}

type Query {
getMyAuthor(id: ID): Author! @custom(http: {url: "http://blah.com", method: "GET"})
getAuthorsForCountry(country_code: String!): [Author] @custom(http: {
url: "http://blah.com"
method: "POST"
body: "{country_code: $country_code, version: 1, tag: \"temp\"}"
})
}
type Mutation {
updateMyAuthor(input: AuthorUpdate!): Author! @custom(http: {url: "http://blah.com",
Expand Down
8 changes: 4 additions & 4 deletions graphql/schema/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,11 +1418,11 @@ func customDirectiveValidation(sch *ast.Schema,
// 8. Validating body
var requiredFields map[string]bool
if body != nil {
_, requiredFields, err = parseBodyTemplate(body.Raw)
_, requiredFields, err = parseBodyTemplate(body.Raw, graphql == nil)
if err != nil {
errs = append(errs, gqlerror.ErrorPosf(body.Position,
"Type %s; Field %s; body template inside @custom directive could not be parsed.",
typ.Name, field.Name))
"Type %s; Field %s; body template inside @custom directive could not be parsed: %s",
typ.Name, field.Name, err.Error()))
}
// Validating params to body template for Query/Mutation types. For other types the
// validation is performed later along with graphql.
Expand Down Expand Up @@ -1564,7 +1564,7 @@ func customDirectiveValidation(sch *ast.Schema,
bodyBuilder.WriteString(comma)
}
bodyBuilder.WriteString("}")
_, requiredVars, err := parseBodyTemplate(bodyBuilder.String())
_, requiredVars, err := parseBodyTemplate(bodyBuilder.String(), false)
if err != nil {
errs = append(errs, gqlerror.ErrorPosf(graphql.Position,
"Type %s; Field %s: inside graphql in @custom directive, "+
Expand Down
Loading