-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(GRAPHQL): Add language tag support in GraphQL #7663
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
Changes from 13 commits
739514d
bd8026a
010468b
6d9640c
480e471
68a36a1
b0ed0ea
4eb535d
058906e
6f746db
7746323
1e3b145
9691175
1195b19
f86b4b7
e073f83
af62712
5d943ad
1aa1078
6a205d8
1905eec
887550e
fa159b3
98c69f8
fc41d46
f794fc5
02273de
07f080d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -414,6 +414,7 @@ | |
"upsert": true | ||
}, | ||
{ | ||
"lang": true, | ||
"predicate": "Person.name", | ||
"type": "string" | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Add a line at end of file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3382,4 +3382,30 @@ | |
dgraph.uid : uid | ||
} | ||
} | ||
} | ||
} | ||
|
||
- | ||
name: "query with language tag fields" | ||
gqlquery: | | ||
query { | ||
queryPerson { | ||
name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Indentation |
||
nameZh | ||
nameHi | ||
nameHiZh | ||
nameHi_Zh_Untag | ||
name_Untag_AnyLang | ||
} | ||
} | ||
dgquery: |- | ||
query { | ||
queryPerson(func: type(Person)) { | ||
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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1200,7 +1200,7 @@ func addUnionMemberTypeEnum(schema *ast.Schema, defn *ast.Definition) { | |
// it should be present in the addTypeInput as it should not be generated automatically by dgraph | ||
// but determined by the value of field in the GraphQL service where the type is defined. | ||
func addInputType(schema *ast.Schema, defn *ast.Definition, providesTypeMap map[string]bool) { | ||
field := getFieldsWithoutIDType(schema, defn, providesTypeMap) | ||
field := getFieldsWithoutIDType(schema, defn, providesTypeMap, true) | ||
if hasExtends(defn) { | ||
idField := getIDField(defn, providesTypeMap) | ||
field = append(idField, field...) | ||
|
@@ -1223,7 +1223,8 @@ func addReferenceType(schema *ast.Schema, defn *ast.Definition, providesTypeMap | |
} | ||
flds = append(getIDField(defn, providesTypeMap), getXIDField(defn, providesTypeMap)...) | ||
} else { | ||
flds = append(getIDField(defn, providesTypeMap), getFieldsWithoutIDType(schema, defn, providesTypeMap)...) | ||
flds = append(getIDField(defn, providesTypeMap), | ||
getFieldsWithoutIDType(schema, defn, providesTypeMap, true)...) | ||
} | ||
|
||
if len(flds) == 1 && (hasID(defn) || hasXID(defn)) { | ||
|
@@ -1571,7 +1572,7 @@ func addFilterType(schema *ast.Schema, defn *ast.Definition, providesTypeMap map | |
} | ||
|
||
// Has filter makes sense only if there is atleast one non ID field in the defn | ||
if len(getFieldsWithoutIDType(schema, defn, providesTypeMap)) > 0 { | ||
if len(getFieldsWithoutIDType(schema, defn, providesTypeMap, false)) > 0 { | ||
filter.Fields = append(filter.Fields, | ||
&ast.FieldDefinition{Name: "has", Type: &ast.Type{Elem: &ast.Type{NamedType: defn.Name + "HasFilter"}}}, | ||
) | ||
|
@@ -2208,7 +2209,7 @@ func getNonIDFields(schema *ast.Schema, defn *ast.Definition, providesTypeMap ma | |
|
||
// Ignore Fields with @external directives also as they shouldn't be present | ||
// in the Patch Type also. If the field is an argument to `@provides` directive | ||
// then it should be presnt. | ||
// then it should be present. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
if externalAndNonKeyField(fld, defn, providesTypeMap) { | ||
continue | ||
} | ||
|
@@ -2217,7 +2218,12 @@ func getNonIDFields(schema *ast.Schema, defn *ast.Definition, providesTypeMap ma | |
if hasCustomOrLambda(fld) { | ||
continue | ||
} | ||
|
||
// We don't include fields in update patch, which corresponds to multiple language tags in dgraph | ||
// Example, nameHi_En: String @dgraph(pred:"Person.name@hi:en") | ||
// We don't add above field in update patch because it corresponds to multiple languages | ||
if !isMutableLanguageField(fld) { | ||
continue | ||
} | ||
// Remove edges which have a reverse predicate as they should only be updated through their | ||
// forward edge. | ||
fname := fieldName(fld, defn.Name) | ||
|
@@ -2246,7 +2252,8 @@ func getNonIDFields(schema *ast.Schema, defn *ast.Definition, providesTypeMap ma | |
return append(fldList, pd) | ||
} | ||
|
||
func getFieldsWithoutIDType(schema *ast.Schema, defn *ast.Definition, providesTypeMap map[string]bool) ast.FieldList { | ||
func getFieldsWithoutIDType(schema *ast.Schema, defn *ast.Definition, | ||
providesTypeMap map[string]bool, addInput bool) ast.FieldList { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Rename addInput to something more meaningful like isAddingInput or something. |
||
fldList := make([]*ast.FieldDefinition, 0) | ||
for _, fld := range defn.Fields { | ||
if isIDField(defn, fld) { | ||
|
@@ -2264,7 +2271,10 @@ func getFieldsWithoutIDType(schema *ast.Schema, defn *ast.Definition, providesTy | |
if hasCustomOrLambda(fld) { | ||
continue | ||
} | ||
|
||
// see also comment in getNonIDFields | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: see the comment in getNonIDFields as well. |
||
if !isMutableLanguageField(fld) && addInput { | ||
continue | ||
} | ||
// Remove edges which have a reverse predicate as they should only be updated through their | ||
// forward edge. | ||
fname := fieldName(fld, defn.Name) | ||
|
@@ -2287,6 +2297,23 @@ func getFieldsWithoutIDType(schema *ast.Schema, defn *ast.Definition, providesTy | |
return append(fldList, pd) | ||
} | ||
|
||
// This function check if given gql field have multiple language tags | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: have --> has |
||
func isMutableLanguageField(fld *ast.FieldDefinition) bool { | ||
dgDirective := fld.Directives.ForName(dgraphDirective) | ||
if dgDirective != nil { | ||
pred := dgDirective.Arguments.ForName("pred") | ||
if pred != nil { | ||
if strings.Contains(pred.Value.Raw, "@") { | ||
langs := strings.Split(pred.Value.Raw, "@")[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a chance of this panicking if there is a dgraph predicate with name like "somePredicate@" . Can you confirm that @ cannot be at the end of Dgraph predicate or handle this case as well. |
||
if strings.Contains(langs, ":") || langs == "." { | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func getIDField(defn *ast.Definition, providesTypeMap map[string]bool) ast.FieldList { | ||
fldList := make([]*ast.FieldDefinition, 0) | ||
for _, fld := range defn.Fields { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also update in this e2e test ?