Skip to content

Commit

Permalink
graphql: Fix mutation on predicate with special characters having dgr…
Browse files Browse the repository at this point in the history
…aph directive. (#5526)

* Fix Dgraph directive for multiple language.

(cherry picked from commit 92232c0)
  • Loading branch information
Arijit Das committed Jun 4, 2020
1 parent ea9bd79 commit c1d94b8
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 5 deletions.
32 changes: 32 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,3 +1425,35 @@ func queryWithAlias(t *testing.T) {
"postAuthor": { "theName": "Ann Author" }}]}`,
string(gqlResponse.Data))
}

func DgraphDirectiveWithSpecialCharacters(t *testing.T) {
mutation := &GraphQLParams{
Query: `
mutation {
addMessage(input : [{content : "content1", author: "author1"}]) {
message {
content
author
}
}
}`,
}
result := `{"addMessage":{"message":[{"content":"content1","author":"author1"}]}}`
gqlResponse := mutation.ExecuteAsPost(t, graphqlURL)
requireNoGQLErrors(t, gqlResponse)
require.JSONEq(t, result, string(gqlResponse.Data))

queryParams := &GraphQLParams{
Query: `
query {
queryMessage {
content
author
}
}`,
}
result = `{"queryMessage":[{"content":"content1","author":"author1"}]}`
gqlResponse = queryParams.ExecuteAsPost(t, graphqlURL)
requireNoGQLErrors(t, gqlResponse)
require.JSONEq(t, result, string(gqlResponse.Data))
}
15 changes: 15 additions & 0 deletions graphql/e2e/directives/dgraph_directives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

func TestRunAll_WithDgraphDirectives(t *testing.T) {
common.RunAll(t)
t.Run("dgraph predicate with special characters",
common.DgraphDirectiveWithSpecialCharacters)
}

func TestSchema_WithDgraphDirectives(t *testing.T) {
Expand Down Expand Up @@ -200,6 +202,12 @@ func TestSchema_WithDgraphDirectives(t *testing.T) {
"index": true,
"tokenizer": ["hash"],
"upsert": true
}, {
"predicate": "post",
"type": "string"
},{
"predicate": "职业",
"type": "string"
}, {
"predicate": "People.name",
"type": "string"
Expand Down Expand Up @@ -297,6 +305,13 @@ func TestSchema_WithDgraphDirectives(t *testing.T) {
"name": "dgraph.graphql.xid"
}],
"name": "dgraph.graphql"
}, {
"fields": [{
"name": "post"
}, {
"name": "职业"
}],
"name": "Message"
}, {
"fields": [{
"name": "myPost.title"
Expand Down
5 changes: 5 additions & 0 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ type Teacher implements People {
type Student implements People {
taughtBy: [Teacher]
}

type Message {
content: String! @dgraph(pred: "post")
author: String @dgraph(pred: "<职业>")
}
2 changes: 1 addition & 1 deletion graphql/e2e/directives/test_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@
"State.name": "Nusa",
"State.xcode": "nusa"
}
]
]
20 changes: 20 additions & 0 deletions graphql/resolve/add_mutation_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@
"Author.posts":[]
}
-
name: "Add mutation for predicates with special characters having @dgraph directive."
gqlmutation: |
mutation {
addMessage(input : [{content : "content1", author: "author1"}]) {
message {
content
author
}
}
}
dgmutations:
- setjson: |
{
"uid":"_:Message1",
"dgraph.type":["Message"],
"职业":"author1",
"post":"content1"
}
-
name: "Add multiple mutation with variables"
gqlmutation: |
Expand Down
10 changes: 6 additions & 4 deletions graphql/resolve/mutation_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,7 @@ func asUID(val interface{}) (uint64, error) {

func mutationsFromFragments(
frags []*mutationFragment,
setBuilder mutationBuilder,
delBuilder mutationBuilder) ([]*dgoapi.Mutation, error) {
setBuilder, delBuilder mutationBuilder) ([]*dgoapi.Mutation, error) {

mutations := make([]*dgoapi.Mutation, 0, len(frags))
var errs x.GqlErrorList
Expand Down Expand Up @@ -875,6 +874,10 @@ func rewriteObject(

fieldDef := typ.Field(field)
fieldName := typ.DgraphPredicate(field)
// This fixes mutation when dgraph predicate has special characters. PR #5526
if strings.HasPrefix(fieldName, "<") && strings.HasSuffix(fieldName, ">") {
fieldName = fieldName[1 : len(fieldName)-1]
}

switch val := val.(type) {
case map[string]interface{}:
Expand Down Expand Up @@ -975,8 +978,7 @@ func checkQueryResult(qry string, yes, no error) resultChecker {
func asIDReference(
val interface{},
srcField schema.FieldDefinition,
srcUID string,
variable string,
srcUID, variable string,
withAdditionalDeletes bool,
varGen *VariableGenerator) *mutationFragment {

Expand Down
2 changes: 2 additions & 0 deletions graphql/schema/gqlschema_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -864,11 +864,13 @@ valid_schemas:
f3: [String!] @dgraph(pred: "f3")
f4: [String]! @dgraph(pred: "f4")
f5: String
f6: String @dgraph(pred: "<职业>")
}
type Y {
f1: String! @dgraph(pred: "f1")
f2: [String!] @dgraph(pred: "f2")
f3: [String]! @dgraph(pred: "f3")
f4: [String!]! @dgraph(pred: "f4")
f5: String @dgraph(pred: "X.f5")
f6: String @dgraph(pred: "<职业>")
}

0 comments on commit c1d94b8

Please sign in to comment.