Skip to content

Commit

Permalink
graphql: handle unknown property in MapFilter
Browse files Browse the repository at this point in the history
Handling unknown GraphQL filter property in MapFilter func.
It is required to fallback into selector filtering when
the specified property is not available in the search response.
E.g. ?filter=(eq,location,US) --> should filter using the selector.
  • Loading branch information
danielerez committed Jan 31, 2024
1 parent 505414e commit 7ed4744
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions internal/graphql/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ License.
package graphql

import (
"errors"
"fmt"

"github.com/openshift-kni/oran-o2ims/internal/model"
Expand Down Expand Up @@ -99,6 +100,9 @@ func (t FilterTerm) MapFilter(mapPropertyFunc func(string) string) (searchFilter

// Convert to GraphQL property
searchProperty := mapPropertyFunc(t.Path[0])
if searchProperty == "" {
return nil, errors.New("unknown GraphQL property")
}

// Build search filter
searchFilter = &model.SearchFilter{
Expand Down
31 changes: 28 additions & 3 deletions internal/graphql/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ License.
package graphql

import (
"errors"

"github.com/openshift-kni/oran-o2ims/internal/model"
"github.com/openshift-kni/oran-o2ims/internal/search"

Expand All @@ -27,10 +29,14 @@ import (
var _ = Describe("GraphQL filters", func() {
DescribeTable(
"Map a filter term to a SearchFilter",
func(term search.Term, expected *model.SearchFilter, mapPropertyFunc func(string) string) {
func(term search.Term, expectedFilter *model.SearchFilter, expectedErr error, mapPropertyFunc func(string) string) {
actual, err := FilterTerm(term).MapFilter(mapPropertyFunc)
Expect(err).ToNot(HaveOccurred())
Expect(actual).To(Equal(expected))
Expect(actual).To(Equal(expectedFilter))
if expectedErr != nil {
Expect(err).To(HaveOccurred())
} else {
Expect(err).ToNot(HaveOccurred())
}
},
Entry(
"Filter term for Cluster",
Expand All @@ -47,6 +53,7 @@ var _ = Describe("GraphQL filters", func() {
Property: "cluster",
Values: []*string{ptr.To("=spoke0")},
},
nil,
func(s string) string {
return PropertyCluster(s).MapProperty()
},
Expand All @@ -66,6 +73,24 @@ var _ = Describe("GraphQL filters", func() {
Property: "cluster",
Values: []*string{ptr.To("=spoke0")},
},
nil,
func(s string) string {
return PropertyNode(s).MapProperty()
},
),
Entry(
"Fail when an unknown property is specified",
search.Term{
Operator: search.Eq,
Path: []string{
"location",
},
Values: []any{
"EU",
},
},
nil,
errors.New("unknown GraphQL property"),
func(s string) string {
return PropertyNode(s).MapProperty()
},
Expand Down

0 comments on commit 7ed4744

Please sign in to comment.