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

Merged
merged 5 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 11 additions & 8 deletions graphql/schema/gqlschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1648,20 +1648,23 @@ 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 !hasIndex(indexes, "hash") && !hasIndex(indexes, "exact") {
indexes = append(indexes, "hash")
}
}
return indexes
}

func hasIndex(indexes []string, index string) bool {
for i := range indexes {
if indexes[i] == index {
return true
}
}
return false
}

func getDefaultSearchIndex(fldName string) string {
if search, ok := defaultSearches[fldName]; ok {
return search
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 !hasIndex(indexes, "exact") {
indexes = append(indexes, "hash")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This will add exact index on content field, overwriting the default "hash" index for field of type "String! @id".
type Post {
content: String! @id @search(by:[exact])
}
Loading