From 7087674a76fad76636afa0d28b9e9711c68aff63 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Sat, 29 Apr 2023 21:00:40 +0300 Subject: [PATCH] entgql: fix field arguments collection (#495) --- entgql/internal/todo/ent/gql_collection.go | 55 +++++-------- entgql/internal/todo/todo_test.go | 82 ++++++++++++++++++- entgql/internal/todofed/ent/gql_collection.go | 37 +++------ .../internal/todogotype/ent/gql_collection.go | 51 ++++-------- .../internal/todopulid/ent/gql_collection.go | 49 ++++------- .../internal/todouuid/ent/gql_collection.go | 49 ++++------- entgql/template/collection.tmpl | 37 +++------ 7 files changed, 169 insertions(+), 191 deletions(-) diff --git a/entgql/internal/todo/ent/gql_collection.go b/entgql/internal/todo/ent/gql_collection.go index 5a570a133..31110099b 100644 --- a/entgql/internal/todo/ent/gql_collection.go +++ b/entgql/internal/todo/ent/gql_collection.go @@ -88,7 +88,7 @@ type billproductPaginateArgs struct { opts []BillProductPaginateOption } -func newBillProductPaginateArgs(rv map[string]interface{}) *billproductPaginateArgs { +func newBillProductPaginateArgs(rv map[string]any) *billproductPaginateArgs { args := &billproductPaginateArgs{} if rv == nil { return args @@ -352,7 +352,7 @@ type categoryPaginateArgs struct { opts []CategoryPaginateOption } -func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { +func newCategoryPaginateArgs(rv map[string]any) *categoryPaginateArgs { args := &categoryPaginateArgs{} if rv == nil { return args @@ -373,10 +373,10 @@ func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { switch v := v.(type) { case []*CategoryOrder: args.opts = append(args.opts, WithCategoryOrder(v)) - case []interface{}: + case []any: var orders []*CategoryOrder for i := range v { - mv, ok := v[i].(map[string]interface{}) + mv, ok := v[i].(map[string]any) if !ok { continue } @@ -485,7 +485,7 @@ type friendshipPaginateArgs struct { opts []FriendshipPaginateOption } -func newFriendshipPaginateArgs(rv map[string]interface{}) *friendshipPaginateArgs { +func newFriendshipPaginateArgs(rv map[string]any) *friendshipPaginateArgs { args := &friendshipPaginateArgs{} if rv == nil { return args @@ -640,7 +640,7 @@ type groupPaginateArgs struct { opts []GroupPaginateOption } -func newGroupPaginateArgs(rv map[string]interface{}) *groupPaginateArgs { +func newGroupPaginateArgs(rv map[string]any) *groupPaginateArgs { args := &groupPaginateArgs{} if rv == nil { return args @@ -738,7 +738,7 @@ type onetomanyPaginateArgs struct { opts []OneToManyPaginateOption } -func newOneToManyPaginateArgs(rv map[string]interface{}) *onetomanyPaginateArgs { +func newOneToManyPaginateArgs(rv map[string]any) *onetomanyPaginateArgs { args := &onetomanyPaginateArgs{} if rv == nil { return args @@ -757,7 +757,7 @@ func newOneToManyPaginateArgs(rv map[string]interface{}) *onetomanyPaginateArgs } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &OneToManyOrder{Field: &OneToManyOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -894,7 +894,7 @@ type projectPaginateArgs struct { opts []ProjectPaginateOption } -func newProjectPaginateArgs(rv map[string]interface{}) *projectPaginateArgs { +func newProjectPaginateArgs(rv map[string]any) *projectPaginateArgs { args := &projectPaginateArgs{} if rv == nil { return args @@ -1104,7 +1104,7 @@ type todoPaginateArgs struct { opts []TodoPaginateOption } -func newTodoPaginateArgs(rv map[string]interface{}) *todoPaginateArgs { +func newTodoPaginateArgs(rv map[string]any) *todoPaginateArgs { args := &todoPaginateArgs{} if rv == nil { return args @@ -1123,7 +1123,7 @@ func newTodoPaginateArgs(rv map[string]interface{}) *todoPaginateArgs { } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &TodoOrder{Field: &TodoOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -1463,7 +1463,7 @@ type userPaginateArgs struct { opts []UserPaginateOption } -func newUserPaginateArgs(rv map[string]interface{}) *userPaginateArgs { +func newUserPaginateArgs(rv map[string]any) *userPaginateArgs { args := &userPaginateArgs{} if rv == nil { return args @@ -1482,7 +1482,7 @@ func newUserPaginateArgs(rv map[string]interface{}) *userPaginateArgs { } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &UserOrder{Field: &UserOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -1519,35 +1519,18 @@ const ( whereField = "where" ) -func fieldArgs(ctx context.Context, whereInput interface{}, path ...string) map[string]interface{} { - fc := graphql.GetFieldContext(ctx) - if fc == nil { +func fieldArgs(ctx context.Context, whereInput any, path ...string) map[string]any { + field := collectedField(ctx, path...) + if field == nil || field.Arguments == nil { return nil } oc := graphql.GetOperationContext(ctx) - for _, name := range path { - var field *graphql.CollectedField - for _, f := range graphql.CollectFields(oc, fc.Field.Selections, nil) { - if f.Alias == name { - field = &f - break - } - } - if field == nil { - return nil - } - cf, err := fc.Child(ctx, *field) - if err != nil { - args := field.ArgumentMap(oc.Variables) - return unmarshalArgs(ctx, whereInput, args) - } - fc = cf - } - return fc.Args + args := field.ArgumentMap(oc.Variables) + return unmarshalArgs(ctx, whereInput, args) } // unmarshalArgs allows extracting the field arguments from their raw representation. -func unmarshalArgs(ctx context.Context, whereInput interface{}, args map[string]interface{}) map[string]interface{} { +func unmarshalArgs(ctx context.Context, whereInput any, args map[string]any) map[string]any { for _, k := range []string{firstField, lastField} { v, ok := args[k] if !ok { diff --git a/entgql/internal/todo/todo_test.go b/entgql/internal/todo/todo_test.go index 8526385c2..e43d2437d 100644 --- a/entgql/internal/todo/todo_test.go +++ b/entgql/internal/todo/todo_test.go @@ -1313,7 +1313,87 @@ func TestNestedConnection(t *testing.T) { bulkU[i] = ec.User.Create().SetName(fmt.Sprintf("user-%d", i)).AddGroups(groups[:len(groups)-i]...) } users := ec.User.CreateBulk(bulkU...).SaveX(ctx) - users[0].Update().AddFriends(users[1:]...).SaveX(ctx) + users[0].Update().AddFriends(users[1:]...).SaveX(ctx) // user 0 is friends with all + users[1].Update().AddFriends(users[2:]...).SaveX(ctx) // user 1 is friends with all + + t.Run("After Cursor", func(t *testing.T) { + var ( + query = `query ($id: ID!, $after: Cursor) { + user: node(id: $id) { + ... on User { + id + name + friends(after: $after) { + totalCount + edges { + cursor + node { + id + name + friends { + totalCount + edges { + node { + id + name + } + } + } + } + } + } + } + } + }` + rsp struct { + User struct { + ID string + Name string + Friends struct { + TotalCount int + Edges []struct { + Cursor string + Node struct { + ID string + Name string + Friends struct { + TotalCount int + Edges []struct { + Node struct { + ID string + Name string + } + } + } + } + } + } + } + } + after any + ) + err = gqlc.Post(query, &rsp, client.Var("id", users[0].ID), client.Var("after", after)) + require.NoError(t, err) + require.Equal(t, 9, rsp.User.Friends.TotalCount) + require.Len(t, rsp.User.Friends.Edges, 9, "All users are friends with user 0") + // First friend of user 0 is user 1. + require.Equal(t, strconv.Itoa(users[1].ID), rsp.User.Friends.Edges[0].Node.ID) + require.Len(t, rsp.User.Friends.Edges[0].Node.Friends.Edges, 9, "All users are friends with user 1") + // All other users have 2 friends (user 0 and user 1). + for _, u := range rsp.User.Friends.Edges[1:] { + require.Len(t, u.Node.Friends.Edges, 2) + } + + // Paginate over the friends of user 0. + n := len(rsp.User.Friends.Edges) + for i := 0; i < n; i++ { + err = gqlc.Post(query, &rsp, client.Var("id", users[0].ID), client.Var("after", after)) + require.NoError(t, err) + require.Equal(t, 9, rsp.User.Friends.TotalCount) + require.Lenf(t, rsp.User.Friends.Edges, n-i, "There are %d friends after %v", n-i, after) + after = rsp.User.Friends.Edges[0].Cursor + } + }) t.Run("TotalCount", func(t *testing.T) { var ( diff --git a/entgql/internal/todofed/ent/gql_collection.go b/entgql/internal/todofed/ent/gql_collection.go index 2cc10fff7..d858d7f0e 100644 --- a/entgql/internal/todofed/ent/gql_collection.go +++ b/entgql/internal/todofed/ent/gql_collection.go @@ -107,7 +107,7 @@ type categoryPaginateArgs struct { opts []CategoryPaginateOption } -func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { +func newCategoryPaginateArgs(rv map[string]any) *categoryPaginateArgs { args := &categoryPaginateArgs{} if rv == nil { return args @@ -126,7 +126,7 @@ func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &CategoryOrder{Field: &CategoryOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -245,7 +245,7 @@ type todoPaginateArgs struct { opts []TodoPaginateOption } -func newTodoPaginateArgs(rv map[string]interface{}) *todoPaginateArgs { +func newTodoPaginateArgs(rv map[string]any) *todoPaginateArgs { args := &todoPaginateArgs{} if rv == nil { return args @@ -264,7 +264,7 @@ func newTodoPaginateArgs(rv map[string]interface{}) *todoPaginateArgs { } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &TodoOrder{Field: &TodoOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -298,35 +298,18 @@ const ( whereField = "where" ) -func fieldArgs(ctx context.Context, whereInput interface{}, path ...string) map[string]interface{} { - fc := graphql.GetFieldContext(ctx) - if fc == nil { +func fieldArgs(ctx context.Context, whereInput any, path ...string) map[string]any { + field := collectedField(ctx, path...) + if field == nil || field.Arguments == nil { return nil } oc := graphql.GetOperationContext(ctx) - for _, name := range path { - var field *graphql.CollectedField - for _, f := range graphql.CollectFields(oc, fc.Field.Selections, nil) { - if f.Alias == name { - field = &f - break - } - } - if field == nil { - return nil - } - cf, err := fc.Child(ctx, *field) - if err != nil { - args := field.ArgumentMap(oc.Variables) - return unmarshalArgs(ctx, whereInput, args) - } - fc = cf - } - return fc.Args + args := field.ArgumentMap(oc.Variables) + return unmarshalArgs(ctx, whereInput, args) } // unmarshalArgs allows extracting the field arguments from their raw representation. -func unmarshalArgs(ctx context.Context, whereInput interface{}, args map[string]interface{}) map[string]interface{} { +func unmarshalArgs(ctx context.Context, whereInput any, args map[string]any) map[string]any { for _, k := range []string{firstField, lastField} { v, ok := args[k] if !ok { diff --git a/entgql/internal/todogotype/ent/gql_collection.go b/entgql/internal/todogotype/ent/gql_collection.go index 6137e0e09..573a7fc9e 100644 --- a/entgql/internal/todogotype/ent/gql_collection.go +++ b/entgql/internal/todogotype/ent/gql_collection.go @@ -88,7 +88,7 @@ type billproductPaginateArgs struct { opts []BillProductPaginateOption } -func newBillProductPaginateArgs(rv map[string]interface{}) *billproductPaginateArgs { +func newBillProductPaginateArgs(rv map[string]any) *billproductPaginateArgs { args := &billproductPaginateArgs{} if rv == nil { return args @@ -352,7 +352,7 @@ type categoryPaginateArgs struct { opts []CategoryPaginateOption } -func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { +func newCategoryPaginateArgs(rv map[string]any) *categoryPaginateArgs { args := &categoryPaginateArgs{} if rv == nil { return args @@ -373,10 +373,10 @@ func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { switch v := v.(type) { case []*CategoryOrder: args.opts = append(args.opts, WithCategoryOrder(v)) - case []interface{}: + case []any: var orders []*CategoryOrder for i := range v { - mv, ok := v[i].(map[string]interface{}) + mv, ok := v[i].(map[string]any) if !ok { continue } @@ -485,7 +485,7 @@ type friendshipPaginateArgs struct { opts []FriendshipPaginateOption } -func newFriendshipPaginateArgs(rv map[string]interface{}) *friendshipPaginateArgs { +func newFriendshipPaginateArgs(rv map[string]any) *friendshipPaginateArgs { args := &friendshipPaginateArgs{} if rv == nil { return args @@ -640,7 +640,7 @@ type groupPaginateArgs struct { opts []GroupPaginateOption } -func newGroupPaginateArgs(rv map[string]interface{}) *groupPaginateArgs { +func newGroupPaginateArgs(rv map[string]any) *groupPaginateArgs { args := &groupPaginateArgs{} if rv == nil { return args @@ -707,7 +707,7 @@ type petPaginateArgs struct { opts []PetPaginateOption } -func newPetPaginateArgs(rv map[string]interface{}) *petPaginateArgs { +func newPetPaginateArgs(rv map[string]any) *petPaginateArgs { args := &petPaginateArgs{} if rv == nil { return args @@ -917,7 +917,7 @@ type todoPaginateArgs struct { opts []TodoPaginateOption } -func newTodoPaginateArgs(rv map[string]interface{}) *todoPaginateArgs { +func newTodoPaginateArgs(rv map[string]any) *todoPaginateArgs { args := &todoPaginateArgs{} if rv == nil { return args @@ -936,7 +936,7 @@ func newTodoPaginateArgs(rv map[string]interface{}) *todoPaginateArgs { } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &TodoOrder{Field: &TodoOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -1118,7 +1118,7 @@ type userPaginateArgs struct { opts []UserPaginateOption } -func newUserPaginateArgs(rv map[string]interface{}) *userPaginateArgs { +func newUserPaginateArgs(rv map[string]any) *userPaginateArgs { args := &userPaginateArgs{} if rv == nil { return args @@ -1137,7 +1137,7 @@ func newUserPaginateArgs(rv map[string]interface{}) *userPaginateArgs { } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &UserOrder{Field: &UserOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -1174,35 +1174,18 @@ const ( whereField = "where" ) -func fieldArgs(ctx context.Context, whereInput interface{}, path ...string) map[string]interface{} { - fc := graphql.GetFieldContext(ctx) - if fc == nil { +func fieldArgs(ctx context.Context, whereInput any, path ...string) map[string]any { + field := collectedField(ctx, path...) + if field == nil || field.Arguments == nil { return nil } oc := graphql.GetOperationContext(ctx) - for _, name := range path { - var field *graphql.CollectedField - for _, f := range graphql.CollectFields(oc, fc.Field.Selections, nil) { - if f.Alias == name { - field = &f - break - } - } - if field == nil { - return nil - } - cf, err := fc.Child(ctx, *field) - if err != nil { - args := field.ArgumentMap(oc.Variables) - return unmarshalArgs(ctx, whereInput, args) - } - fc = cf - } - return fc.Args + args := field.ArgumentMap(oc.Variables) + return unmarshalArgs(ctx, whereInput, args) } // unmarshalArgs allows extracting the field arguments from their raw representation. -func unmarshalArgs(ctx context.Context, whereInput interface{}, args map[string]interface{}) map[string]interface{} { +func unmarshalArgs(ctx context.Context, whereInput any, args map[string]any) map[string]any { for _, k := range []string{firstField, lastField} { v, ok := args[k] if !ok { diff --git a/entgql/internal/todopulid/ent/gql_collection.go b/entgql/internal/todopulid/ent/gql_collection.go index 314c36f5d..d408b3ed7 100644 --- a/entgql/internal/todopulid/ent/gql_collection.go +++ b/entgql/internal/todopulid/ent/gql_collection.go @@ -87,7 +87,7 @@ type billproductPaginateArgs struct { opts []BillProductPaginateOption } -func newBillProductPaginateArgs(rv map[string]interface{}) *billproductPaginateArgs { +func newBillProductPaginateArgs(rv map[string]any) *billproductPaginateArgs { args := &billproductPaginateArgs{} if rv == nil { return args @@ -351,7 +351,7 @@ type categoryPaginateArgs struct { opts []CategoryPaginateOption } -func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { +func newCategoryPaginateArgs(rv map[string]any) *categoryPaginateArgs { args := &categoryPaginateArgs{} if rv == nil { return args @@ -372,10 +372,10 @@ func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { switch v := v.(type) { case []*CategoryOrder: args.opts = append(args.opts, WithCategoryOrder(v)) - case []interface{}: + case []any: var orders []*CategoryOrder for i := range v { - mv, ok := v[i].(map[string]interface{}) + mv, ok := v[i].(map[string]any) if !ok { continue } @@ -484,7 +484,7 @@ type friendshipPaginateArgs struct { opts []FriendshipPaginateOption } -func newFriendshipPaginateArgs(rv map[string]interface{}) *friendshipPaginateArgs { +func newFriendshipPaginateArgs(rv map[string]any) *friendshipPaginateArgs { args := &friendshipPaginateArgs{} if rv == nil { return args @@ -639,7 +639,7 @@ type groupPaginateArgs struct { opts []GroupPaginateOption } -func newGroupPaginateArgs(rv map[string]interface{}) *groupPaginateArgs { +func newGroupPaginateArgs(rv map[string]any) *groupPaginateArgs { args := &groupPaginateArgs{} if rv == nil { return args @@ -849,7 +849,7 @@ type todoPaginateArgs struct { opts []TodoPaginateOption } -func newTodoPaginateArgs(rv map[string]interface{}) *todoPaginateArgs { +func newTodoPaginateArgs(rv map[string]any) *todoPaginateArgs { args := &todoPaginateArgs{} if rv == nil { return args @@ -868,7 +868,7 @@ func newTodoPaginateArgs(rv map[string]interface{}) *todoPaginateArgs { } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &TodoOrder{Field: &TodoOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -1060,7 +1060,7 @@ type userPaginateArgs struct { opts []UserPaginateOption } -func newUserPaginateArgs(rv map[string]interface{}) *userPaginateArgs { +func newUserPaginateArgs(rv map[string]any) *userPaginateArgs { args := &userPaginateArgs{} if rv == nil { return args @@ -1079,7 +1079,7 @@ func newUserPaginateArgs(rv map[string]interface{}) *userPaginateArgs { } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &UserOrder{Field: &UserOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -1116,35 +1116,18 @@ const ( whereField = "where" ) -func fieldArgs(ctx context.Context, whereInput interface{}, path ...string) map[string]interface{} { - fc := graphql.GetFieldContext(ctx) - if fc == nil { +func fieldArgs(ctx context.Context, whereInput any, path ...string) map[string]any { + field := collectedField(ctx, path...) + if field == nil || field.Arguments == nil { return nil } oc := graphql.GetOperationContext(ctx) - for _, name := range path { - var field *graphql.CollectedField - for _, f := range graphql.CollectFields(oc, fc.Field.Selections, nil) { - if f.Alias == name { - field = &f - break - } - } - if field == nil { - return nil - } - cf, err := fc.Child(ctx, *field) - if err != nil { - args := field.ArgumentMap(oc.Variables) - return unmarshalArgs(ctx, whereInput, args) - } - fc = cf - } - return fc.Args + args := field.ArgumentMap(oc.Variables) + return unmarshalArgs(ctx, whereInput, args) } // unmarshalArgs allows extracting the field arguments from their raw representation. -func unmarshalArgs(ctx context.Context, whereInput interface{}, args map[string]interface{}) map[string]interface{} { +func unmarshalArgs(ctx context.Context, whereInput any, args map[string]any) map[string]any { for _, k := range []string{firstField, lastField} { v, ok := args[k] if !ok { diff --git a/entgql/internal/todouuid/ent/gql_collection.go b/entgql/internal/todouuid/ent/gql_collection.go index 11b5f253a..f9ce56fc9 100644 --- a/entgql/internal/todouuid/ent/gql_collection.go +++ b/entgql/internal/todouuid/ent/gql_collection.go @@ -87,7 +87,7 @@ type billproductPaginateArgs struct { opts []BillProductPaginateOption } -func newBillProductPaginateArgs(rv map[string]interface{}) *billproductPaginateArgs { +func newBillProductPaginateArgs(rv map[string]any) *billproductPaginateArgs { args := &billproductPaginateArgs{} if rv == nil { return args @@ -351,7 +351,7 @@ type categoryPaginateArgs struct { opts []CategoryPaginateOption } -func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { +func newCategoryPaginateArgs(rv map[string]any) *categoryPaginateArgs { args := &categoryPaginateArgs{} if rv == nil { return args @@ -372,10 +372,10 @@ func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { switch v := v.(type) { case []*CategoryOrder: args.opts = append(args.opts, WithCategoryOrder(v)) - case []interface{}: + case []any: var orders []*CategoryOrder for i := range v { - mv, ok := v[i].(map[string]interface{}) + mv, ok := v[i].(map[string]any) if !ok { continue } @@ -484,7 +484,7 @@ type friendshipPaginateArgs struct { opts []FriendshipPaginateOption } -func newFriendshipPaginateArgs(rv map[string]interface{}) *friendshipPaginateArgs { +func newFriendshipPaginateArgs(rv map[string]any) *friendshipPaginateArgs { args := &friendshipPaginateArgs{} if rv == nil { return args @@ -639,7 +639,7 @@ type groupPaginateArgs struct { opts []GroupPaginateOption } -func newGroupPaginateArgs(rv map[string]interface{}) *groupPaginateArgs { +func newGroupPaginateArgs(rv map[string]any) *groupPaginateArgs { args := &groupPaginateArgs{} if rv == nil { return args @@ -849,7 +849,7 @@ type todoPaginateArgs struct { opts []TodoPaginateOption } -func newTodoPaginateArgs(rv map[string]interface{}) *todoPaginateArgs { +func newTodoPaginateArgs(rv map[string]any) *todoPaginateArgs { args := &todoPaginateArgs{} if rv == nil { return args @@ -868,7 +868,7 @@ func newTodoPaginateArgs(rv map[string]interface{}) *todoPaginateArgs { } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &TodoOrder{Field: &TodoOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -1060,7 +1060,7 @@ type userPaginateArgs struct { opts []UserPaginateOption } -func newUserPaginateArgs(rv map[string]interface{}) *userPaginateArgs { +func newUserPaginateArgs(rv map[string]any) *userPaginateArgs { args := &userPaginateArgs{} if rv == nil { return args @@ -1079,7 +1079,7 @@ func newUserPaginateArgs(rv map[string]interface{}) *userPaginateArgs { } if v, ok := rv[orderByField]; ok { switch v := v.(type) { - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &UserOrder{Field: &UserOrderField{}, Direction: entgql.OrderDirectionAsc} @@ -1116,35 +1116,18 @@ const ( whereField = "where" ) -func fieldArgs(ctx context.Context, whereInput interface{}, path ...string) map[string]interface{} { - fc := graphql.GetFieldContext(ctx) - if fc == nil { +func fieldArgs(ctx context.Context, whereInput any, path ...string) map[string]any { + field := collectedField(ctx, path...) + if field == nil || field.Arguments == nil { return nil } oc := graphql.GetOperationContext(ctx) - for _, name := range path { - var field *graphql.CollectedField - for _, f := range graphql.CollectFields(oc, fc.Field.Selections, nil) { - if f.Alias == name { - field = &f - break - } - } - if field == nil { - return nil - } - cf, err := fc.Child(ctx, *field) - if err != nil { - args := field.ArgumentMap(oc.Variables) - return unmarshalArgs(ctx, whereInput, args) - } - fc = cf - } - return fc.Args + args := field.ArgumentMap(oc.Variables) + return unmarshalArgs(ctx, whereInput, args) } // unmarshalArgs allows extracting the field arguments from their raw representation. -func unmarshalArgs(ctx context.Context, whereInput interface{}, args map[string]interface{}) map[string]interface{} { +func unmarshalArgs(ctx context.Context, whereInput any, args map[string]any) map[string]any { for _, k := range []string{firstField, lastField} { v, ok := args[k] if !ok { diff --git a/entgql/template/collection.tmpl b/entgql/template/collection.tmpl index b391ad8da..04e68a846 100644 --- a/entgql/template/collection.tmpl +++ b/entgql/template/collection.tmpl @@ -190,7 +190,7 @@ type {{ $paginateArg }} struct { opts []{{ print $name "PaginateOption" }} } -func {{ $newPaginateArg }}(rv map[string]interface{}) *{{ $paginateArg }} { +func {{ $newPaginateArg }}(rv map[string]any) *{{ $paginateArg }} { args := &{{ $paginateArg }}{} if rv == nil { return args @@ -213,10 +213,10 @@ func {{ $newPaginateArg }}(rv map[string]interface{}) *{{ $paginateArg }} { {{- if $multiOrder }} case []*{{ $order }}: args.opts = append(args.opts, {{ print "With" $order }}(v)) - case []interface{}: + case []any: var orders []*{{ $order }} for i := range v { - mv, ok := v[i].(map[string]interface{}) + mv, ok := v[i].(map[string]any) if !ok { continue } @@ -236,7 +236,7 @@ func {{ $newPaginateArg }}(rv map[string]interface{}) *{{ $paginateArg }} { } args.opts = append(args.opts, {{ print "With" $order }}(orders)) {{- else }} - case map[string]interface{}: + case map[string]any: var ( err1, err2 error order = &{{ $order }}{Field: &{{ $orderField }}{}, Direction: entgql.OrderDirectionAsc} @@ -277,35 +277,18 @@ const ( {{- end }} ) -func fieldArgs(ctx context.Context, whereInput interface{}, path ...string) map[string]interface{} { - fc := graphql.GetFieldContext(ctx) - if fc == nil { +func fieldArgs(ctx context.Context, whereInput any, path ...string) map[string]any { + field := collectedField(ctx, path...) + if field == nil || field.Arguments == nil { return nil } oc := graphql.GetOperationContext(ctx) - for _, name := range path { - var field *graphql.CollectedField - for _, f := range graphql.CollectFields(oc, fc.Field.Selections, nil) { - if f.Alias == name { - field = &f - break - } - } - if field == nil { - return nil - } - cf, err := fc.Child(ctx, *field) - if err != nil { - args := field.ArgumentMap(oc.Variables) - return unmarshalArgs(ctx, whereInput, args) - } - fc = cf - } - return fc.Args + args := field.ArgumentMap(oc.Variables) + return unmarshalArgs(ctx, whereInput, args) } // unmarshalArgs allows extracting the field arguments from their raw representation. -func unmarshalArgs(ctx context.Context, whereInput interface{}, args map[string]interface{}) map[string]interface{} { +func unmarshalArgs(ctx context.Context, whereInput any, args map[string]any) map[string]any { for _, k := range []string{firstField, lastField} { v, ok := args[k] if !ok {