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

feat(GraphQL): add support for IN filter #6662

Merged
merged 9 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
17 changes: 13 additions & 4 deletions graphql/dgraph/graphquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ func writeRoot(b *strings.Builder, q *gql.GraphQuery) {
x.Check2(b.WriteRune(')'))
}

func writeFilterArguments(b *strings.Builder, args []gql.Arg) {
for i, arg := range args {
if i != 0 {
x.Check2(b.WriteString(", "))
}
x.Check2(b.WriteString(fmt.Sprintf("%s", arg.Value)))
}
}

func writeFilterFunction(b *strings.Builder, f *gql.Function) {
if f == nil {
return
Expand All @@ -151,10 +160,10 @@ func writeFilterFunction(b *strings.Builder, f *gql.Function) {
switch {
case f.Name == "uid":
writeUIDFunc(b, f.UID, f.Args)
case len(f.Args) == 1:
x.Check2(b.WriteString(fmt.Sprintf("%s(%s)", f.Name, f.Args[0].Value)))
case len(f.Args) == 2:
x.Check2(b.WriteString(fmt.Sprintf("%s(%s, %s)", f.Name, f.Args[0].Value, f.Args[1].Value)))
default:
x.Check2(b.WriteString(fmt.Sprintf("%s(", f.Name)))
writeFilterArguments(b, f.Args)
x.Check2(b.WriteRune(')'))
}
}

Expand Down
1 change: 1 addition & 0 deletions graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ func RunAll(t *testing.T) {
t.Run("multiple search indexes", multipleSearchIndexes)
t.Run("multiple search indexes wrong field", multipleSearchIndexesWrongField)
t.Run("hash search", hashSearch)
t.Run("in filter", InFilter)
t.Run("deep filter", deepFilter)
t.Run("deep has filter", deepHasFilter)
t.Run("many queries", manyQueries)
Expand Down
60 changes: 60 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,66 @@ func allPosts(t *testing.T) []*post {
return result.QueryPost
}

func InFilter(t *testing.T) {
addStateParams := &GraphQLParams{
Query: `mutation addState($name1: String!, $code1: String!, $name2: String!, $code2: String! ) {
addState(input: [{name: $name1, xcode: $code1},{name: $name2, xcode: $code2}]) {
state {
xcode
name
}
}
}`,

Variables: map[string]interface{}{
"name1": "A State",
"code1": "abc",
"name2": "B State",
"code2": "def",
},
}

gqlResponse := addStateParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

getStateParams := &GraphQLParams{
Query: `query{
queryState(filter: {xcode: {in: ["abc", "def"]}}){
xcode
name
}
}`,
}

gqlResponse = getStateParams.ExecuteAsPost(t, GraphqlURL)
RequireNoGQLErrors(t, gqlResponse)

var result struct {
QueryState []*state
}
err := json.Unmarshal([]byte(gqlResponse.Data), &result)
require.NoError(t, err)
require.Equal(t, 2, len(result.QueryState))

state1 := &state{
Name: "A State",
Code: "abc",
}
state2 := &state{
Name: "B State",
Code: "def",
}

expected := []*state{state1, state2}

if diff := cmp.Diff(expected, result.QueryState); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}

deleteFilter := map[string]interface{}{"xcode": map[string]interface{}{"in": []string{"abc", "def"}}}
deleteGqlType(t, "State", deleteFilter, 2, nil)
}

func deepFilter(t *testing.T) {
getAuthorParams := &GraphQLParams{
Query: `query {
Expand Down
2 changes: 2 additions & 0 deletions graphql/e2e/schema/generatedSchema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -143,6 +144,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
23 changes: 19 additions & 4 deletions graphql/resolve/query_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,14 +1056,29 @@ func buildFilter(typ schema.Type, filter map[string]interface{}) *gql.FilterTree
// title: { anyofterms: "GraphQL" } -> anyofterms(Post.title, "GraphQL")
// OR
// numLikes: { le: 10 } -> le(Post.numLikes, 10)

fn, val := first(dgFunc)
args := []gql.Arg{
{Value: typ.DgraphPredicate(field)},
}
switch fn {
// in takes List of Scalars as argument, for eg:
// code : { in: {"abc", "def", "ghi"} } -> eq(State.code,"abc","def","ghi")
case "in":
vals := val.([]interface{})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unchecked type assertions can cause panics. Check for success with the x, ok := y.(type) style.

View Rule

fn = "eq"

for _, v := range vals {
args = append(args, gql.Arg{Value: maybeQuoteArg(fn, v)})
}

default:
args = append(args, gql.Arg{Value: maybeQuoteArg(fn, val)})
}
ands = append(ands, &gql.FilterTree{
Func: &gql.Function{
Name: fn,
Args: []gql.Arg{
{Value: typ.DgraphPredicate(field)},
{Value: maybeQuoteArg(fn, val)},
},
Args: args,
},
})
case []interface{}:
Expand Down
18 changes: 18 additions & 0 deletions graphql/resolve/query_test.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
-
name: "in filter"
gqlquery: |
query {
queryState(filter: {code: {in: ["abc", "def", "ghi"]}}) {
code
name
}
}
dgquery: |-
query {
queryState(func: type(State)) @filter(eq(State.code, "abc", "def", "ghi")) {
code : State.code
name : State.name
dgraph.uid : uid
}
}

-
name: "ID query"
gqlquery: |
Expand Down
2 changes: 2 additions & 0 deletions graphql/schema/gqlschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -194,6 +195,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}
`
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -154,6 +155,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -159,6 +160,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -147,6 +148,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -164,6 +165,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -148,6 +149,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -147,6 +148,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -143,6 +144,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
2 changes: 2 additions & 0 deletions graphql/schema/testdata/schemagen/output/deprecated.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -143,6 +144,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -157,6 +158,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -157,6 +158,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -156,6 +157,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down Expand Up @@ -332,6 +334,7 @@ input PostRef {

input StringHashFilter_StringRegExpFilter {
eq: String
in: [String]
regexp: String
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ input StringFullTextFilter {

input StringExactFilter {
eq: String
in: [String]
le: String
lt: String
ge: String
Expand All @@ -150,6 +151,7 @@ input StringExactFilter {

input StringHashFilter {
eq: String
in: [String]
}

#######################
Expand Down
Loading