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

graphql: Fix mutation on predicate with special characters having dgraph directive. #5526

Merged
merged 9 commits into from
May 29, 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
32 changes: 32 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,38 @@ func queryWithAlias(t *testing.T) {
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))
}

func queryWithCascade(t *testing.T) {
// for testing @cascade with get by ID and filter queries, also for testing @cascade on field
authors := addMultipleAuthorFromRef(t, []*author{
Expand Down
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 @@ -773,8 +773,7 @@ func deleteAuthSelector(t schema.Type) *schema.RuleNode {

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 @@ -1009,6 +1008,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 @@ -1110,8 +1113,7 @@ func asIDReference(
ctx context.Context,
val interface{},
srcField schema.FieldDefinition,
srcUID string,
variable string,
srcUID, variable string,
withAdditionalDeletes bool,
varGen *VariableGenerator) *mutationFragment {

Expand Down
5 changes: 5 additions & 0 deletions graphql/resolve/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ type Mutation {
})
}

type Message {
content: String! @dgraph(pred: "post")
author: String @dgraph(pred: "<职业>")
}

interface X {
id: ID!
username: String! @id
Expand Down
14 changes: 14 additions & 0 deletions graphql/schema/dgraph_schemagen_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,20 @@ schemas:
name
}

- name: "fields with @dgraph(pred: ...) contain different language."
input: |
type A {
content: String! @dgraph(pred: "post") @search(by: [exact, term])
author: String @dgraph(pred: "<公司>") @search(by: [exact, term])
}
output: |
type A {
post
<公司>
}
post: string @index(exact, term) .
<公司>: string @index(exact, term) .

-
name: "custom types shouldn't be part of Dgraph schema"
input: |
Expand Down
19 changes: 2 additions & 17 deletions graphql/schema/gqlschema_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2032,13 +2032,15 @@ 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: "<职业>")
}

-
Expand Down Expand Up @@ -2106,23 +2108,6 @@ valid_schemas:
})
}

- name: "@dgraph predicate type validation gives no errors with non-null variations"
input: |
type X {
f1: String @dgraph(pred: "f1")
f2: [String] @dgraph(pred: "f2")
f3: [String!] @dgraph(pred: "f3")
f4: [String]! @dgraph(pred: "f4")
f5: String
}
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")
}

-
name: "@custom directive with variable definitions in operation in graphql"
input: |
Expand Down