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): Add language tag support in GraphQL #7663

Merged
merged 28 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
739514d
added code for supporting language tags in graphql
JatinDev543 Mar 26, 2021
bd8026a
added schema test and modified some validations`
JatinDev543 Mar 26, 2021
010468b
added invalid schema tests for language tags
JatinDev543 Mar 27, 2021
6d9640c
added unit tests for mutation and query rewriting
JatinDev543 Mar 27, 2021
480e471
Merge branch 'master' of github.com:dgraph-io/dgraph into jatin/GRAPH…
JatinDev543 Mar 27, 2021
68a36a1
added e2e test
JatinDev543 Mar 27, 2021
b0ed0ea
clean test
JatinDev543 Mar 27, 2021
4eb535d
fix test
JatinDev543 Mar 27, 2021
058906e
added schema for directive test
JatinDev543 Mar 27, 2021
6f746db
fixed formatting
JatinDev543 Mar 28, 2021
7746323
clean code
JatinDev543 Mar 28, 2021
1e3b145
clean code
JatinDev543 Mar 28, 2021
9691175
clean code, added comments
JatinDev543 Mar 28, 2021
1195b19
addressed comments
JatinDev543 Mar 31, 2021
f86b4b7
added some more tests and modified schema
JatinDev543 Apr 1, 2021
e073f83
refactor code
JatinDev543 Apr 1, 2021
af62712
clean code
JatinDev543 Apr 1, 2021
5d943ad
fix test and indentation
JatinDev543 Apr 1, 2021
1aa1078
clean comment
JatinDev543 Apr 1, 2021
6a205d8
fixed schema test
JatinDev543 Apr 1, 2021
1905eec
added error case
JatinDev543 Apr 1, 2021
887550e
modify schema for tests
JatinDev543 Apr 1, 2021
fa159b3
addressed abhimanyu's comments
JatinDev543 Apr 1, 2021
98c69f8
fixed test formatting
JatinDev543 Apr 1, 2021
fc41d46
fixed test order issue
JatinDev543 Apr 1, 2021
f794fc5
addressed abhimanyu's comments
JatinDev543 Apr 5, 2021
02273de
addressed abhimanyu's comment
JatinDev543 Apr 6, 2021
07f080d
simplify logic
abhimanyusinghgaur Apr 6, 2021
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
4 changes: 2 additions & 2 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ func JSONEqGraphQL(t *testing.T, expected, actual string) {
actual = strings.ReplaceAll(actual, "\r", "")
actual = strings.ReplaceAll(actual, "\n", "")
actual = strings.ReplaceAll(actual, "\t", "")

require.Equal(t, expected, actual)
}

Expand Down Expand Up @@ -836,7 +835,7 @@ func RunAll(t *testing.T) {
t.Run("query id directive with int", idDirectiveWithInt)
t.Run("query id directive with int64", idDirectiveWithInt64)
t.Run("query filter ID values coercion to List", queryFilterWithIDInputCoercion)

t.Run("query multiple language Fields", queryMultipleLangFields)
// mutation tests
t.Run("add mutation", addMutation)
t.Run("update mutation by ids", updateMutationByIds)
Expand Down Expand Up @@ -894,6 +893,7 @@ func RunAll(t *testing.T) {
t.Run("input coercion to list", inputCoerciontoList)
t.Run("multiple external Id's tests", multipleXidsTests)
t.Run("Upsert Mutation Tests", upsertMutationTests)
t.Run("Update language tag fields", updateLangTagFields)

// error tests
t.Run("graphql completion on", graphQLCompletionOn)
Expand Down
64 changes: 64 additions & 0 deletions graphql/e2e/common/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6018,3 +6018,67 @@ func upsertMutationTests(t *testing.T) {
filter = GetXidFilter("xcode", []interface{}{"S1", "S10"})
deleteState(t, filter, 2, nil)
}

func updateLangTagFields(t *testing.T) {
addPersonParams := &GraphQLParams{
Query: `
mutation addPerson($person: [AddPersonInput!]!) {
addPerson(input: $person) {
numUids
}
}`,
}
addPersonParams.Variables = map[string]interface{}{"person": []interface{}{
map[string]interface{}{
"name": "Juliet",
"nameHi": "जूलियट",
"nameZh": "朱丽叶",
},
},
}
gqlResponse := addPersonParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
// update Person using language tag field
updatePersonParams := &GraphQLParams{
Query: `
mutation updatePerson {
updatePerson(
input: {
filter: { nameHi: { eq: "जूलियट" } }
set: { nameHi: "जूली", nameZh: "朱丽叶" }
}
) {
numUids
}
}`,
}
gqlResponse = updatePersonParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

queryPerson := &GraphQLParams{
Query: `
query {
queryPerson(filter: { name: { eq: "Juliet" } }) {
name
nameZh
nameHi
}
}`,
}
gqlResponse = queryPerson.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

queryPersonExpected := `
{
"queryPerson": [
{
"name": "Juliet",
"nameZh": "朱丽叶",
"nameHi": "जूली"
}
]
}`

testutil.CompareJSON(t, queryPersonExpected, string(gqlResponse.Data))
DeleteGqlType(t, "Person", map[string]interface{}{}, 1, nil)
}
70 changes: 70 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3913,3 +3913,73 @@ func idDirectiveWithInt(t *testing.T) {
}`
require.JSONEq(t, expected, string(response.Data))
}

func queryMultipleLangFields(t *testing.T) {
addPersonParams := &GraphQLParams{
Query: `
mutation addPerson($person: [AddPersonInput!]!) {
addPerson(input: $person) {
numUids
}
}`,
}

// add three Persons
addPersonParams.Variables = map[string]interface{}{"person": []interface{}{
map[string]interface{}{
"name": "Bob",
"professionEn": "writer",
},
map[string]interface{}{
"name": "Alice",
"nameHi": "ऐलिस",
"professionEn": "cricketer",
},
map[string]interface{}{
"name": "Juliet",
"nameHi": "जूलियट",
"nameZh": "朱丽叶",
"professionEn": "singer",
},
}}

gqlResponse := addPersonParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

queryPerson := &GraphQLParams{
Query: `
query {
queryPerson(
filter: {
or: [
{ name: { eq: "Bob" } }
{ nameHi: { eq: "ऐलिस" } }
{ nameZh: { eq: "朱丽叶" } }
]
}
order: { desc: nameHi }
) {
name
nameZh
nameHi
nameHiZh
nameZhHi
nameHi_Zh_Untag
name_Untag_AnyLang
professionEn
}
}`,
}
gqlResponse = queryPerson.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)
queryPersonExpected := `{"queryPerson":[{"name":"Juliet","nameZh":"朱丽叶","nameHi":"जूलियट",
"nameHiZh":"जूलियट","nameZhHi":"朱丽叶","nameHi_Zh_Untag":"जूलियट","name_Untag_AnyLang":"Juliet",
"professionEn":"singer"},{"name":"Alice","nameZh":null,"nameHi":"ऐलिस","nameHiZh":"ऐलिस",
"nameZhHi":"ऐलिस","nameHi_Zh_Untag":"ऐलिस","name_Untag_AnyLang":"Alice","professionEn":"cricketer"},
{"name":"Bob","nameZh":null,"nameHi":null,"nameHiZh":null,"nameZhHi":null,"nameHi_Zh_Untag":"Bob",
"name_Untag_AnyLang":"Bob","professionEn":"writer"}]}`

JSONEqGraphQL(t, queryPersonExpected, string(gqlResponse.Data))
// Cleanup
DeleteGqlType(t, "Person", map[string]interface{}{}, 3, nil)
}
12 changes: 12 additions & 0 deletions graphql/e2e/directives/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,18 @@ type Person1 {
friends: [Person1] @hasInverse(field: friends)
}

type Person {
id: ID!
name: String! @search(by: [hash])
nameHi: String @dgraph(pred:"Person.name@hi") @search(by: [hash])
nameZh: String @dgraph(pred:"Person.name@zh") @search(by: [hash])
nameHiZh: String @dgraph(pred:"Person.name@hi:zh")
nameZhHi: String @dgraph(pred:"Person.name@zh:hi")
nameHi_Zh_Untag: String @dgraph(pred:"Person.name@hi:zh:.")
name_Untag_AnyLang: String @dgraph(pred:"Person.name@.") @search(by: [hash])
professionEn: String @dgraph(pred:"Person.profession@en")
}

# union testing - start
enum AnimalCategory {
Fish
Expand Down
25 changes: 25 additions & 0 deletions graphql/e2e/directives/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@
"predicate": "Employer.worker",
"type": "uid"
},
{
"lang": true,
"predicate": "Person.profession",
"type": "string"
},
{
"index": true,
"lang": true,
"predicate": "Person.name",
"tokenizer": [
"hash"
],
"type": "string"
},
{
"predicate": "Book.chapters",
"type": "uid",
Expand Down Expand Up @@ -914,6 +928,17 @@
],
"name": "Home"
},
{
"fields": [
{
"name": "Person.name"
},
{
"name": "Person.profession"
}
],
"name": "Person"
},
{
"fields": [
{
Expand Down
11 changes: 9 additions & 2 deletions graphql/e2e/normal/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,15 @@ type Student implements People {
}

type Person @withSubscription{
id: ID!
name: String!
id: ID!
name: String! @search(by: [hash])
nameHi: String @dgraph(pred:"Person.name@hi") @search(by: [hash])
nameZh: String @dgraph(pred:"Person.name@zh") @search(by: [hash])
nameHiZh: String @dgraph(pred:"Person.name@hi:zh")
nameZhHi: String @dgraph(pred:"Person.name@zh:hi")
nameHi_Zh_Untag: String @dgraph(pred:"Person.name@hi:zh:.")
name_Untag_AnyLang: String @dgraph(pred:"Person.name@.") @search(by: [hash])
professionEn: String @dgraph(pred:"Person.profession@en")
}

"""
Expand Down
29 changes: 21 additions & 8 deletions graphql/e2e/normal/schema_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,17 @@
"upsert": true
},
{
"lang": true,
"predicate": "Person.profession",
"type": "string"
},
{
"index": true,
"lang": true,
"predicate": "Person.name",
"tokenizer": [
"hash"
],
"type": "string"
},
{
Expand Down Expand Up @@ -1102,14 +1112,6 @@
],
"name": "People"
},
{
"fields": [
{
"name": "Person.name"
}
],
"name": "Person"
},
{
"fields": [
{
Expand Down Expand Up @@ -1419,6 +1421,17 @@
}
],
"name": "Employer"
},
{
"fields": [
{
"name": "Person.name"
},
{
"name": "Person.profession"
}
],
"name": "Person"
}
]
}
23 changes: 22 additions & 1 deletion graphql/resolve/add_mutation_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4520,4 +4520,25 @@
"Friend1.id":"Main Friend",
"dgraph.type":["Friend1"],
"uid":"_:Friend1_1"
}
}

-
name: "Add mutation with language tag fields"
gqlmutation: |
mutation {
addPerson(input: { name: "Alice", nameHi: "ऐलिस",nameZh: "爱丽丝"}) {
person {
name
nameZh
nameHi
}
}
}
dgmutations:
- setjson: |
{ "Person.name":"Alice",
"Person.name@hi":"ऐलिस",
"Person.name@zh":"爱丽丝",
"dgraph.type": ["Person"],
"uid": "_:Person_1"
}
29 changes: 28 additions & 1 deletion graphql/resolve/query_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3382,4 +3382,31 @@
dgraph.uid : uid
}
}
}
}

-
name: "query language tag fields with filter and order"
gqlquery: |
query {
queryPerson(filter:{or:[{name:{eq:"Alice"}},{nameHi:{eq:"ऐलिस"}},{nameZh:{eq:"爱丽丝"}},{name_Untag_AnyLang:{eq:"Alice"}}]}, order: { asc: nameHi })
{
name
nameZh
nameHi
nameHiZh
nameHi_Zh_Untag
name_Untag_AnyLang
}
}
dgquery: |-
query {
queryPerson(func: type(Person), orderasc: Person.name@hi) @filter((eq(Person.name, "Alice") OR eq(Person.name@hi, "ऐलिस") OR eq(Person.name@zh, "爱丽丝") OR eq(Person.name@., "Alice"))) {
Person.name : Person.name
Person.nameZh : Person.name@zh
Person.nameHi : Person.name@hi
Person.nameHiZh : Person.name@hi:zh
Person.nameHi_Zh_Untag : Person.name@hi:zh:.
Person.name_Untag_AnyLang : Person.name@.
dgraph.uid : uid
}
}
5 changes: 5 additions & 0 deletions graphql/resolve/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ type ThingTwo implements Thing {
type Person {
id: ID!
name: String @search(by: [hash])
nameHi: String @dgraph(pred:"Person.name@hi") @search(by: [hash])
nameZh: String @dgraph(pred:"Person.name@zh") @search(by: [hash])
nameHiZh: String @dgraph(pred:"Person.name@hi:zh")
nameHi_Zh_Untag: String @dgraph(pred:"Person.name@hi:zh:.")
name_Untag_AnyLang: String @dgraph(pred:"Person.name@.") @search(by: [hash])
friends: [Person] @hasInverse(field: friends)
}

Expand Down
Loading