Skip to content

Commit

Permalink
fix(GRAPHQL): Added support for exact index on field having @id direc…
Browse files Browse the repository at this point in the history
…tive. (#7534)

Currently we add hash index on a field of type String! @id by default. And as index exact and hash are not compatible , user can't add exact index on such field and can't take advantage of comparator functions like lt,le,gt,ge.

To allow this we now changing that behavior, i.e. for a field of type String! @id @search(by:[exact]) , we don't generate default hash index and only generate exact index.

(cherry picked from commit 195f247)
  • Loading branch information
JatinDev543 committed Mar 11, 2021
1 parent ecb27f0 commit 6f78651
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
19 changes: 19 additions & 0 deletions graphql/schema/dgraph_schemagen_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,25 @@ schemas:
}
B.correct: bool @index(bool) .
- name: "Field with @id directive and a exact arg in search directive generates correct schema."
input: |
interface A {
id: String! @id @search(by: [exact])
}
type B implements A {
correct: Boolean @search
}
output: |
type A {
A.id
}
A.id: string @index(exact) @upsert .
type B {
A.id
B.correct
}
B.correct: bool @index(bool) .
-
name: "Field with reverse predicate in dgraph directive adds @reverse to predicate."
input: |
Expand Down
10 changes: 2 additions & 8 deletions graphql/schema/gqlschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1648,14 +1648,8 @@ func addHashIfRequired(fld *ast.FieldDefinition, indexes []string) []string {
id := fld.Directives.ForName(idDirective)
if id != nil {
// If @id directive is applied along with @search, we check if the search has hash as an
// arg. If it doesn't, then we add it.
containsHash := false
for _, index := range indexes {
if index == "hash" {
containsHash = true
}
}
if !containsHash {
// arg. If it doesn't and there is no exact arg, then we add hash in it.
if !x.HasString(indexes, "hash") && !x.HasString(indexes, "exact") {
indexes = append(indexes, "hash")
}
}
Expand Down
6 changes: 6 additions & 0 deletions graphql/schema/gqlschema_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2838,6 +2838,12 @@ valid_schemas:
f2: String! @id
}
- name: "field with @id directive can have exact index"
input: |
type X {
f1: String! @id @search(by:[exact])
}
- name: "Type implements from two interfaces where both have ID"
input: |
interface X {
Expand Down
24 changes: 13 additions & 11 deletions graphql/schema/schemagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,16 @@ func genDgSchema(gqlSch *ast.Schema, definitions []string) string {
var indexes []string
upsertStr := ""
search := f.Directives.ForName(searchDirective)
if search != nil {
arg := search.Arguments.ForName(searchArgs)
if arg != nil {
indexes = append(indexes, getAllSearchIndexes(arg.Value)...)
} else {
indexes = append(indexes, supportedSearches[defaultSearches[f.Type.
Name()]].dgIndex)
}
}

id := f.Directives.ForName(idDirective)
if id != nil || f.Type.Name() == "ID" {
upsertStr = "@upsert "
Expand All @@ -646,17 +656,9 @@ func genDgSchema(gqlSch *ast.Schema, definitions []string) string {
case "Float":
indexes = append(indexes, "float")
case "String", "ID":
indexes = append(indexes, "hash")
}
}

if search != nil {
arg := search.Arguments.ForName(searchArgs)
if arg != nil {
indexes = append(indexes, getAllSearchIndexes(arg.Value)...)
} else {
indexes = append(indexes, supportedSearches[defaultSearches[f.Type.
Name()]].dgIndex)
if !x.HasString(indexes, "exact") {
indexes = append(indexes, "hash")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ type Author {
}

type Genre {
name: String! @id
# This will add exact index on name field, overwriting the default "hash" index for field of type "String! @id".
name: String! @id @search(by: [exact])
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Author {
}

type Genre {
name: String! @id
name: String! @id @search(by: [exact])
}

#######################
Expand Down Expand Up @@ -430,7 +430,7 @@ input AuthorRef {
}

input GenreFilter {
name: StringHashFilter
name: StringExactFilter
has: [GenreHasFilter]
and: [GenreFilter]
or: [GenreFilter]
Expand Down

0 comments on commit 6f78651

Please sign in to comment.