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

fix(GRAPHQL): Added support for exact index on field having @id directive. (#7534) #7551

Merged
merged 1 commit into from
Mar 11, 2021
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
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