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 (#7550)

* fix(GRAPHQL): Added support for exact index on field having @id directive. (#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 authored Mar 11, 2021
1 parent 856fad4 commit 4b8a1fd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 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 @@ -1472,14 +1472,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 @@ -2676,6 +2676,12 @@ invalid_schemas:


valid_schemas:
- 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
15 changes: 8 additions & 7 deletions graphql/schema/schemagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func parseSecrets(sch string) (map[string]string, *authorization.AuthMeta, error
if err := scanner.Err(); err != nil {
return nil, nil, errors.Wrapf(err, "while trying to parse secrets from schema file")
}

if authSecret == "" {
return m, nil, nil
}
Expand Down Expand Up @@ -485,12 +484,6 @@ func genDgSchema(gqlSch *ast.Schema, definitions []string) string {
var indexes []string
upsertStr := ""
search := f.Directives.ForName(searchDirective)
id := f.Directives.ForName(idDirective)
if id != nil {
upsertStr = "@upsert "
indexes = append(indexes, "hash")
}

if search != nil {
arg := search.Arguments.ForName(searchArgs)
if arg != nil {
Expand All @@ -501,6 +494,14 @@ func genDgSchema(gqlSch *ast.Schema, definitions []string) string {
}
}

id := f.Directives.ForName(idDirective)
if id != nil {
upsertStr = "@upsert "
if !x.HasString(indexes, "exact") {
indexes = append(indexes, "hash")
}
}

if parentInt == nil {
dgPreds[fname] = getUpdatedPred(fname, typStr, upsertStr, indexes)
}
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 @@ -425,7 +425,7 @@ input AuthorRef {
}

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

0 comments on commit 4b8a1fd

Please sign in to comment.