diff --git a/entgql/internal/todo/ent.graphql b/entgql/internal/todo/ent.graphql index 70fed6f5b..afd86fcba 100644 --- a/entgql/internal/todo/ent.graphql +++ b/entgql/internal/todo/ent.graphql @@ -132,10 +132,54 @@ input CreateTodoInput { secretID: ID } """ +CreateUserInput is used for create User object. +Input was generated by ent. +""" +input CreateUserInput { + name: String + groupIDs: [ID!] + friendIDs: [ID!] +} +""" Define a Relay Cursor type: https://relay.dev/graphql/connections.htm#sec-Cursor """ scalar Cursor +type Friendship implements Node { + id: ID! + createdAt: Time! + userID: ID! + friendID: ID! + user: User! + friend: User! +} +""" +FriendshipWhereInput is used for filtering Friendship objects. +Input was generated by ent. +""" +input FriendshipWhereInput { + not: FriendshipWhereInput + and: [FriendshipWhereInput!] + or: [FriendshipWhereInput!] + """id field predicates""" + id: ID + idNEQ: ID + idIn: [ID!] + idNotIn: [ID!] + idGT: ID + idGTE: ID + idLT: ID + idLTE: ID + """created_at field predicates""" + createdAt: Time + createdAtNEQ: Time + createdAtIn: [Time!] + createdAtNotIn: [Time!] + createdAtGT: Time + createdAtGTE: Time + createdAtLT: Time + createdAtLTE: Time +} type Group implements Node { id: ID! name: String! @@ -434,6 +478,17 @@ input TodoWhereInput { hasCategory: Boolean hasCategoryWith: [CategoryWhereInput!] } +""" +UpdateUserInput is used for update User object. +Input was generated by ent. +""" +input UpdateUserInput { + name: String + addGroupIDs: [ID!] + removeGroupIDs: [ID!] + addFriendIDs: [ID!] + removeFriendIDs: [ID!] +} type User implements Node { id: ID! name: String! @@ -454,6 +509,7 @@ type User implements Node { where: GroupWhereInput ): GroupConnection! friends: [User!] + friendships: [Friendship!] } """A connection to a list of items.""" type UserConnection { @@ -508,4 +564,7 @@ input UserWhereInput { """friends edge predicates""" hasFriends: Boolean hasFriendsWith: [UserWhereInput!] + """friendships edge predicates""" + hasFriendships: Boolean + hasFriendshipsWith: [FriendshipWhereInput!] } diff --git a/entgql/internal/todo/ent/gql_collection.go b/entgql/internal/todo/ent/gql_collection.go index 59a9d18fc..572582b41 100644 --- a/entgql/internal/todo/ent/gql_collection.go +++ b/entgql/internal/todo/ent/gql_collection.go @@ -198,6 +198,74 @@ func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { return args } +// CollectFields tells the query-builder to eagerly load connected nodes by resolver context. +func (f *FriendshipQuery) CollectFields(ctx context.Context, satisfies ...string) (*FriendshipQuery, error) { + fc := graphql.GetFieldContext(ctx) + if fc == nil { + return f, nil + } + if err := f.collectField(ctx, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil { + return nil, err + } + return f, nil +} + +func (f *FriendshipQuery) collectField(ctx context.Context, op *graphql.OperationContext, field graphql.CollectedField, path []string, satisfies ...string) error { + path = append([]string(nil), path...) + for _, field := range graphql.CollectFields(op, field.Selections, satisfies) { + switch field.Name { + case "user": + var ( + path = append(path, field.Name) + query = &UserQuery{config: f.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + f.withUser = query + case "friend": + var ( + path = append(path, field.Name) + query = &UserQuery{config: f.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + f.withFriend = query + } + } + return nil +} + +type friendshipPaginateArgs struct { + first, last *int + after, before *Cursor + opts []FriendshipPaginateOption +} + +func newFriendshipPaginateArgs(rv map[string]interface{}) *friendshipPaginateArgs { + args := &friendshipPaginateArgs{} + if rv == nil { + return args + } + if v := rv[firstField]; v != nil { + args.first = v.(*int) + } + if v := rv[lastField]; v != nil { + args.last = v.(*int) + } + if v := rv[afterField]; v != nil { + args.after = v.(*Cursor) + } + if v := rv[beforeField]; v != nil { + args.before = v.(*Cursor) + } + if v, ok := rv[whereField].(*FriendshipWhereInput); ok { + args.opts = append(args.opts, WithFriendshipFilter(v.Filter)) + } + return args +} + // CollectFields tells the query-builder to eagerly load connected nodes by resolver context. func (gr *GroupQuery) CollectFields(ctx context.Context, satisfies ...string) (*GroupQuery, error) { fc := graphql.GetFieldContext(ctx) @@ -670,6 +738,15 @@ func (u *UserQuery) collectField(ctx context.Context, op *graphql.OperationConte return err } u.withFriends = query + case "friendships": + var ( + path = append(path, field.Name) + query = &FriendshipQuery{config: u.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + u.withFriendships = query } } return nil diff --git a/entgql/internal/todo/ent/gql_edge.go b/entgql/internal/todo/ent/gql_edge.go index c22eed8c1..a30781f2d 100644 --- a/entgql/internal/todo/ent/gql_edge.go +++ b/entgql/internal/todo/ent/gql_edge.go @@ -94,6 +94,22 @@ func (c *Category) Todos( return conn, nil } +func (f *Friendship) User(ctx context.Context) (*User, error) { + result, err := f.Edges.UserOrErr() + if IsNotLoaded(err) { + result, err = f.QueryUser().Only(ctx) + } + return result, err +} + +func (f *Friendship) Friend(ctx context.Context) (*User, error) { + result, err := f.Edges.FriendOrErr() + if IsNotLoaded(err) { + result, err = f.QueryFriend().Only(ctx) + } + return result, err +} + func (gr *Group) Users( ctx context.Context, after *Cursor, first *int, before *Cursor, last *int, where *UserWhereInput, ) (*UserConnection, error) { @@ -331,3 +347,11 @@ func (u *User) Friends(ctx context.Context) ([]*User, error) { } return result, err } + +func (u *User) Friendships(ctx context.Context) ([]*Friendship, error) { + result, err := u.Edges.FriendshipsOrErr() + if IsNotLoaded(err) { + result, err = u.QueryFriendships().All(ctx) + } + return result, err +} diff --git a/entgql/internal/todo/ent/gql_mutation_input.go b/entgql/internal/todo/ent/gql_mutation_input.go index f2961e46e..fa75df09a 100644 --- a/entgql/internal/todo/ent/gql_mutation_input.go +++ b/entgql/internal/todo/ent/gql_mutation_input.go @@ -55,3 +55,69 @@ func (c *TodoCreate) SetInput(i CreateTodoInput) *TodoCreate { i.Mutate(c.Mutation()) return c } + +// CreateUserInput represents a mutation input for creating users. +type CreateUserInput struct { + Name *string + GroupIDs []int + FriendIDs []int +} + +// Mutate applies the CreateUserInput on the UserMutation builder. +func (i *CreateUserInput) Mutate(m *UserMutation) { + if v := i.Name; v != nil { + m.SetName(*v) + } + if v := i.GroupIDs; len(v) > 0 { + m.AddGroupIDs(v...) + } + if v := i.FriendIDs; len(v) > 0 { + m.AddFriendIDs(v...) + } +} + +// SetInput applies the change-set in the CreateUserInput on the UserCreate builder. +func (c *UserCreate) SetInput(i CreateUserInput) *UserCreate { + i.Mutate(c.Mutation()) + return c +} + +// UpdateUserInput represents a mutation input for updating users. +type UpdateUserInput struct { + Name *string + AddGroupIDs []int + RemoveGroupIDs []int + AddFriendIDs []int + RemoveFriendIDs []int +} + +// Mutate applies the UpdateUserInput on the UserMutation builder. +func (i *UpdateUserInput) Mutate(m *UserMutation) { + if v := i.Name; v != nil { + m.SetName(*v) + } + if v := i.AddGroupIDs; len(v) > 0 { + m.AddGroupIDs(v...) + } + if v := i.RemoveGroupIDs; len(v) > 0 { + m.RemoveGroupIDs(v...) + } + if v := i.AddFriendIDs; len(v) > 0 { + m.AddFriendIDs(v...) + } + if v := i.RemoveFriendIDs; len(v) > 0 { + m.RemoveFriendIDs(v...) + } +} + +// SetInput applies the change-set in the UpdateUserInput on the UserUpdate builder. +func (c *UserUpdate) SetInput(i UpdateUserInput) *UserUpdate { + i.Mutate(c.Mutation()) + return c +} + +// SetInput applies the change-set in the UpdateUserInput on the UserUpdateOne builder. +func (c *UserUpdateOne) SetInput(i UpdateUserInput) *UserUpdateOne { + i.Mutate(c.Mutation()) + return c +} diff --git a/entgql/internal/todo/ent/gql_node.go b/entgql/internal/todo/ent/gql_node.go index 9af7b1744..82d3cec04 100644 --- a/entgql/internal/todo/ent/gql_node.go +++ b/entgql/internal/todo/ent/gql_node.go @@ -25,6 +25,7 @@ import ( "entgo.io/contrib/entgql" "entgo.io/contrib/entgql/internal/todo/ent/category" + "entgo.io/contrib/entgql/internal/todo/ent/friendship" "entgo.io/contrib/entgql/internal/todo/ent/group" "entgo.io/contrib/entgql/internal/todo/ent/todo" "entgo.io/contrib/entgql/internal/todo/ent/user" @@ -132,6 +133,61 @@ func (c *Category) Node(ctx context.Context) (node *Node, err error) { return node, nil } +func (f *Friendship) Node(ctx context.Context) (node *Node, err error) { + node = &Node{ + ID: f.ID, + Type: "Friendship", + Fields: make([]*Field, 3), + Edges: make([]*Edge, 2), + } + var buf []byte + if buf, err = json.Marshal(f.CreatedAt); err != nil { + return nil, err + } + node.Fields[0] = &Field{ + Type: "time.Time", + Name: "created_at", + Value: string(buf), + } + if buf, err = json.Marshal(f.UserID); err != nil { + return nil, err + } + node.Fields[1] = &Field{ + Type: "int", + Name: "user_id", + Value: string(buf), + } + if buf, err = json.Marshal(f.FriendID); err != nil { + return nil, err + } + node.Fields[2] = &Field{ + Type: "int", + Name: "friend_id", + Value: string(buf), + } + node.Edges[0] = &Edge{ + Type: "User", + Name: "user", + } + err = f.QueryUser(). + Select(user.FieldID). + Scan(ctx, &node.Edges[0].IDs) + if err != nil { + return nil, err + } + node.Edges[1] = &Edge{ + Type: "User", + Name: "friend", + } + err = f.QueryFriend(). + Select(user.FieldID). + Scan(ctx, &node.Edges[1].IDs) + if err != nil { + return nil, err + } + return node, nil +} + func (gr *Group) Node(ctx context.Context) (node *Node, err error) { node = &Node{ ID: gr.ID, @@ -247,7 +303,7 @@ func (u *User) Node(ctx context.Context) (node *Node, err error) { ID: u.ID, Type: "User", Fields: make([]*Field, 1), - Edges: make([]*Edge, 2), + Edges: make([]*Edge, 3), } var buf []byte if buf, err = json.Marshal(u.Name); err != nil { @@ -278,6 +334,16 @@ func (u *User) Node(ctx context.Context) (node *Node, err error) { if err != nil { return nil, err } + node.Edges[2] = &Edge{ + Type: "Friendship", + Name: "friendships", + } + err = u.QueryFriendships(). + Select(friendship.FieldID). + Scan(ctx, &node.Edges[2].IDs) + if err != nil { + return nil, err + } return node, nil } @@ -360,6 +426,18 @@ func (c *Client) noder(ctx context.Context, table string, id int) (Noder, error) return nil, err } return n, nil + case friendship.Table: + query := c.Friendship.Query(). + Where(friendship.ID(id)) + query, err := query.CollectFields(ctx, "Friendship") + if err != nil { + return nil, err + } + n, err := query.Only(ctx) + if err != nil { + return nil, err + } + return n, nil case group.Table: query := c.Group.Query(). Where(group.ID(id)) @@ -485,6 +563,22 @@ func (c *Client) noders(ctx context.Context, table string, ids []int) ([]Noder, *noder = node } } + case friendship.Table: + query := c.Friendship.Query(). + Where(friendship.IDIn(ids...)) + query, err := query.CollectFields(ctx, "Friendship") + if err != nil { + return nil, err + } + nodes, err := query.All(ctx) + if err != nil { + return nil, err + } + for _, node := range nodes { + for _, noder := range idmap[node.ID] { + *noder = node + } + } case group.Table: query := c.Group.Query(). Where(group.IDIn(ids...)) diff --git a/entgql/internal/todo/ent/gql_pagination.go b/entgql/internal/todo/ent/gql_pagination.go index 5e74d1106..a28e72d12 100644 --- a/entgql/internal/todo/ent/gql_pagination.go +++ b/entgql/internal/todo/ent/gql_pagination.go @@ -26,6 +26,7 @@ import ( "strings" "entgo.io/contrib/entgql/internal/todo/ent/category" + "entgo.io/contrib/entgql/internal/todo/ent/friendship" "entgo.io/contrib/entgql/internal/todo/ent/group" "entgo.io/contrib/entgql/internal/todo/ent/todo" "entgo.io/contrib/entgql/internal/todo/ent/user" @@ -550,6 +551,241 @@ func (c *Category) ToEdge(order *CategoryOrder) *CategoryEdge { } } +// FriendshipEdge is the edge representation of Friendship. +type FriendshipEdge struct { + Node *Friendship `json:"node"` + Cursor Cursor `json:"cursor"` +} + +// FriendshipConnection is the connection containing edges to Friendship. +type FriendshipConnection struct { + Edges []*FriendshipEdge `json:"edges"` + PageInfo PageInfo `json:"pageInfo"` + TotalCount int `json:"totalCount"` +} + +func (c *FriendshipConnection) build(nodes []*Friendship, pager *friendshipPager, after *Cursor, first *int, before *Cursor, last *int) { + c.PageInfo.HasNextPage = before != nil + c.PageInfo.HasPreviousPage = after != nil + if first != nil && *first+1 == len(nodes) { + c.PageInfo.HasNextPage = true + nodes = nodes[:len(nodes)-1] + } else if last != nil && *last+1 == len(nodes) { + c.PageInfo.HasPreviousPage = true + nodes = nodes[:len(nodes)-1] + } + var nodeAt func(int) *Friendship + if last != nil { + n := len(nodes) - 1 + nodeAt = func(i int) *Friendship { + return nodes[n-i] + } + } else { + nodeAt = func(i int) *Friendship { + return nodes[i] + } + } + c.Edges = make([]*FriendshipEdge, len(nodes)) + for i := range nodes { + node := nodeAt(i) + c.Edges[i] = &FriendshipEdge{ + Node: node, + Cursor: pager.toCursor(node), + } + } + if l := len(c.Edges); l > 0 { + c.PageInfo.StartCursor = &c.Edges[0].Cursor + c.PageInfo.EndCursor = &c.Edges[l-1].Cursor + } + if c.TotalCount == 0 { + c.TotalCount = len(nodes) + } +} + +// FriendshipPaginateOption enables pagination customization. +type FriendshipPaginateOption func(*friendshipPager) error + +// WithFriendshipOrder configures pagination ordering. +func WithFriendshipOrder(order *FriendshipOrder) FriendshipPaginateOption { + if order == nil { + order = DefaultFriendshipOrder + } + o := *order + return func(pager *friendshipPager) error { + if err := o.Direction.Validate(); err != nil { + return err + } + if o.Field == nil { + o.Field = DefaultFriendshipOrder.Field + } + pager.order = &o + return nil + } +} + +// WithFriendshipFilter configures pagination filter. +func WithFriendshipFilter(filter func(*FriendshipQuery) (*FriendshipQuery, error)) FriendshipPaginateOption { + return func(pager *friendshipPager) error { + if filter == nil { + return errors.New("FriendshipQuery filter cannot be nil") + } + pager.filter = filter + return nil + } +} + +type friendshipPager struct { + order *FriendshipOrder + filter func(*FriendshipQuery) (*FriendshipQuery, error) +} + +func newFriendshipPager(opts []FriendshipPaginateOption) (*friendshipPager, error) { + pager := &friendshipPager{} + for _, opt := range opts { + if err := opt(pager); err != nil { + return nil, err + } + } + if pager.order == nil { + pager.order = DefaultFriendshipOrder + } + return pager, nil +} + +func (p *friendshipPager) applyFilter(query *FriendshipQuery) (*FriendshipQuery, error) { + if p.filter != nil { + return p.filter(query) + } + return query, nil +} + +func (p *friendshipPager) toCursor(f *Friendship) Cursor { + return p.order.Field.toCursor(f) +} + +func (p *friendshipPager) applyCursors(query *FriendshipQuery, after, before *Cursor) *FriendshipQuery { + for _, predicate := range cursorsToPredicates( + p.order.Direction, after, before, + p.order.Field.field, DefaultFriendshipOrder.Field.field, + ) { + query = query.Where(predicate) + } + return query +} + +func (p *friendshipPager) applyOrder(query *FriendshipQuery, reverse bool) *FriendshipQuery { + direction := p.order.Direction + if reverse { + direction = direction.reverse() + } + query = query.Order(direction.orderFunc(p.order.Field.field)) + if p.order.Field != DefaultFriendshipOrder.Field { + query = query.Order(direction.orderFunc(DefaultFriendshipOrder.Field.field)) + } + return query +} + +func (p *friendshipPager) orderExpr(reverse bool) sql.Querier { + direction := p.order.Direction + if reverse { + direction = direction.reverse() + } + return sql.ExprFunc(func(b *sql.Builder) { + b.Ident(p.order.Field.field).Pad().WriteString(string(direction)) + if p.order.Field != DefaultFriendshipOrder.Field { + b.Comma().Ident(DefaultFriendshipOrder.Field.field).Pad().WriteString(string(direction)) + } + }) +} + +// Paginate executes the query and returns a relay based cursor connection to Friendship. +func (f *FriendshipQuery) Paginate( + ctx context.Context, after *Cursor, first *int, + before *Cursor, last *int, opts ...FriendshipPaginateOption, +) (*FriendshipConnection, error) { + if err := validateFirstLast(first, last); err != nil { + return nil, err + } + pager, err := newFriendshipPager(opts) + if err != nil { + return nil, err + } + if f, err = pager.applyFilter(f); err != nil { + return nil, err + } + conn := &FriendshipConnection{Edges: []*FriendshipEdge{}} + if !hasCollectedField(ctx, edgesField) || first != nil && *first == 0 || last != nil && *last == 0 { + if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) { + if conn.TotalCount, err = f.Count(ctx); err != nil { + return nil, err + } + conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0 + conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0 + } + return conn, nil + } + + if (after != nil || first != nil || before != nil || last != nil) && hasCollectedField(ctx, totalCountField) { + count, err := f.Clone().Count(ctx) + if err != nil { + return nil, err + } + conn.TotalCount = count + } + + f = pager.applyCursors(f, after, before) + f = pager.applyOrder(f, last != nil) + if limit := paginateLimit(first, last); limit != 0 { + f.Limit(limit) + } + if field := collectedField(ctx, edgesField, nodeField); field != nil { + if err := f.collectField(ctx, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil { + return nil, err + } + } + + nodes, err := f.All(ctx) + if err != nil || len(nodes) == 0 { + return conn, err + } + conn.build(nodes, pager, after, first, before, last) + return conn, nil +} + +// FriendshipOrderField defines the ordering field of Friendship. +type FriendshipOrderField struct { + field string + toCursor func(*Friendship) Cursor +} + +// FriendshipOrder defines the ordering of Friendship. +type FriendshipOrder struct { + Direction OrderDirection `json:"direction"` + Field *FriendshipOrderField `json:"field"` +} + +// DefaultFriendshipOrder is the default ordering of Friendship. +var DefaultFriendshipOrder = &FriendshipOrder{ + Direction: OrderDirectionAsc, + Field: &FriendshipOrderField{ + field: friendship.FieldID, + toCursor: func(f *Friendship) Cursor { + return Cursor{ID: f.ID} + }, + }, +} + +// ToEdge converts Friendship into FriendshipEdge. +func (f *Friendship) ToEdge(order *FriendshipOrder) *FriendshipEdge { + if order == nil { + order = DefaultFriendshipOrder + } + return &FriendshipEdge{ + Node: f, + Cursor: order.Field.toCursor(f), + } +} + // GroupEdge is the edge representation of Group. type GroupEdge struct { Node *Group `json:"node"` diff --git a/entgql/internal/todo/ent/gql_where_input.go b/entgql/internal/todo/ent/gql_where_input.go index 9ceadf0b4..cc1b6d1fc 100644 --- a/entgql/internal/todo/ent/gql_where_input.go +++ b/entgql/internal/todo/ent/gql_where_input.go @@ -22,6 +22,7 @@ import ( "time" "entgo.io/contrib/entgql/internal/todo/ent/category" + "entgo.io/contrib/entgql/internal/todo/ent/friendship" "entgo.io/contrib/entgql/internal/todo/ent/group" "entgo.io/contrib/entgql/internal/todo/ent/predicate" "entgo.io/contrib/entgql/internal/todo/ent/schema/schematype" @@ -373,6 +374,244 @@ func (i *CategoryWhereInput) P() (predicate.Category, error) { } } +// FriendshipWhereInput represents a where input for filtering Friendship queries. +type FriendshipWhereInput struct { + Predicates []predicate.Friendship `json:"-"` + Not *FriendshipWhereInput `json:"not,omitempty"` + Or []*FriendshipWhereInput `json:"or,omitempty"` + And []*FriendshipWhereInput `json:"and,omitempty"` + + // "id" field predicates. + ID *int `json:"id,omitempty"` + IDNEQ *int `json:"idNEQ,omitempty"` + IDIn []int `json:"idIn,omitempty"` + IDNotIn []int `json:"idNotIn,omitempty"` + IDGT *int `json:"idGT,omitempty"` + IDGTE *int `json:"idGTE,omitempty"` + IDLT *int `json:"idLT,omitempty"` + IDLTE *int `json:"idLTE,omitempty"` + + // "created_at" field predicates. + CreatedAt *time.Time `json:"createdAt,omitempty"` + CreatedAtNEQ *time.Time `json:"createdAtNEQ,omitempty"` + CreatedAtIn []time.Time `json:"createdAtIn,omitempty"` + CreatedAtNotIn []time.Time `json:"createdAtNotIn,omitempty"` + CreatedAtGT *time.Time `json:"createdAtGT,omitempty"` + CreatedAtGTE *time.Time `json:"createdAtGTE,omitempty"` + CreatedAtLT *time.Time `json:"createdAtLT,omitempty"` + CreatedAtLTE *time.Time `json:"createdAtLTE,omitempty"` + + // "user_id" field predicates. + UserID *int `json:"userID,omitempty"` + UserIDNEQ *int `json:"userIDNEQ,omitempty"` + UserIDIn []int `json:"userIDIn,omitempty"` + UserIDNotIn []int `json:"userIDNotIn,omitempty"` + + // "friend_id" field predicates. + FriendID *int `json:"friendID,omitempty"` + FriendIDNEQ *int `json:"friendIDNEQ,omitempty"` + FriendIDIn []int `json:"friendIDIn,omitempty"` + FriendIDNotIn []int `json:"friendIDNotIn,omitempty"` + + // "user" edge predicates. + HasUser *bool `json:"hasUser,omitempty"` + HasUserWith []*UserWhereInput `json:"hasUserWith,omitempty"` + + // "friend" edge predicates. + HasFriend *bool `json:"hasFriend,omitempty"` + HasFriendWith []*UserWhereInput `json:"hasFriendWith,omitempty"` +} + +// AddPredicates adds custom predicates to the where input to be used during the filtering phase. +func (i *FriendshipWhereInput) AddPredicates(predicates ...predicate.Friendship) { + i.Predicates = append(i.Predicates, predicates...) +} + +// Filter applies the FriendshipWhereInput filter on the FriendshipQuery builder. +func (i *FriendshipWhereInput) Filter(q *FriendshipQuery) (*FriendshipQuery, error) { + if i == nil { + return q, nil + } + p, err := i.P() + if err != nil { + if err == ErrEmptyFriendshipWhereInput { + return q, nil + } + return nil, err + } + return q.Where(p), nil +} + +// ErrEmptyFriendshipWhereInput is returned in case the FriendshipWhereInput is empty. +var ErrEmptyFriendshipWhereInput = errors.New("ent: empty predicate FriendshipWhereInput") + +// P returns a predicate for filtering friendships. +// An error is returned if the input is empty or invalid. +func (i *FriendshipWhereInput) P() (predicate.Friendship, error) { + var predicates []predicate.Friendship + if i.Not != nil { + p, err := i.Not.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'not'", err) + } + predicates = append(predicates, friendship.Not(p)) + } + switch n := len(i.Or); { + case n == 1: + p, err := i.Or[0].P() + if err != nil { + return nil, fmt.Errorf("%w: field 'or'", err) + } + predicates = append(predicates, p) + case n > 1: + or := make([]predicate.Friendship, 0, n) + for _, w := range i.Or { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'or'", err) + } + or = append(or, p) + } + predicates = append(predicates, friendship.Or(or...)) + } + switch n := len(i.And); { + case n == 1: + p, err := i.And[0].P() + if err != nil { + return nil, fmt.Errorf("%w: field 'and'", err) + } + predicates = append(predicates, p) + case n > 1: + and := make([]predicate.Friendship, 0, n) + for _, w := range i.And { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'and'", err) + } + and = append(and, p) + } + predicates = append(predicates, friendship.And(and...)) + } + predicates = append(predicates, i.Predicates...) + if i.ID != nil { + predicates = append(predicates, friendship.IDEQ(*i.ID)) + } + if i.IDNEQ != nil { + predicates = append(predicates, friendship.IDNEQ(*i.IDNEQ)) + } + if len(i.IDIn) > 0 { + predicates = append(predicates, friendship.IDIn(i.IDIn...)) + } + if len(i.IDNotIn) > 0 { + predicates = append(predicates, friendship.IDNotIn(i.IDNotIn...)) + } + if i.IDGT != nil { + predicates = append(predicates, friendship.IDGT(*i.IDGT)) + } + if i.IDGTE != nil { + predicates = append(predicates, friendship.IDGTE(*i.IDGTE)) + } + if i.IDLT != nil { + predicates = append(predicates, friendship.IDLT(*i.IDLT)) + } + if i.IDLTE != nil { + predicates = append(predicates, friendship.IDLTE(*i.IDLTE)) + } + if i.CreatedAt != nil { + predicates = append(predicates, friendship.CreatedAtEQ(*i.CreatedAt)) + } + if i.CreatedAtNEQ != nil { + predicates = append(predicates, friendship.CreatedAtNEQ(*i.CreatedAtNEQ)) + } + if len(i.CreatedAtIn) > 0 { + predicates = append(predicates, friendship.CreatedAtIn(i.CreatedAtIn...)) + } + if len(i.CreatedAtNotIn) > 0 { + predicates = append(predicates, friendship.CreatedAtNotIn(i.CreatedAtNotIn...)) + } + if i.CreatedAtGT != nil { + predicates = append(predicates, friendship.CreatedAtGT(*i.CreatedAtGT)) + } + if i.CreatedAtGTE != nil { + predicates = append(predicates, friendship.CreatedAtGTE(*i.CreatedAtGTE)) + } + if i.CreatedAtLT != nil { + predicates = append(predicates, friendship.CreatedAtLT(*i.CreatedAtLT)) + } + if i.CreatedAtLTE != nil { + predicates = append(predicates, friendship.CreatedAtLTE(*i.CreatedAtLTE)) + } + if i.UserID != nil { + predicates = append(predicates, friendship.UserIDEQ(*i.UserID)) + } + if i.UserIDNEQ != nil { + predicates = append(predicates, friendship.UserIDNEQ(*i.UserIDNEQ)) + } + if len(i.UserIDIn) > 0 { + predicates = append(predicates, friendship.UserIDIn(i.UserIDIn...)) + } + if len(i.UserIDNotIn) > 0 { + predicates = append(predicates, friendship.UserIDNotIn(i.UserIDNotIn...)) + } + if i.FriendID != nil { + predicates = append(predicates, friendship.FriendIDEQ(*i.FriendID)) + } + if i.FriendIDNEQ != nil { + predicates = append(predicates, friendship.FriendIDNEQ(*i.FriendIDNEQ)) + } + if len(i.FriendIDIn) > 0 { + predicates = append(predicates, friendship.FriendIDIn(i.FriendIDIn...)) + } + if len(i.FriendIDNotIn) > 0 { + predicates = append(predicates, friendship.FriendIDNotIn(i.FriendIDNotIn...)) + } + + if i.HasUser != nil { + p := friendship.HasUser() + if !*i.HasUser { + p = friendship.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasUserWith) > 0 { + with := make([]predicate.User, 0, len(i.HasUserWith)) + for _, w := range i.HasUserWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasUserWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, friendship.HasUserWith(with...)) + } + if i.HasFriend != nil { + p := friendship.HasFriend() + if !*i.HasFriend { + p = friendship.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasFriendWith) > 0 { + with := make([]predicate.User, 0, len(i.HasFriendWith)) + for _, w := range i.HasFriendWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasFriendWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, friendship.HasFriendWith(with...)) + } + switch len(predicates) { + case 0: + return nil, ErrEmptyFriendshipWhereInput + case 1: + return predicates[0], nil + default: + return friendship.And(predicates...), nil + } +} + // GroupWhereInput represents a where input for filtering Group queries. type GroupWhereInput struct { Predicates []predicate.Group `json:"-"` @@ -968,6 +1207,10 @@ type UserWhereInput struct { // "friends" edge predicates. HasFriends *bool `json:"hasFriends,omitempty"` HasFriendsWith []*UserWhereInput `json:"hasFriendsWith,omitempty"` + + // "friendships" edge predicates. + HasFriendships *bool `json:"hasFriendships,omitempty"` + HasFriendshipsWith []*FriendshipWhereInput `json:"hasFriendshipsWith,omitempty"` } // AddPredicates adds custom predicates to the where input to be used during the filtering phase. @@ -1141,6 +1384,24 @@ func (i *UserWhereInput) P() (predicate.User, error) { } predicates = append(predicates, user.HasFriendsWith(with...)) } + if i.HasFriendships != nil { + p := user.HasFriendships() + if !*i.HasFriendships { + p = user.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasFriendshipsWith) > 0 { + with := make([]predicate.Friendship, 0, len(i.HasFriendshipsWith)) + for _, w := range i.HasFriendshipsWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasFriendshipsWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, user.HasFriendshipsWith(with...)) + } switch len(predicates) { case 0: return nil, ErrEmptyUserWhereInput diff --git a/entgql/internal/todo/ent/schema/user.go b/entgql/internal/todo/ent/schema/user.go index d83588805..3c87b8161 100644 --- a/entgql/internal/todo/ent/schema/user.go +++ b/entgql/internal/todo/ent/schema/user.go @@ -50,5 +50,9 @@ func (User) Annotations() []schema.Annotation { return []schema.Annotation{ entgql.RelayConnection(), entgql.QueryField(), + entgql.Mutations( + entgql.MutationCreate(), + entgql.MutationUpdate(), + ), } } diff --git a/entgql/internal/todo/ent/user.go b/entgql/internal/todo/ent/user.go index 4463115e9..9c6164783 100644 --- a/entgql/internal/todo/ent/user.go +++ b/entgql/internal/todo/ent/user.go @@ -48,7 +48,7 @@ type UserEdges struct { // type was loaded (or requested) in eager-loading or not. loadedTypes [3]bool // totalCount holds the count of the edges above. - totalCount [2]*int + totalCount [3]*int } // GroupsOrErr returns the Groups value or an error if the edge diff --git a/entgql/internal/todo/generated.go b/entgql/internal/todo/generated.go index bd2578388..b87c45bce 100644 --- a/entgql/internal/todo/generated.go +++ b/entgql/internal/todo/generated.go @@ -65,6 +65,15 @@ type ComplexityRoot struct { MaxMembers func(childComplexity int) int } + Friendship struct { + CreatedAt func(childComplexity int) int + Friend func(childComplexity int) int + FriendID func(childComplexity int) int + ID func(childComplexity int) int + User func(childComplexity int) int + UserID func(childComplexity int) int + } + Group struct { ID func(childComplexity int) int Name func(childComplexity int) int @@ -127,10 +136,11 @@ type ComplexityRoot struct { } User struct { - Friends func(childComplexity int) int - Groups func(childComplexity int, after *ent.Cursor, first *int, before *ent.Cursor, last *int, where *ent.GroupWhereInput) int - ID func(childComplexity int) int - Name func(childComplexity int) int + Friends func(childComplexity int) int + Friendships func(childComplexity int) int + Groups func(childComplexity int, after *ent.Cursor, first *int, before *ent.Cursor, last *int, where *ent.GroupWhereInput) int + ID func(childComplexity int) int + Name func(childComplexity int) int } UserConnection struct { @@ -245,6 +255,48 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.CategoryConfig.MaxMembers(childComplexity), true + case "Friendship.createdAt": + if e.complexity.Friendship.CreatedAt == nil { + break + } + + return e.complexity.Friendship.CreatedAt(childComplexity), true + + case "Friendship.friend": + if e.complexity.Friendship.Friend == nil { + break + } + + return e.complexity.Friendship.Friend(childComplexity), true + + case "Friendship.friendID": + if e.complexity.Friendship.FriendID == nil { + break + } + + return e.complexity.Friendship.FriendID(childComplexity), true + + case "Friendship.id": + if e.complexity.Friendship.ID == nil { + break + } + + return e.complexity.Friendship.ID(childComplexity), true + + case "Friendship.user": + if e.complexity.Friendship.User == nil { + break + } + + return e.complexity.Friendship.User(childComplexity), true + + case "Friendship.userID": + if e.complexity.Friendship.UserID == nil { + break + } + + return e.complexity.Friendship.UserID(childComplexity), true + case "Group.id": if e.complexity.Group.ID == nil { break @@ -530,6 +582,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.User.Friends(childComplexity), true + case "User.friendships": + if e.complexity.User.Friendships == nil { + break + } + + return e.complexity.User.Friendships(childComplexity), true + case "User.groups": if e.complexity.User.Groups == nil { break @@ -603,9 +662,12 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler { ec.unmarshalInputCategoryOrder, ec.unmarshalInputCategoryWhereInput, ec.unmarshalInputCreateTodoInput, + ec.unmarshalInputCreateUserInput, + ec.unmarshalInputFriendshipWhereInput, ec.unmarshalInputGroupWhereInput, ec.unmarshalInputTodoOrder, ec.unmarshalInputTodoWhereInput, + ec.unmarshalInputUpdateUserInput, ec.unmarshalInputUserWhereInput, ) first := true @@ -829,10 +891,54 @@ input CreateTodoInput { secretID: ID } """ +CreateUserInput is used for create User object. +Input was generated by ent. +""" +input CreateUserInput { + name: String + groupIDs: [ID!] + friendIDs: [ID!] +} +""" Define a Relay Cursor type: https://relay.dev/graphql/connections.htm#sec-Cursor """ scalar Cursor +type Friendship implements Node { + id: ID! + createdAt: Time! + userID: ID! + friendID: ID! + user: User! + friend: User! +} +""" +FriendshipWhereInput is used for filtering Friendship objects. +Input was generated by ent. +""" +input FriendshipWhereInput { + not: FriendshipWhereInput + and: [FriendshipWhereInput!] + or: [FriendshipWhereInput!] + """id field predicates""" + id: ID + idNEQ: ID + idIn: [ID!] + idNotIn: [ID!] + idGT: ID + idGTE: ID + idLT: ID + idLTE: ID + """created_at field predicates""" + createdAt: Time + createdAtNEQ: Time + createdAtIn: [Time!] + createdAtNotIn: [Time!] + createdAtGT: Time + createdAtGTE: Time + createdAtLT: Time + createdAtLTE: Time +} type Group implements Node { id: ID! name: String! @@ -1131,6 +1237,17 @@ input TodoWhereInput { hasCategory: Boolean hasCategoryWith: [CategoryWhereInput!] } +""" +UpdateUserInput is used for update User object. +Input was generated by ent. +""" +input UpdateUserInput { + name: String + addGroupIDs: [ID!] + removeGroupIDs: [ID!] + addFriendIDs: [ID!] + removeFriendIDs: [ID!] +} type User implements Node { id: ID! name: String! @@ -1151,6 +1268,7 @@ type User implements Node { where: GroupWhereInput ): GroupConnection! friends: [User!] + friendships: [Friendship!] } """A connection to a list of items.""" type UserConnection { @@ -1205,6 +1323,9 @@ input UserWhereInput { """friends edge predicates""" hasFriends: Boolean hasFriendsWith: [UserWhereInput!] + """friendships edge predicates""" + hasFriendships: Boolean + hasFriendshipsWith: [FriendshipWhereInput!] } `, BuiltIn: false}, } @@ -2059,8 +2180,281 @@ func (ec *executionContext) fieldContext_Category_todos(ctx context.Context, fie return fc, nil } -func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField, obj *schematype.CategoryConfig) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_CategoryConfig_maxMembers(ctx, field) +func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField, obj *schematype.CategoryConfig) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_CategoryConfig_maxMembers(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.MaxMembers, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(int) + fc.Result = res + return ec.marshalOInt2int(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "CategoryConfig", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Int does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_id(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_id(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.ID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(int) + fc.Result = res + return ec.marshalNID2int(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_id(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_createdAt(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_createdAt(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.CreatedAt, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(time.Time) + fc.Result = res + return ec.marshalNTime2timeᚐTime(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_createdAt(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Time does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_userID(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_userID(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.UserID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(int) + fc.Result = res + return ec.marshalNID2int(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_userID(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_friendID(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_friendID(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.FriendID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(int) + fc.Result = res + return ec.marshalNID2int(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_friendID(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_user(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_user(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.User(ctx) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(*ent.User) + fc.Result = res + return ec.marshalNUser2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐUser(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_user(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: true, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "id": + return ec.fieldContext_User_id(ctx, field) + case "name": + return ec.fieldContext_User_name(ctx, field) + case "groups": + return ec.fieldContext_User_groups(ctx, field) + case "friends": + return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type User", field.Name) + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_friend(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_friend(ctx, field) if err != nil { return graphql.Null } @@ -2073,28 +2467,43 @@ func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, fiel }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.MaxMembers, nil + return obj.Friend(ctx) }) if err != nil { ec.Error(ctx, err) return graphql.Null } if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } return graphql.Null } - res := resTmp.(int) + res := resTmp.(*ent.User) fc.Result = res - return ec.marshalOInt2int(ctx, field.Selections, res) + return ec.marshalNUser2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐUser(ctx, field.Selections, res) } -func (ec *executionContext) fieldContext_CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { +func (ec *executionContext) fieldContext_Friendship_friend(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { fc = &graphql.FieldContext{ - Object: "CategoryConfig", + Object: "Friendship", Field: field, - IsMethod: false, + IsMethod: true, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type Int does not have child fields") + switch field.Name { + case "id": + return ec.fieldContext_User_id(ctx, field) + case "name": + return ec.fieldContext_User_name(ctx, field) + case "groups": + return ec.fieldContext_User_groups(ctx, field) + case "friends": + return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, } return fc, nil @@ -4136,6 +4545,8 @@ func (ec *executionContext) fieldContext_User_friends(ctx context.Context, field return ec.fieldContext_User_groups(ctx, field) case "friends": return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, @@ -4143,6 +4554,61 @@ func (ec *executionContext) fieldContext_User_friends(ctx context.Context, field return fc, nil } +func (ec *executionContext) _User_friendships(ctx context.Context, field graphql.CollectedField, obj *ent.User) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_User_friendships(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Friendships(ctx) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]*ent.Friendship) + fc.Result = res + return ec.marshalOFriendship2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendshipᚄ(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_User_friendships(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "User", + Field: field, + IsMethod: true, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "id": + return ec.fieldContext_Friendship_id(ctx, field) + case "createdAt": + return ec.fieldContext_Friendship_createdAt(ctx, field) + case "userID": + return ec.fieldContext_Friendship_userID(ctx, field) + case "friendID": + return ec.fieldContext_Friendship_friendID(ctx, field) + case "user": + return ec.fieldContext_Friendship_user(ctx, field) + case "friend": + return ec.fieldContext_Friendship_friend(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type Friendship", field.Name) + }, + } + return fc, nil +} + func (ec *executionContext) _UserConnection_edges(ctx context.Context, field graphql.CollectedField, obj *ent.UserConnection) (ret graphql.Marshaler) { fc, err := ec.fieldContext_UserConnection_edges(ctx, field) if err != nil { @@ -4332,6 +4798,8 @@ func (ec *executionContext) fieldContext_UserEdge_node(ctx context.Context, fiel return ec.fieldContext_User_groups(ctx, field) case "friends": return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, @@ -6734,43 +7202,249 @@ func (ec *executionContext) unmarshalInputCreateTodoInput(ctx context.Context, o if err != nil { return it, err } - case "text": + case "text": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("text")) + it.Text, err = ec.unmarshalNString2string(ctx, v) + if err != nil { + return it, err + } + case "parentID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("parentID")) + it.ParentID, err = ec.unmarshalOID2ᚖint(ctx, v) + if err != nil { + return it, err + } + case "childIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("childIDs")) + it.ChildIDs, err = ec.unmarshalOID2ᚕintᚄ(ctx, v) + if err != nil { + return it, err + } + case "categoryID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("categoryID")) + it.CategoryID, err = ec.unmarshalOID2ᚖint(ctx, v) + if err != nil { + return it, err + } + case "secretID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("secretID")) + it.SecretID, err = ec.unmarshalOID2ᚖint(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + +func (ec *executionContext) unmarshalInputCreateUserInput(ctx context.Context, obj interface{}) (ent.CreateUserInput, error) { + var it ent.CreateUserInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "name": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name")) + it.Name, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "groupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("groupIDs")) + it.GroupIDs, err = ec.unmarshalOID2ᚕintᚄ(ctx, v) + if err != nil { + return it, err + } + case "friendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("friendIDs")) + it.FriendIDs, err = ec.unmarshalOID2ᚕintᚄ(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + +func (ec *executionContext) unmarshalInputFriendshipWhereInput(ctx context.Context, obj interface{}) (ent.FriendshipWhereInput, error) { + var it ent.FriendshipWhereInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "not": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("not")) + it.Not, err = ec.unmarshalOFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendshipWhereInput(ctx, v) + if err != nil { + return it, err + } + case "and": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("and")) + it.And, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } + case "or": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("or")) + it.Or, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } + case "id": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("id")) + it.ID, err = ec.unmarshalOID2ᚖint(ctx, v) + if err != nil { + return it, err + } + case "idNEQ": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idNEQ")) + it.IDNEQ, err = ec.unmarshalOID2ᚖint(ctx, v) + if err != nil { + return it, err + } + case "idIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idIn")) + it.IDIn, err = ec.unmarshalOID2ᚕintᚄ(ctx, v) + if err != nil { + return it, err + } + case "idNotIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idNotIn")) + it.IDNotIn, err = ec.unmarshalOID2ᚕintᚄ(ctx, v) + if err != nil { + return it, err + } + case "idGT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idGT")) + it.IDGT, err = ec.unmarshalOID2ᚖint(ctx, v) + if err != nil { + return it, err + } + case "idGTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idGTE")) + it.IDGTE, err = ec.unmarshalOID2ᚖint(ctx, v) + if err != nil { + return it, err + } + case "idLT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idLT")) + it.IDLT, err = ec.unmarshalOID2ᚖint(ctx, v) + if err != nil { + return it, err + } + case "idLTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idLTE")) + it.IDLTE, err = ec.unmarshalOID2ᚖint(ctx, v) + if err != nil { + return it, err + } + case "createdAt": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAt")) + it.CreatedAt, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + case "createdAtNEQ": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtNEQ")) + it.CreatedAtNEQ, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + case "createdAtIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtIn")) + it.CreatedAtIn, err = ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) + if err != nil { + return it, err + } + case "createdAtNotIn": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("text")) - it.Text, err = ec.unmarshalNString2string(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtNotIn")) + it.CreatedAtNotIn, err = ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) if err != nil { return it, err } - case "parentID": + case "createdAtGT": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("parentID")) - it.ParentID, err = ec.unmarshalOID2ᚖint(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtGT")) + it.CreatedAtGT, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "childIDs": + case "createdAtGTE": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("childIDs")) - it.ChildIDs, err = ec.unmarshalOID2ᚕintᚄ(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtGTE")) + it.CreatedAtGTE, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "categoryID": + case "createdAtLT": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("categoryID")) - it.CategoryID, err = ec.unmarshalOID2ᚖint(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtLT")) + it.CreatedAtLT, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "secretID": + case "createdAtLTE": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("secretID")) - it.SecretID, err = ec.unmarshalOID2ᚖint(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtLTE")) + it.CreatedAtLTE, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } @@ -7512,6 +8186,61 @@ func (ec *executionContext) unmarshalInputTodoWhereInput(ctx context.Context, ob return it, nil } +func (ec *executionContext) unmarshalInputUpdateUserInput(ctx context.Context, obj interface{}) (ent.UpdateUserInput, error) { + var it ent.UpdateUserInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "name": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name")) + it.Name, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "addGroupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("addGroupIDs")) + it.AddGroupIDs, err = ec.unmarshalOID2ᚕintᚄ(ctx, v) + if err != nil { + return it, err + } + case "removeGroupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("removeGroupIDs")) + it.RemoveGroupIDs, err = ec.unmarshalOID2ᚕintᚄ(ctx, v) + if err != nil { + return it, err + } + case "addFriendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("addFriendIDs")) + it.AddFriendIDs, err = ec.unmarshalOID2ᚕintᚄ(ctx, v) + if err != nil { + return it, err + } + case "removeFriendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("removeFriendIDs")) + it.RemoveFriendIDs, err = ec.unmarshalOID2ᚕintᚄ(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + func (ec *executionContext) unmarshalInputUserWhereInput(ctx context.Context, obj interface{}) (ent.UserWhereInput, error) { var it ent.UserWhereInput asMap := map[string]interface{}{} @@ -7745,6 +8474,22 @@ func (ec *executionContext) unmarshalInputUserWhereInput(ctx context.Context, ob if err != nil { return it, err } + case "hasFriendships": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("hasFriendships")) + it.HasFriendships, err = ec.unmarshalOBoolean2ᚖbool(ctx, v) + if err != nil { + return it, err + } + case "hasFriendshipsWith": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("hasFriendshipsWith")) + it.HasFriendshipsWith, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } } } @@ -7764,6 +8509,11 @@ func (ec *executionContext) _Node(ctx context.Context, sel ast.SelectionSet, obj return graphql.Null } return ec._Category(ctx, sel, obj) + case *ent.Friendship: + if obj == nil { + return graphql.Null + } + return ec._Friendship(ctx, sel, obj) case *ent.Group: if obj == nil { return graphql.Null @@ -7891,6 +8641,95 @@ func (ec *executionContext) _CategoryConfig(ctx context.Context, sel ast.Selecti return out } +var friendshipImplementors = []string{"Friendship", "Node"} + +func (ec *executionContext) _Friendship(ctx context.Context, sel ast.SelectionSet, obj *ent.Friendship) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, friendshipImplementors) + out := graphql.NewFieldSet(fields) + var invalids uint32 + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("Friendship") + case "id": + + out.Values[i] = ec._Friendship_id(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "createdAt": + + out.Values[i] = ec._Friendship_createdAt(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "userID": + + out.Values[i] = ec._Friendship_userID(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "friendID": + + out.Values[i] = ec._Friendship_friendID(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "user": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Friendship_user(ctx, field, obj) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + return res + } + + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + case "friend": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Friendship_friend(ctx, field, obj) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + return res + } + + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch() + if invalids > 0 { + return graphql.Null + } + return out +} + var groupImplementors = []string{"Group", "Node"} func (ec *executionContext) _Group(ctx context.Context, sel ast.SelectionSet, obj *ent.Group) graphql.Marshaler { @@ -8527,6 +9366,23 @@ func (ec *executionContext) _User(ctx context.Context, sel ast.SelectionSet, obj return res } + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + case "friendships": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._User_friendships(ctx, field, obj) + return res + } + out.Concurrently(i, func() graphql.Marshaler { return innerFunc(ctx) @@ -9012,6 +9868,21 @@ func (ec *executionContext) marshalNDuration2timeᚐDuration(ctx context.Context return res } +func (ec *executionContext) marshalNFriendship2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendship(ctx context.Context, sel ast.SelectionSet, v *ent.Friendship) graphql.Marshaler { + if v == nil { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + return graphql.Null + } + return ec._Friendship(ctx, sel, v) +} + +func (ec *executionContext) unmarshalNFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendshipWhereInput(ctx context.Context, v interface{}) (*ent.FriendshipWhereInput, error) { + res, err := ec.unmarshalInputFriendshipWhereInput(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) marshalNGroupConnection2entgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐGroupConnection(ctx context.Context, sel ast.SelectionSet, v ent.GroupConnection) graphql.Marshaler { return ec._GroupConnection(ctx, sel, &v) } @@ -9790,6 +10661,81 @@ func (ec *executionContext) marshalODuration2ᚖtimeᚐDuration(ctx context.Cont return res } +func (ec *executionContext) marshalOFriendship2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendshipᚄ(ctx context.Context, sel ast.SelectionSet, v []*ent.Friendship) graphql.Marshaler { + if v == nil { + return graphql.Null + } + ret := make(graphql.Array, len(v)) + var wg sync.WaitGroup + isLen1 := len(v) == 1 + if !isLen1 { + wg.Add(len(v)) + } + for i := range v { + i := i + fc := &graphql.FieldContext{ + Index: &i, + Result: &v[i], + } + ctx := graphql.WithFieldContext(ctx, fc) + f := func(i int) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = nil + } + }() + if !isLen1 { + defer wg.Done() + } + ret[i] = ec.marshalNFriendship2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendship(ctx, sel, v[i]) + } + if isLen1 { + f(i) + } else { + go f(i) + } + + } + wg.Wait() + + for _, e := range ret { + if e == graphql.Null { + return graphql.Null + } + } + + return ret +} + +func (ec *executionContext) unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendshipWhereInputᚄ(ctx context.Context, v interface{}) ([]*ent.FriendshipWhereInput, error) { + if v == nil { + return nil, nil + } + var vSlice []interface{} + if v != nil { + vSlice = graphql.CoerceList(v) + } + var err error + res := make([]*ent.FriendshipWhereInput, len(vSlice)) + for i := range vSlice { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithIndex(i)) + res[i], err = ec.unmarshalNFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendshipWhereInput(ctx, vSlice[i]) + if err != nil { + return nil, err + } + } + return res, nil +} + +func (ec *executionContext) unmarshalOFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐFriendshipWhereInput(ctx context.Context, v interface{}) (*ent.FriendshipWhereInput, error) { + if v == nil { + return nil, nil + } + res, err := ec.unmarshalInputFriendshipWhereInput(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) marshalOGroup2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodoᚋentᚐGroup(ctx context.Context, sel ast.SelectionSet, v *ent.Group) graphql.Marshaler { if v == nil { return graphql.Null diff --git a/entgql/internal/todogotype/ent/gql_collection.go b/entgql/internal/todogotype/ent/gql_collection.go index 11594445d..4b2ed1ff4 100644 --- a/entgql/internal/todogotype/ent/gql_collection.go +++ b/entgql/internal/todogotype/ent/gql_collection.go @@ -199,6 +199,74 @@ func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { return args } +// CollectFields tells the query-builder to eagerly load connected nodes by resolver context. +func (f *FriendshipQuery) CollectFields(ctx context.Context, satisfies ...string) (*FriendshipQuery, error) { + fc := graphql.GetFieldContext(ctx) + if fc == nil { + return f, nil + } + if err := f.collectField(ctx, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil { + return nil, err + } + return f, nil +} + +func (f *FriendshipQuery) collectField(ctx context.Context, op *graphql.OperationContext, field graphql.CollectedField, path []string, satisfies ...string) error { + path = append([]string(nil), path...) + for _, field := range graphql.CollectFields(op, field.Selections, satisfies) { + switch field.Name { + case "user": + var ( + path = append(path, field.Name) + query = &UserQuery{config: f.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + f.withUser = query + case "friend": + var ( + path = append(path, field.Name) + query = &UserQuery{config: f.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + f.withFriend = query + } + } + return nil +} + +type friendshipPaginateArgs struct { + first, last *int + after, before *Cursor + opts []FriendshipPaginateOption +} + +func newFriendshipPaginateArgs(rv map[string]interface{}) *friendshipPaginateArgs { + args := &friendshipPaginateArgs{} + if rv == nil { + return args + } + if v := rv[firstField]; v != nil { + args.first = v.(*int) + } + if v := rv[lastField]; v != nil { + args.last = v.(*int) + } + if v := rv[afterField]; v != nil { + args.after = v.(*Cursor) + } + if v := rv[beforeField]; v != nil { + args.before = v.(*Cursor) + } + if v, ok := rv[whereField].(*FriendshipWhereInput); ok { + args.opts = append(args.opts, WithFriendshipFilter(v.Filter)) + } + return args +} + // CollectFields tells the query-builder to eagerly load connected nodes by resolver context. func (gr *GroupQuery) CollectFields(ctx context.Context, satisfies ...string) (*GroupQuery, error) { fc := graphql.GetFieldContext(ctx) @@ -717,6 +785,15 @@ func (u *UserQuery) collectField(ctx context.Context, op *graphql.OperationConte return err } u.withFriends = query + case "friendships": + var ( + path = append(path, field.Name) + query = &FriendshipQuery{config: u.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + u.withFriendships = query } } return nil diff --git a/entgql/internal/todogotype/ent/gql_edge.go b/entgql/internal/todogotype/ent/gql_edge.go index c22eed8c1..a30781f2d 100644 --- a/entgql/internal/todogotype/ent/gql_edge.go +++ b/entgql/internal/todogotype/ent/gql_edge.go @@ -94,6 +94,22 @@ func (c *Category) Todos( return conn, nil } +func (f *Friendship) User(ctx context.Context) (*User, error) { + result, err := f.Edges.UserOrErr() + if IsNotLoaded(err) { + result, err = f.QueryUser().Only(ctx) + } + return result, err +} + +func (f *Friendship) Friend(ctx context.Context) (*User, error) { + result, err := f.Edges.FriendOrErr() + if IsNotLoaded(err) { + result, err = f.QueryFriend().Only(ctx) + } + return result, err +} + func (gr *Group) Users( ctx context.Context, after *Cursor, first *int, before *Cursor, last *int, where *UserWhereInput, ) (*UserConnection, error) { @@ -331,3 +347,11 @@ func (u *User) Friends(ctx context.Context) ([]*User, error) { } return result, err } + +func (u *User) Friendships(ctx context.Context) ([]*Friendship, error) { + result, err := u.Edges.FriendshipsOrErr() + if IsNotLoaded(err) { + result, err = u.QueryFriendships().All(ctx) + } + return result, err +} diff --git a/entgql/internal/todogotype/ent/gql_node.go b/entgql/internal/todogotype/ent/gql_node.go index 84865889c..e014c34a1 100644 --- a/entgql/internal/todogotype/ent/gql_node.go +++ b/entgql/internal/todogotype/ent/gql_node.go @@ -24,6 +24,7 @@ import ( "entgo.io/contrib/entgql" "entgo.io/contrib/entgql/internal/todogotype/ent/category" + "entgo.io/contrib/entgql/internal/todogotype/ent/friendship" "entgo.io/contrib/entgql/internal/todogotype/ent/group" "entgo.io/contrib/entgql/internal/todogotype/ent/pet" "entgo.io/contrib/entgql/internal/todogotype/ent/schema/bigintgql" @@ -136,6 +137,61 @@ func (c *Category) Node(ctx context.Context) (node *Node, err error) { return node, nil } +func (f *Friendship) Node(ctx context.Context) (node *Node, err error) { + node = &Node{ + ID: f.ID, + Type: "Friendship", + Fields: make([]*Field, 3), + Edges: make([]*Edge, 2), + } + var buf []byte + if buf, err = json.Marshal(f.CreatedAt); err != nil { + return nil, err + } + node.Fields[0] = &Field{ + Type: "time.Time", + Name: "created_at", + Value: string(buf), + } + if buf, err = json.Marshal(f.UserID); err != nil { + return nil, err + } + node.Fields[1] = &Field{ + Type: "string", + Name: "user_id", + Value: string(buf), + } + if buf, err = json.Marshal(f.FriendID); err != nil { + return nil, err + } + node.Fields[2] = &Field{ + Type: "string", + Name: "friend_id", + Value: string(buf), + } + node.Edges[0] = &Edge{ + Type: "User", + Name: "user", + } + err = f.QueryUser(). + Select(user.FieldID). + Scan(ctx, &node.Edges[0].IDs) + if err != nil { + return nil, err + } + node.Edges[1] = &Edge{ + Type: "User", + Name: "friend", + } + err = f.QueryFriend(). + Select(user.FieldID). + Scan(ctx, &node.Edges[1].IDs) + if err != nil { + return nil, err + } + return node, nil +} + func (gr *Group) Node(ctx context.Context) (node *Node, err error) { node = &Node{ ID: gr.ID, @@ -276,7 +332,7 @@ func (u *User) Node(ctx context.Context) (node *Node, err error) { ID: u.ID, Type: "User", Fields: make([]*Field, 1), - Edges: make([]*Edge, 2), + Edges: make([]*Edge, 3), } var buf []byte if buf, err = json.Marshal(u.Name); err != nil { @@ -307,6 +363,16 @@ func (u *User) Node(ctx context.Context) (node *Node, err error) { if err != nil { return nil, err } + node.Edges[2] = &Edge{ + Type: "Friendship", + Name: "friendships", + } + err = u.QueryFriendships(). + Select(friendship.FieldID). + Scan(ctx, &node.Edges[2].IDs) + if err != nil { + return nil, err + } return node, nil } @@ -393,6 +459,18 @@ func (c *Client) noder(ctx context.Context, table string, id string) (Noder, err return nil, err } return n, nil + case friendship.Table: + query := c.Friendship.Query(). + Where(friendship.ID(id)) + query, err := query.CollectFields(ctx, "Friendship") + if err != nil { + return nil, err + } + n, err := query.Only(ctx) + if err != nil { + return nil, err + } + return n, nil case group.Table: query := c.Group.Query(). Where(group.ID(id)) @@ -540,6 +618,22 @@ func (c *Client) noders(ctx context.Context, table string, ids []string) ([]Node *noder = node } } + case friendship.Table: + query := c.Friendship.Query(). + Where(friendship.IDIn(ids...)) + query, err := query.CollectFields(ctx, "Friendship") + if err != nil { + return nil, err + } + nodes, err := query.All(ctx) + if err != nil { + return nil, err + } + for _, node := range nodes { + for _, noder := range idmap[node.ID] { + *noder = node + } + } case group.Table: query := c.Group.Query(). Where(group.IDIn(ids...)) diff --git a/entgql/internal/todogotype/ent/gql_pagination.go b/entgql/internal/todogotype/ent/gql_pagination.go index e7e24ce4e..9eb2819e8 100644 --- a/entgql/internal/todogotype/ent/gql_pagination.go +++ b/entgql/internal/todogotype/ent/gql_pagination.go @@ -26,6 +26,7 @@ import ( "strings" "entgo.io/contrib/entgql/internal/todogotype/ent/category" + "entgo.io/contrib/entgql/internal/todogotype/ent/friendship" "entgo.io/contrib/entgql/internal/todogotype/ent/group" "entgo.io/contrib/entgql/internal/todogotype/ent/pet" "entgo.io/contrib/entgql/internal/todogotype/ent/todo" @@ -551,6 +552,241 @@ func (c *Category) ToEdge(order *CategoryOrder) *CategoryEdge { } } +// FriendshipEdge is the edge representation of Friendship. +type FriendshipEdge struct { + Node *Friendship `json:"node"` + Cursor Cursor `json:"cursor"` +} + +// FriendshipConnection is the connection containing edges to Friendship. +type FriendshipConnection struct { + Edges []*FriendshipEdge `json:"edges"` + PageInfo PageInfo `json:"pageInfo"` + TotalCount int `json:"totalCount"` +} + +func (c *FriendshipConnection) build(nodes []*Friendship, pager *friendshipPager, after *Cursor, first *int, before *Cursor, last *int) { + c.PageInfo.HasNextPage = before != nil + c.PageInfo.HasPreviousPage = after != nil + if first != nil && *first+1 == len(nodes) { + c.PageInfo.HasNextPage = true + nodes = nodes[:len(nodes)-1] + } else if last != nil && *last+1 == len(nodes) { + c.PageInfo.HasPreviousPage = true + nodes = nodes[:len(nodes)-1] + } + var nodeAt func(int) *Friendship + if last != nil { + n := len(nodes) - 1 + nodeAt = func(i int) *Friendship { + return nodes[n-i] + } + } else { + nodeAt = func(i int) *Friendship { + return nodes[i] + } + } + c.Edges = make([]*FriendshipEdge, len(nodes)) + for i := range nodes { + node := nodeAt(i) + c.Edges[i] = &FriendshipEdge{ + Node: node, + Cursor: pager.toCursor(node), + } + } + if l := len(c.Edges); l > 0 { + c.PageInfo.StartCursor = &c.Edges[0].Cursor + c.PageInfo.EndCursor = &c.Edges[l-1].Cursor + } + if c.TotalCount == 0 { + c.TotalCount = len(nodes) + } +} + +// FriendshipPaginateOption enables pagination customization. +type FriendshipPaginateOption func(*friendshipPager) error + +// WithFriendshipOrder configures pagination ordering. +func WithFriendshipOrder(order *FriendshipOrder) FriendshipPaginateOption { + if order == nil { + order = DefaultFriendshipOrder + } + o := *order + return func(pager *friendshipPager) error { + if err := o.Direction.Validate(); err != nil { + return err + } + if o.Field == nil { + o.Field = DefaultFriendshipOrder.Field + } + pager.order = &o + return nil + } +} + +// WithFriendshipFilter configures pagination filter. +func WithFriendshipFilter(filter func(*FriendshipQuery) (*FriendshipQuery, error)) FriendshipPaginateOption { + return func(pager *friendshipPager) error { + if filter == nil { + return errors.New("FriendshipQuery filter cannot be nil") + } + pager.filter = filter + return nil + } +} + +type friendshipPager struct { + order *FriendshipOrder + filter func(*FriendshipQuery) (*FriendshipQuery, error) +} + +func newFriendshipPager(opts []FriendshipPaginateOption) (*friendshipPager, error) { + pager := &friendshipPager{} + for _, opt := range opts { + if err := opt(pager); err != nil { + return nil, err + } + } + if pager.order == nil { + pager.order = DefaultFriendshipOrder + } + return pager, nil +} + +func (p *friendshipPager) applyFilter(query *FriendshipQuery) (*FriendshipQuery, error) { + if p.filter != nil { + return p.filter(query) + } + return query, nil +} + +func (p *friendshipPager) toCursor(f *Friendship) Cursor { + return p.order.Field.toCursor(f) +} + +func (p *friendshipPager) applyCursors(query *FriendshipQuery, after, before *Cursor) *FriendshipQuery { + for _, predicate := range cursorsToPredicates( + p.order.Direction, after, before, + p.order.Field.field, DefaultFriendshipOrder.Field.field, + ) { + query = query.Where(predicate) + } + return query +} + +func (p *friendshipPager) applyOrder(query *FriendshipQuery, reverse bool) *FriendshipQuery { + direction := p.order.Direction + if reverse { + direction = direction.reverse() + } + query = query.Order(direction.orderFunc(p.order.Field.field)) + if p.order.Field != DefaultFriendshipOrder.Field { + query = query.Order(direction.orderFunc(DefaultFriendshipOrder.Field.field)) + } + return query +} + +func (p *friendshipPager) orderExpr(reverse bool) sql.Querier { + direction := p.order.Direction + if reverse { + direction = direction.reverse() + } + return sql.ExprFunc(func(b *sql.Builder) { + b.Ident(p.order.Field.field).Pad().WriteString(string(direction)) + if p.order.Field != DefaultFriendshipOrder.Field { + b.Comma().Ident(DefaultFriendshipOrder.Field.field).Pad().WriteString(string(direction)) + } + }) +} + +// Paginate executes the query and returns a relay based cursor connection to Friendship. +func (f *FriendshipQuery) Paginate( + ctx context.Context, after *Cursor, first *int, + before *Cursor, last *int, opts ...FriendshipPaginateOption, +) (*FriendshipConnection, error) { + if err := validateFirstLast(first, last); err != nil { + return nil, err + } + pager, err := newFriendshipPager(opts) + if err != nil { + return nil, err + } + if f, err = pager.applyFilter(f); err != nil { + return nil, err + } + conn := &FriendshipConnection{Edges: []*FriendshipEdge{}} + if !hasCollectedField(ctx, edgesField) || first != nil && *first == 0 || last != nil && *last == 0 { + if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) { + if conn.TotalCount, err = f.Count(ctx); err != nil { + return nil, err + } + conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0 + conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0 + } + return conn, nil + } + + if (after != nil || first != nil || before != nil || last != nil) && hasCollectedField(ctx, totalCountField) { + count, err := f.Clone().Count(ctx) + if err != nil { + return nil, err + } + conn.TotalCount = count + } + + f = pager.applyCursors(f, after, before) + f = pager.applyOrder(f, last != nil) + if limit := paginateLimit(first, last); limit != 0 { + f.Limit(limit) + } + if field := collectedField(ctx, edgesField, nodeField); field != nil { + if err := f.collectField(ctx, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil { + return nil, err + } + } + + nodes, err := f.All(ctx) + if err != nil || len(nodes) == 0 { + return conn, err + } + conn.build(nodes, pager, after, first, before, last) + return conn, nil +} + +// FriendshipOrderField defines the ordering field of Friendship. +type FriendshipOrderField struct { + field string + toCursor func(*Friendship) Cursor +} + +// FriendshipOrder defines the ordering of Friendship. +type FriendshipOrder struct { + Direction OrderDirection `json:"direction"` + Field *FriendshipOrderField `json:"field"` +} + +// DefaultFriendshipOrder is the default ordering of Friendship. +var DefaultFriendshipOrder = &FriendshipOrder{ + Direction: OrderDirectionAsc, + Field: &FriendshipOrderField{ + field: friendship.FieldID, + toCursor: func(f *Friendship) Cursor { + return Cursor{ID: f.ID} + }, + }, +} + +// ToEdge converts Friendship into FriendshipEdge. +func (f *Friendship) ToEdge(order *FriendshipOrder) *FriendshipEdge { + if order == nil { + order = DefaultFriendshipOrder + } + return &FriendshipEdge{ + Node: f, + Cursor: order.Field.toCursor(f), + } +} + // GroupEdge is the edge representation of Group. type GroupEdge struct { Node *Group `json:"node"` diff --git a/entgql/internal/todogotype/ent/gql_where_input.go b/entgql/internal/todogotype/ent/gql_where_input.go index e83c4b5ae..6e8b76a64 100644 --- a/entgql/internal/todogotype/ent/gql_where_input.go +++ b/entgql/internal/todogotype/ent/gql_where_input.go @@ -23,6 +23,7 @@ import ( "entgo.io/contrib/entgql/internal/todo/ent/schema/schematype" "entgo.io/contrib/entgql/internal/todogotype/ent/category" + "entgo.io/contrib/entgql/internal/todogotype/ent/friendship" "entgo.io/contrib/entgql/internal/todogotype/ent/group" "entgo.io/contrib/entgql/internal/todogotype/ent/pet" "entgo.io/contrib/entgql/internal/todogotype/ent/predicate" @@ -376,6 +377,316 @@ func (i *CategoryWhereInput) P() (predicate.Category, error) { } } +// FriendshipWhereInput represents a where input for filtering Friendship queries. +type FriendshipWhereInput struct { + Predicates []predicate.Friendship `json:"-"` + Not *FriendshipWhereInput `json:"not,omitempty"` + Or []*FriendshipWhereInput `json:"or,omitempty"` + And []*FriendshipWhereInput `json:"and,omitempty"` + + // "id" field predicates. + ID *string `json:"id,omitempty"` + IDNEQ *string `json:"idNEQ,omitempty"` + IDIn []string `json:"idIn,omitempty"` + IDNotIn []string `json:"idNotIn,omitempty"` + IDGT *string `json:"idGT,omitempty"` + IDGTE *string `json:"idGTE,omitempty"` + IDLT *string `json:"idLT,omitempty"` + IDLTE *string `json:"idLTE,omitempty"` + + // "created_at" field predicates. + CreatedAt *time.Time `json:"createdAt,omitempty"` + CreatedAtNEQ *time.Time `json:"createdAtNEQ,omitempty"` + CreatedAtIn []time.Time `json:"createdAtIn,omitempty"` + CreatedAtNotIn []time.Time `json:"createdAtNotIn,omitempty"` + CreatedAtGT *time.Time `json:"createdAtGT,omitempty"` + CreatedAtGTE *time.Time `json:"createdAtGTE,omitempty"` + CreatedAtLT *time.Time `json:"createdAtLT,omitempty"` + CreatedAtLTE *time.Time `json:"createdAtLTE,omitempty"` + + // "user_id" field predicates. + UserID *string `json:"userID,omitempty"` + UserIDNEQ *string `json:"userIDNEQ,omitempty"` + UserIDIn []string `json:"userIDIn,omitempty"` + UserIDNotIn []string `json:"userIDNotIn,omitempty"` + UserIDGT *string `json:"userIDGT,omitempty"` + UserIDGTE *string `json:"userIDGTE,omitempty"` + UserIDLT *string `json:"userIDLT,omitempty"` + UserIDLTE *string `json:"userIDLTE,omitempty"` + UserIDContains *string `json:"userIDContains,omitempty"` + UserIDHasPrefix *string `json:"userIDHasPrefix,omitempty"` + UserIDHasSuffix *string `json:"userIDHasSuffix,omitempty"` + UserIDEqualFold *string `json:"userIDEqualFold,omitempty"` + UserIDContainsFold *string `json:"userIDContainsFold,omitempty"` + + // "friend_id" field predicates. + FriendID *string `json:"friendID,omitempty"` + FriendIDNEQ *string `json:"friendIDNEQ,omitempty"` + FriendIDIn []string `json:"friendIDIn,omitempty"` + FriendIDNotIn []string `json:"friendIDNotIn,omitempty"` + FriendIDGT *string `json:"friendIDGT,omitempty"` + FriendIDGTE *string `json:"friendIDGTE,omitempty"` + FriendIDLT *string `json:"friendIDLT,omitempty"` + FriendIDLTE *string `json:"friendIDLTE,omitempty"` + FriendIDContains *string `json:"friendIDContains,omitempty"` + FriendIDHasPrefix *string `json:"friendIDHasPrefix,omitempty"` + FriendIDHasSuffix *string `json:"friendIDHasSuffix,omitempty"` + FriendIDEqualFold *string `json:"friendIDEqualFold,omitempty"` + FriendIDContainsFold *string `json:"friendIDContainsFold,omitempty"` + + // "user" edge predicates. + HasUser *bool `json:"hasUser,omitempty"` + HasUserWith []*UserWhereInput `json:"hasUserWith,omitempty"` + + // "friend" edge predicates. + HasFriend *bool `json:"hasFriend,omitempty"` + HasFriendWith []*UserWhereInput `json:"hasFriendWith,omitempty"` +} + +// AddPredicates adds custom predicates to the where input to be used during the filtering phase. +func (i *FriendshipWhereInput) AddPredicates(predicates ...predicate.Friendship) { + i.Predicates = append(i.Predicates, predicates...) +} + +// Filter applies the FriendshipWhereInput filter on the FriendshipQuery builder. +func (i *FriendshipWhereInput) Filter(q *FriendshipQuery) (*FriendshipQuery, error) { + if i == nil { + return q, nil + } + p, err := i.P() + if err != nil { + if err == ErrEmptyFriendshipWhereInput { + return q, nil + } + return nil, err + } + return q.Where(p), nil +} + +// ErrEmptyFriendshipWhereInput is returned in case the FriendshipWhereInput is empty. +var ErrEmptyFriendshipWhereInput = errors.New("ent: empty predicate FriendshipWhereInput") + +// P returns a predicate for filtering friendships. +// An error is returned if the input is empty or invalid. +func (i *FriendshipWhereInput) P() (predicate.Friendship, error) { + var predicates []predicate.Friendship + if i.Not != nil { + p, err := i.Not.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'not'", err) + } + predicates = append(predicates, friendship.Not(p)) + } + switch n := len(i.Or); { + case n == 1: + p, err := i.Or[0].P() + if err != nil { + return nil, fmt.Errorf("%w: field 'or'", err) + } + predicates = append(predicates, p) + case n > 1: + or := make([]predicate.Friendship, 0, n) + for _, w := range i.Or { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'or'", err) + } + or = append(or, p) + } + predicates = append(predicates, friendship.Or(or...)) + } + switch n := len(i.And); { + case n == 1: + p, err := i.And[0].P() + if err != nil { + return nil, fmt.Errorf("%w: field 'and'", err) + } + predicates = append(predicates, p) + case n > 1: + and := make([]predicate.Friendship, 0, n) + for _, w := range i.And { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'and'", err) + } + and = append(and, p) + } + predicates = append(predicates, friendship.And(and...)) + } + predicates = append(predicates, i.Predicates...) + if i.ID != nil { + predicates = append(predicates, friendship.IDEQ(*i.ID)) + } + if i.IDNEQ != nil { + predicates = append(predicates, friendship.IDNEQ(*i.IDNEQ)) + } + if len(i.IDIn) > 0 { + predicates = append(predicates, friendship.IDIn(i.IDIn...)) + } + if len(i.IDNotIn) > 0 { + predicates = append(predicates, friendship.IDNotIn(i.IDNotIn...)) + } + if i.IDGT != nil { + predicates = append(predicates, friendship.IDGT(*i.IDGT)) + } + if i.IDGTE != nil { + predicates = append(predicates, friendship.IDGTE(*i.IDGTE)) + } + if i.IDLT != nil { + predicates = append(predicates, friendship.IDLT(*i.IDLT)) + } + if i.IDLTE != nil { + predicates = append(predicates, friendship.IDLTE(*i.IDLTE)) + } + if i.CreatedAt != nil { + predicates = append(predicates, friendship.CreatedAtEQ(*i.CreatedAt)) + } + if i.CreatedAtNEQ != nil { + predicates = append(predicates, friendship.CreatedAtNEQ(*i.CreatedAtNEQ)) + } + if len(i.CreatedAtIn) > 0 { + predicates = append(predicates, friendship.CreatedAtIn(i.CreatedAtIn...)) + } + if len(i.CreatedAtNotIn) > 0 { + predicates = append(predicates, friendship.CreatedAtNotIn(i.CreatedAtNotIn...)) + } + if i.CreatedAtGT != nil { + predicates = append(predicates, friendship.CreatedAtGT(*i.CreatedAtGT)) + } + if i.CreatedAtGTE != nil { + predicates = append(predicates, friendship.CreatedAtGTE(*i.CreatedAtGTE)) + } + if i.CreatedAtLT != nil { + predicates = append(predicates, friendship.CreatedAtLT(*i.CreatedAtLT)) + } + if i.CreatedAtLTE != nil { + predicates = append(predicates, friendship.CreatedAtLTE(*i.CreatedAtLTE)) + } + if i.UserID != nil { + predicates = append(predicates, friendship.UserIDEQ(*i.UserID)) + } + if i.UserIDNEQ != nil { + predicates = append(predicates, friendship.UserIDNEQ(*i.UserIDNEQ)) + } + if len(i.UserIDIn) > 0 { + predicates = append(predicates, friendship.UserIDIn(i.UserIDIn...)) + } + if len(i.UserIDNotIn) > 0 { + predicates = append(predicates, friendship.UserIDNotIn(i.UserIDNotIn...)) + } + if i.UserIDGT != nil { + predicates = append(predicates, friendship.UserIDGT(*i.UserIDGT)) + } + if i.UserIDGTE != nil { + predicates = append(predicates, friendship.UserIDGTE(*i.UserIDGTE)) + } + if i.UserIDLT != nil { + predicates = append(predicates, friendship.UserIDLT(*i.UserIDLT)) + } + if i.UserIDLTE != nil { + predicates = append(predicates, friendship.UserIDLTE(*i.UserIDLTE)) + } + if i.UserIDContains != nil { + predicates = append(predicates, friendship.UserIDContains(*i.UserIDContains)) + } + if i.UserIDHasPrefix != nil { + predicates = append(predicates, friendship.UserIDHasPrefix(*i.UserIDHasPrefix)) + } + if i.UserIDHasSuffix != nil { + predicates = append(predicates, friendship.UserIDHasSuffix(*i.UserIDHasSuffix)) + } + if i.UserIDEqualFold != nil { + predicates = append(predicates, friendship.UserIDEqualFold(*i.UserIDEqualFold)) + } + if i.UserIDContainsFold != nil { + predicates = append(predicates, friendship.UserIDContainsFold(*i.UserIDContainsFold)) + } + if i.FriendID != nil { + predicates = append(predicates, friendship.FriendIDEQ(*i.FriendID)) + } + if i.FriendIDNEQ != nil { + predicates = append(predicates, friendship.FriendIDNEQ(*i.FriendIDNEQ)) + } + if len(i.FriendIDIn) > 0 { + predicates = append(predicates, friendship.FriendIDIn(i.FriendIDIn...)) + } + if len(i.FriendIDNotIn) > 0 { + predicates = append(predicates, friendship.FriendIDNotIn(i.FriendIDNotIn...)) + } + if i.FriendIDGT != nil { + predicates = append(predicates, friendship.FriendIDGT(*i.FriendIDGT)) + } + if i.FriendIDGTE != nil { + predicates = append(predicates, friendship.FriendIDGTE(*i.FriendIDGTE)) + } + if i.FriendIDLT != nil { + predicates = append(predicates, friendship.FriendIDLT(*i.FriendIDLT)) + } + if i.FriendIDLTE != nil { + predicates = append(predicates, friendship.FriendIDLTE(*i.FriendIDLTE)) + } + if i.FriendIDContains != nil { + predicates = append(predicates, friendship.FriendIDContains(*i.FriendIDContains)) + } + if i.FriendIDHasPrefix != nil { + predicates = append(predicates, friendship.FriendIDHasPrefix(*i.FriendIDHasPrefix)) + } + if i.FriendIDHasSuffix != nil { + predicates = append(predicates, friendship.FriendIDHasSuffix(*i.FriendIDHasSuffix)) + } + if i.FriendIDEqualFold != nil { + predicates = append(predicates, friendship.FriendIDEqualFold(*i.FriendIDEqualFold)) + } + if i.FriendIDContainsFold != nil { + predicates = append(predicates, friendship.FriendIDContainsFold(*i.FriendIDContainsFold)) + } + + if i.HasUser != nil { + p := friendship.HasUser() + if !*i.HasUser { + p = friendship.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasUserWith) > 0 { + with := make([]predicate.User, 0, len(i.HasUserWith)) + for _, w := range i.HasUserWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasUserWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, friendship.HasUserWith(with...)) + } + if i.HasFriend != nil { + p := friendship.HasFriend() + if !*i.HasFriend { + p = friendship.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasFriendWith) > 0 { + with := make([]predicate.User, 0, len(i.HasFriendWith)) + for _, w := range i.HasFriendWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasFriendWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, friendship.HasFriendWith(with...)) + } + switch len(predicates) { + case 0: + return nil, ErrEmptyFriendshipWhereInput + case 1: + return predicates[0], nil + default: + return friendship.And(predicates...), nil + } +} + // GroupWhereInput represents a where input for filtering Group queries. type GroupWhereInput struct { Predicates []predicate.Group `json:"-"` @@ -1185,6 +1496,10 @@ type UserWhereInput struct { // "friends" edge predicates. HasFriends *bool `json:"hasFriends,omitempty"` HasFriendsWith []*UserWhereInput `json:"hasFriendsWith,omitempty"` + + // "friendships" edge predicates. + HasFriendships *bool `json:"hasFriendships,omitempty"` + HasFriendshipsWith []*FriendshipWhereInput `json:"hasFriendshipsWith,omitempty"` } // AddPredicates adds custom predicates to the where input to be used during the filtering phase. @@ -1358,6 +1673,24 @@ func (i *UserWhereInput) P() (predicate.User, error) { } predicates = append(predicates, user.HasFriendsWith(with...)) } + if i.HasFriendships != nil { + p := user.HasFriendships() + if !*i.HasFriendships { + p = user.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasFriendshipsWith) > 0 { + with := make([]predicate.Friendship, 0, len(i.HasFriendshipsWith)) + for _, w := range i.HasFriendshipsWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasFriendshipsWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, user.HasFriendshipsWith(with...)) + } switch len(predicates) { case 0: return nil, ErrEmptyUserWhereInput diff --git a/entgql/internal/todogotype/ent/user.go b/entgql/internal/todogotype/ent/user.go index 7b3886a9e..d6baeb60d 100644 --- a/entgql/internal/todogotype/ent/user.go +++ b/entgql/internal/todogotype/ent/user.go @@ -48,7 +48,7 @@ type UserEdges struct { // type was loaded (or requested) in eager-loading or not. loadedTypes [3]bool // totalCount holds the count of the edges above. - totalCount [2]*int + totalCount [3]*int } // GroupsOrErr returns the Groups value or an error if the edge diff --git a/entgql/internal/todogotype/generated.go b/entgql/internal/todogotype/generated.go index 528b930e2..4a53a74e0 100644 --- a/entgql/internal/todogotype/generated.go +++ b/entgql/internal/todogotype/generated.go @@ -68,6 +68,15 @@ type ComplexityRoot struct { MaxMembers func(childComplexity int) int } + Friendship struct { + CreatedAt func(childComplexity int) int + Friend func(childComplexity int) int + FriendID func(childComplexity int) int + ID func(childComplexity int) int + User func(childComplexity int) int + UserID func(childComplexity int) int + } + Group struct { ID func(childComplexity int) int Name func(childComplexity int) int @@ -130,10 +139,11 @@ type ComplexityRoot struct { } User struct { - Friends func(childComplexity int) int - Groups func(childComplexity int, after *ent.Cursor, first *int, before *ent.Cursor, last *int, where *ent.GroupWhereInput) int - ID func(childComplexity int) int - Name func(childComplexity int) int + Friends func(childComplexity int) int + Friendships func(childComplexity int) int + Groups func(childComplexity int, after *ent.Cursor, first *int, before *ent.Cursor, last *int, where *ent.GroupWhereInput) int + ID func(childComplexity int) int + Name func(childComplexity int) int } UserConnection struct { @@ -259,6 +269,48 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.CategoryConfig.MaxMembers(childComplexity), true + case "Friendship.createdAt": + if e.complexity.Friendship.CreatedAt == nil { + break + } + + return e.complexity.Friendship.CreatedAt(childComplexity), true + + case "Friendship.friend": + if e.complexity.Friendship.Friend == nil { + break + } + + return e.complexity.Friendship.Friend(childComplexity), true + + case "Friendship.friendID": + if e.complexity.Friendship.FriendID == nil { + break + } + + return e.complexity.Friendship.FriendID(childComplexity), true + + case "Friendship.id": + if e.complexity.Friendship.ID == nil { + break + } + + return e.complexity.Friendship.ID(childComplexity), true + + case "Friendship.user": + if e.complexity.Friendship.User == nil { + break + } + + return e.complexity.Friendship.User(childComplexity), true + + case "Friendship.userID": + if e.complexity.Friendship.UserID == nil { + break + } + + return e.complexity.Friendship.UserID(childComplexity), true + case "Group.id": if e.complexity.Group.ID == nil { break @@ -544,6 +596,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.User.Friends(childComplexity), true + case "User.friendships": + if e.complexity.User.Friendships == nil { + break + } + + return e.complexity.User.Friendships(childComplexity), true + case "User.groups": if e.complexity.User.Groups == nil { break @@ -617,9 +676,12 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler { ec.unmarshalInputCategoryOrder, ec.unmarshalInputCategoryWhereInput, ec.unmarshalInputCreateTodoInput, + ec.unmarshalInputCreateUserInput, + ec.unmarshalInputFriendshipWhereInput, ec.unmarshalInputGroupWhereInput, ec.unmarshalInputTodoOrder, ec.unmarshalInputTodoWhereInput, + ec.unmarshalInputUpdateUserInput, ec.unmarshalInputUserWhereInput, ) first := true @@ -843,10 +905,54 @@ input CreateTodoInput { secretID: ID } """ +CreateUserInput is used for create User object. +Input was generated by ent. +""" +input CreateUserInput { + name: String + groupIDs: [ID!] + friendIDs: [ID!] +} +""" Define a Relay Cursor type: https://relay.dev/graphql/connections.htm#sec-Cursor """ scalar Cursor +type Friendship implements Node { + id: ID! + createdAt: Time! + userID: ID! + friendID: ID! + user: User! + friend: User! +} +""" +FriendshipWhereInput is used for filtering Friendship objects. +Input was generated by ent. +""" +input FriendshipWhereInput { + not: FriendshipWhereInput + and: [FriendshipWhereInput!] + or: [FriendshipWhereInput!] + """id field predicates""" + id: ID + idNEQ: ID + idIn: [ID!] + idNotIn: [ID!] + idGT: ID + idGTE: ID + idLT: ID + idLTE: ID + """created_at field predicates""" + createdAt: Time + createdAtNEQ: Time + createdAtIn: [Time!] + createdAtNotIn: [Time!] + createdAtGT: Time + createdAtGTE: Time + createdAtLT: Time + createdAtLTE: Time +} type Group implements Node { id: ID! name: String! @@ -1145,6 +1251,17 @@ input TodoWhereInput { hasCategory: Boolean hasCategoryWith: [CategoryWhereInput!] } +""" +UpdateUserInput is used for update User object. +Input was generated by ent. +""" +input UpdateUserInput { + name: String + addGroupIDs: [ID!] + removeGroupIDs: [ID!] + addFriendIDs: [ID!] + removeFriendIDs: [ID!] +} type User implements Node { id: ID! name: String! @@ -1165,6 +1282,7 @@ type User implements Node { where: GroupWhereInput ): GroupConnection! friends: [User!] + friendships: [Friendship!] } """A connection to a list of items.""" type UserConnection { @@ -1219,6 +1337,9 @@ input UserWhereInput { """friends edge predicates""" hasFriends: Boolean hasFriendsWith: [UserWhereInput!] + """friendships edge predicates""" + hasFriendships: Boolean + hasFriendshipsWith: [FriendshipWhereInput!] } `, BuiltIn: false}, } @@ -2073,8 +2194,281 @@ func (ec *executionContext) fieldContext_Category_todos(ctx context.Context, fie return fc, nil } -func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField, obj *schematype.CategoryConfig) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_CategoryConfig_maxMembers(ctx, field) +func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField, obj *schematype.CategoryConfig) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_CategoryConfig_maxMembers(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.MaxMembers, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(int) + fc.Result = res + return ec.marshalOInt2int(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "CategoryConfig", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Int does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_id(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_id(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.ID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(string) + fc.Result = res + return ec.marshalNID2string(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_id(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_createdAt(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_createdAt(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.CreatedAt, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(time.Time) + fc.Result = res + return ec.marshalNTime2timeᚐTime(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_createdAt(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Time does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_userID(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_userID(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.UserID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(string) + fc.Result = res + return ec.marshalNID2string(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_userID(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_friendID(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_friendID(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.FriendID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(string) + fc.Result = res + return ec.marshalNID2string(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_friendID(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_user(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_user(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.User(ctx) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(*ent.User) + fc.Result = res + return ec.marshalNUser2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐUser(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_user(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: true, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "id": + return ec.fieldContext_User_id(ctx, field) + case "name": + return ec.fieldContext_User_name(ctx, field) + case "groups": + return ec.fieldContext_User_groups(ctx, field) + case "friends": + return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type User", field.Name) + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_friend(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_friend(ctx, field) if err != nil { return graphql.Null } @@ -2087,28 +2481,43 @@ func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, fiel }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.MaxMembers, nil + return obj.Friend(ctx) }) if err != nil { ec.Error(ctx, err) return graphql.Null } if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } return graphql.Null } - res := resTmp.(int) + res := resTmp.(*ent.User) fc.Result = res - return ec.marshalOInt2int(ctx, field.Selections, res) + return ec.marshalNUser2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐUser(ctx, field.Selections, res) } -func (ec *executionContext) fieldContext_CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { +func (ec *executionContext) fieldContext_Friendship_friend(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { fc = &graphql.FieldContext{ - Object: "CategoryConfig", + Object: "Friendship", Field: field, - IsMethod: false, + IsMethod: true, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type Int does not have child fields") + switch field.Name { + case "id": + return ec.fieldContext_User_id(ctx, field) + case "name": + return ec.fieldContext_User_name(ctx, field) + case "groups": + return ec.fieldContext_User_groups(ctx, field) + case "friends": + return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, } return fc, nil @@ -4150,6 +4559,8 @@ func (ec *executionContext) fieldContext_User_friends(ctx context.Context, field return ec.fieldContext_User_groups(ctx, field) case "friends": return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, @@ -4157,6 +4568,61 @@ func (ec *executionContext) fieldContext_User_friends(ctx context.Context, field return fc, nil } +func (ec *executionContext) _User_friendships(ctx context.Context, field graphql.CollectedField, obj *ent.User) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_User_friendships(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Friendships(ctx) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]*ent.Friendship) + fc.Result = res + return ec.marshalOFriendship2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendshipᚄ(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_User_friendships(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "User", + Field: field, + IsMethod: true, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "id": + return ec.fieldContext_Friendship_id(ctx, field) + case "createdAt": + return ec.fieldContext_Friendship_createdAt(ctx, field) + case "userID": + return ec.fieldContext_Friendship_userID(ctx, field) + case "friendID": + return ec.fieldContext_Friendship_friendID(ctx, field) + case "user": + return ec.fieldContext_Friendship_user(ctx, field) + case "friend": + return ec.fieldContext_Friendship_friend(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type Friendship", field.Name) + }, + } + return fc, nil +} + func (ec *executionContext) _UserConnection_edges(ctx context.Context, field graphql.CollectedField, obj *ent.UserConnection) (ret graphql.Marshaler) { fc, err := ec.fieldContext_UserConnection_edges(ctx, field) if err != nil { @@ -4346,6 +4812,8 @@ func (ec *executionContext) fieldContext_UserEdge_node(ctx context.Context, fiel return ec.fieldContext_User_groups(ctx, field) case "friends": return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, @@ -6751,43 +7219,249 @@ func (ec *executionContext) unmarshalInputCreateTodoInput(ctx context.Context, o if err != nil { return it, err } - case "text": + case "text": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("text")) + it.Text, err = ec.unmarshalNString2string(ctx, v) + if err != nil { + return it, err + } + case "parentID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("parentID")) + it.ParentID, err = ec.unmarshalOID2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "childIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("childIDs")) + it.ChildIDs, err = ec.unmarshalOID2ᚕstringᚄ(ctx, v) + if err != nil { + return it, err + } + case "categoryID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("categoryID")) + it.CategoryID, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚋschemaᚋbigintgqlᚐBigInt(ctx, v) + if err != nil { + return it, err + } + case "secretID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("secretID")) + it.SecretID, err = ec.unmarshalOID2ᚖstring(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + +func (ec *executionContext) unmarshalInputCreateUserInput(ctx context.Context, obj interface{}) (CreateUserInput, error) { + var it CreateUserInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "name": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name")) + it.Name, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "groupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("groupIDs")) + it.GroupIDs, err = ec.unmarshalOID2ᚕstringᚄ(ctx, v) + if err != nil { + return it, err + } + case "friendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("friendIDs")) + it.FriendIDs, err = ec.unmarshalOID2ᚕstringᚄ(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + +func (ec *executionContext) unmarshalInputFriendshipWhereInput(ctx context.Context, obj interface{}) (ent.FriendshipWhereInput, error) { + var it ent.FriendshipWhereInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "not": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("not")) + it.Not, err = ec.unmarshalOFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendshipWhereInput(ctx, v) + if err != nil { + return it, err + } + case "and": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("and")) + it.And, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } + case "or": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("or")) + it.Or, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } + case "id": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("id")) + it.ID, err = ec.unmarshalOID2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "idNEQ": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idNEQ")) + it.IDNEQ, err = ec.unmarshalOID2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "idIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idIn")) + it.IDIn, err = ec.unmarshalOID2ᚕstringᚄ(ctx, v) + if err != nil { + return it, err + } + case "idNotIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idNotIn")) + it.IDNotIn, err = ec.unmarshalOID2ᚕstringᚄ(ctx, v) + if err != nil { + return it, err + } + case "idGT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idGT")) + it.IDGT, err = ec.unmarshalOID2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "idGTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idGTE")) + it.IDGTE, err = ec.unmarshalOID2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "idLT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idLT")) + it.IDLT, err = ec.unmarshalOID2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "idLTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idLTE")) + it.IDLTE, err = ec.unmarshalOID2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "createdAt": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAt")) + it.CreatedAt, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + case "createdAtNEQ": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtNEQ")) + it.CreatedAtNEQ, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + case "createdAtIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtIn")) + it.CreatedAtIn, err = ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) + if err != nil { + return it, err + } + case "createdAtNotIn": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("text")) - it.Text, err = ec.unmarshalNString2string(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtNotIn")) + it.CreatedAtNotIn, err = ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) if err != nil { return it, err } - case "parentID": + case "createdAtGT": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("parentID")) - it.ParentID, err = ec.unmarshalOID2ᚖstring(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtGT")) + it.CreatedAtGT, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "childIDs": + case "createdAtGTE": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("childIDs")) - it.ChildIDs, err = ec.unmarshalOID2ᚕstringᚄ(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtGTE")) + it.CreatedAtGTE, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "categoryID": + case "createdAtLT": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("categoryID")) - it.CategoryID, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚋschemaᚋbigintgqlᚐBigInt(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtLT")) + it.CreatedAtLT, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "secretID": + case "createdAtLTE": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("secretID")) - it.SecretID, err = ec.unmarshalOID2ᚖstring(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtLTE")) + it.CreatedAtLTE, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } @@ -7541,6 +8215,61 @@ func (ec *executionContext) unmarshalInputTodoWhereInput(ctx context.Context, ob return it, nil } +func (ec *executionContext) unmarshalInputUpdateUserInput(ctx context.Context, obj interface{}) (UpdateUserInput, error) { + var it UpdateUserInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "name": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name")) + it.Name, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "addGroupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("addGroupIDs")) + it.AddGroupIDs, err = ec.unmarshalOID2ᚕstringᚄ(ctx, v) + if err != nil { + return it, err + } + case "removeGroupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("removeGroupIDs")) + it.RemoveGroupIDs, err = ec.unmarshalOID2ᚕstringᚄ(ctx, v) + if err != nil { + return it, err + } + case "addFriendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("addFriendIDs")) + it.AddFriendIDs, err = ec.unmarshalOID2ᚕstringᚄ(ctx, v) + if err != nil { + return it, err + } + case "removeFriendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("removeFriendIDs")) + it.RemoveFriendIDs, err = ec.unmarshalOID2ᚕstringᚄ(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + func (ec *executionContext) unmarshalInputUserWhereInput(ctx context.Context, obj interface{}) (ent.UserWhereInput, error) { var it ent.UserWhereInput asMap := map[string]interface{}{} @@ -7774,6 +8503,22 @@ func (ec *executionContext) unmarshalInputUserWhereInput(ctx context.Context, ob if err != nil { return it, err } + case "hasFriendships": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("hasFriendships")) + it.HasFriendships, err = ec.unmarshalOBoolean2ᚖbool(ctx, v) + if err != nil { + return it, err + } + case "hasFriendshipsWith": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("hasFriendshipsWith")) + it.HasFriendshipsWith, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } } } @@ -7793,6 +8538,11 @@ func (ec *executionContext) _Node(ctx context.Context, sel ast.SelectionSet, obj return graphql.Null } return ec._Category(ctx, sel, obj) + case *ent.Friendship: + if obj == nil { + return graphql.Null + } + return ec._Friendship(ctx, sel, obj) case *ent.Group: if obj == nil { return graphql.Null @@ -7920,6 +8670,95 @@ func (ec *executionContext) _CategoryConfig(ctx context.Context, sel ast.Selecti return out } +var friendshipImplementors = []string{"Friendship", "Node"} + +func (ec *executionContext) _Friendship(ctx context.Context, sel ast.SelectionSet, obj *ent.Friendship) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, friendshipImplementors) + out := graphql.NewFieldSet(fields) + var invalids uint32 + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("Friendship") + case "id": + + out.Values[i] = ec._Friendship_id(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "createdAt": + + out.Values[i] = ec._Friendship_createdAt(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "userID": + + out.Values[i] = ec._Friendship_userID(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "friendID": + + out.Values[i] = ec._Friendship_friendID(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "user": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Friendship_user(ctx, field, obj) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + return res + } + + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + case "friend": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Friendship_friend(ctx, field, obj) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + return res + } + + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch() + if invalids > 0 { + return graphql.Null + } + return out +} + var groupImplementors = []string{"Group", "Node"} func (ec *executionContext) _Group(ctx context.Context, sel ast.SelectionSet, obj *ent.Group) graphql.Marshaler { @@ -8569,6 +9408,23 @@ func (ec *executionContext) _User(ctx context.Context, sel ast.SelectionSet, obj return res } + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + case "friendships": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._User_friendships(ctx, field, obj) + return res + } + out.Concurrently(i, func() graphql.Marshaler { return innerFunc(ctx) @@ -9054,6 +9910,21 @@ func (ec *executionContext) marshalNDuration2timeᚐDuration(ctx context.Context return res } +func (ec *executionContext) marshalNFriendship2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendship(ctx context.Context, sel ast.SelectionSet, v *ent.Friendship) graphql.Marshaler { + if v == nil { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + return graphql.Null + } + return ec._Friendship(ctx, sel, v) +} + +func (ec *executionContext) unmarshalNFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendshipWhereInput(ctx context.Context, v interface{}) (*ent.FriendshipWhereInput, error) { + res, err := ec.unmarshalInputFriendshipWhereInput(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) marshalNGroupConnection2entgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐGroupConnection(ctx context.Context, sel ast.SelectionSet, v ent.GroupConnection) graphql.Marshaler { return ec._GroupConnection(ctx, sel, &v) } @@ -9842,6 +10713,81 @@ func (ec *executionContext) marshalODuration2ᚖtimeᚐDuration(ctx context.Cont return res } +func (ec *executionContext) marshalOFriendship2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendshipᚄ(ctx context.Context, sel ast.SelectionSet, v []*ent.Friendship) graphql.Marshaler { + if v == nil { + return graphql.Null + } + ret := make(graphql.Array, len(v)) + var wg sync.WaitGroup + isLen1 := len(v) == 1 + if !isLen1 { + wg.Add(len(v)) + } + for i := range v { + i := i + fc := &graphql.FieldContext{ + Index: &i, + Result: &v[i], + } + ctx := graphql.WithFieldContext(ctx, fc) + f := func(i int) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = nil + } + }() + if !isLen1 { + defer wg.Done() + } + ret[i] = ec.marshalNFriendship2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendship(ctx, sel, v[i]) + } + if isLen1 { + f(i) + } else { + go f(i) + } + + } + wg.Wait() + + for _, e := range ret { + if e == graphql.Null { + return graphql.Null + } + } + + return ret +} + +func (ec *executionContext) unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendshipWhereInputᚄ(ctx context.Context, v interface{}) ([]*ent.FriendshipWhereInput, error) { + if v == nil { + return nil, nil + } + var vSlice []interface{} + if v != nil { + vSlice = graphql.CoerceList(v) + } + var err error + res := make([]*ent.FriendshipWhereInput, len(vSlice)) + for i := range vSlice { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithIndex(i)) + res[i], err = ec.unmarshalNFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendshipWhereInput(ctx, vSlice[i]) + if err != nil { + return nil, err + } + } + return res, nil +} + +func (ec *executionContext) unmarshalOFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐFriendshipWhereInput(ctx context.Context, v interface{}) (*ent.FriendshipWhereInput, error) { + if v == nil { + return nil, nil + } + res, err := ec.unmarshalInputFriendshipWhereInput(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) marshalOGroup2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodogotypeᚋentᚐGroup(ctx context.Context, sel ast.SelectionSet, v *ent.Group) graphql.Marshaler { if v == nil { return graphql.Null diff --git a/entgql/internal/todogotype/models_gen.go b/entgql/internal/todogotype/models_gen.go new file mode 100644 index 000000000..aa7ba8d34 --- /dev/null +++ b/entgql/internal/todogotype/models_gen.go @@ -0,0 +1,21 @@ +// Code generated by github.com/99designs/gqlgen, DO NOT EDIT. + +package todo + +// CreateUserInput is used for create User object. +// Input was generated by ent. +type CreateUserInput struct { + Name *string `json:"name"` + GroupIDs []string `json:"groupIDs"` + FriendIDs []string `json:"friendIDs"` +} + +// UpdateUserInput is used for update User object. +// Input was generated by ent. +type UpdateUserInput struct { + Name *string `json:"name"` + AddGroupIDs []string `json:"addGroupIDs"` + RemoveGroupIDs []string `json:"removeGroupIDs"` + AddFriendIDs []string `json:"addFriendIDs"` + RemoveFriendIDs []string `json:"removeFriendIDs"` +} diff --git a/entgql/internal/todopulid/ent/client.go b/entgql/internal/todopulid/ent/client.go index 90d4f598a..d67d0b10a 100644 --- a/entgql/internal/todopulid/ent/client.go +++ b/entgql/internal/todopulid/ent/client.go @@ -319,7 +319,7 @@ func (c *FriendshipClient) UpdateOne(f *Friendship) *FriendshipUpdateOne { } // UpdateOneID returns an update builder for the given id. -func (c *FriendshipClient) UpdateOneID(id int) *FriendshipUpdateOne { +func (c *FriendshipClient) UpdateOneID(id pulid.ID) *FriendshipUpdateOne { mutation := newFriendshipMutation(c.config, OpUpdateOne, withFriendshipID(id)) return &FriendshipUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} } @@ -336,7 +336,7 @@ func (c *FriendshipClient) DeleteOne(f *Friendship) *FriendshipDeleteOne { } // DeleteOne returns a builder for deleting the given entity by its id. -func (c *FriendshipClient) DeleteOneID(id int) *FriendshipDeleteOne { +func (c *FriendshipClient) DeleteOneID(id pulid.ID) *FriendshipDeleteOne { builder := c.Delete().Where(friendship.ID(id)) builder.mutation.id = &id builder.mutation.op = OpDeleteOne @@ -351,12 +351,12 @@ func (c *FriendshipClient) Query() *FriendshipQuery { } // Get returns a Friendship entity by its id. -func (c *FriendshipClient) Get(ctx context.Context, id int) (*Friendship, error) { +func (c *FriendshipClient) Get(ctx context.Context, id pulid.ID) (*Friendship, error) { return c.Query().Where(friendship.ID(id)).Only(ctx) } // GetX is like Get, but panics if an error occurs. -func (c *FriendshipClient) GetX(ctx context.Context, id int) *Friendship { +func (c *FriendshipClient) GetX(ctx context.Context, id pulid.ID) *Friendship { obj, err := c.Get(ctx, id) if err != nil { panic(err) diff --git a/entgql/internal/todopulid/ent/friendship.go b/entgql/internal/todopulid/ent/friendship.go index be0b2133e..e5926bd1c 100644 --- a/entgql/internal/todopulid/ent/friendship.go +++ b/entgql/internal/todopulid/ent/friendship.go @@ -31,7 +31,7 @@ import ( type Friendship struct { config `json:"-"` // ID of the ent. - ID int `json:"id,omitempty"` + ID pulid.ID `json:"id,omitempty"` // CreatedAt holds the value of the "created_at" field. CreatedAt time.Time `json:"created_at,omitempty"` // UserID holds the value of the "user_id" field. @@ -89,10 +89,8 @@ func (*Friendship) scanValues(columns []string) ([]interface{}, error) { values := make([]interface{}, len(columns)) for i := range columns { switch columns[i] { - case friendship.FieldUserID, friendship.FieldFriendID: + case friendship.FieldID, friendship.FieldUserID, friendship.FieldFriendID: values[i] = new(pulid.ID) - case friendship.FieldID: - values[i] = new(sql.NullInt64) case friendship.FieldCreatedAt: values[i] = new(sql.NullTime) default: @@ -111,11 +109,11 @@ func (f *Friendship) assignValues(columns []string, values []interface{}) error for i := range columns { switch columns[i] { case friendship.FieldID: - value, ok := values[i].(*sql.NullInt64) - if !ok { - return fmt.Errorf("unexpected type %T for field id", value) + if value, ok := values[i].(*pulid.ID); !ok { + return fmt.Errorf("unexpected type %T for field id", values[i]) + } else if value != nil { + f.ID = *value } - f.ID = int(value.Int64) case friendship.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { return fmt.Errorf("unexpected type %T for field created_at", values[i]) diff --git a/entgql/internal/todopulid/ent/friendship/friendship.go b/entgql/internal/todopulid/ent/friendship/friendship.go index d6420dffe..fe71d7401 100644 --- a/entgql/internal/todopulid/ent/friendship/friendship.go +++ b/entgql/internal/todopulid/ent/friendship/friendship.go @@ -18,6 +18,8 @@ package friendship import ( "time" + + "entgo.io/contrib/entgql/internal/todopulid/ent/schema/pulid" ) const ( @@ -74,4 +76,6 @@ func ValidColumn(column string) bool { var ( // DefaultCreatedAt holds the default value on creation for the "created_at" field. DefaultCreatedAt func() time.Time + // DefaultID holds the default value on creation for the "id" field. + DefaultID func() pulid.ID ) diff --git a/entgql/internal/todopulid/ent/friendship/where.go b/entgql/internal/todopulid/ent/friendship/where.go index 944fd8293..14e279cfa 100644 --- a/entgql/internal/todopulid/ent/friendship/where.go +++ b/entgql/internal/todopulid/ent/friendship/where.go @@ -26,28 +26,28 @@ import ( ) // ID filters vertices based on their ID field. -func ID(id int) predicate.Friendship { +func ID(id pulid.ID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.EQ(s.C(FieldID), id)) }) } // IDEQ applies the EQ predicate on the ID field. -func IDEQ(id int) predicate.Friendship { +func IDEQ(id pulid.ID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.EQ(s.C(FieldID), id)) }) } // IDNEQ applies the NEQ predicate on the ID field. -func IDNEQ(id int) predicate.Friendship { +func IDNEQ(id pulid.ID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.NEQ(s.C(FieldID), id)) }) } // IDIn applies the In predicate on the ID field. -func IDIn(ids ...int) predicate.Friendship { +func IDIn(ids ...pulid.ID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { // if not arguments were provided, append the FALSE constants, // since we can't apply "IN ()". This will make this predicate falsy. @@ -64,7 +64,7 @@ func IDIn(ids ...int) predicate.Friendship { } // IDNotIn applies the NotIn predicate on the ID field. -func IDNotIn(ids ...int) predicate.Friendship { +func IDNotIn(ids ...pulid.ID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { // if not arguments were provided, append the FALSE constants, // since we can't apply "IN ()". This will make this predicate falsy. @@ -81,28 +81,28 @@ func IDNotIn(ids ...int) predicate.Friendship { } // IDGT applies the GT predicate on the ID field. -func IDGT(id int) predicate.Friendship { +func IDGT(id pulid.ID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.GT(s.C(FieldID), id)) }) } // IDGTE applies the GTE predicate on the ID field. -func IDGTE(id int) predicate.Friendship { +func IDGTE(id pulid.ID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.GTE(s.C(FieldID), id)) }) } // IDLT applies the LT predicate on the ID field. -func IDLT(id int) predicate.Friendship { +func IDLT(id pulid.ID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.LT(s.C(FieldID), id)) }) } // IDLTE applies the LTE predicate on the ID field. -func IDLTE(id int) predicate.Friendship { +func IDLTE(id pulid.ID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.LTE(s.C(FieldID), id)) }) diff --git a/entgql/internal/todopulid/ent/friendship_create.go b/entgql/internal/todopulid/ent/friendship_create.go index 27d123e1d..b73295d2d 100644 --- a/entgql/internal/todopulid/ent/friendship_create.go +++ b/entgql/internal/todopulid/ent/friendship_create.go @@ -62,6 +62,20 @@ func (fc *FriendshipCreate) SetFriendID(pu pulid.ID) *FriendshipCreate { return fc } +// SetID sets the "id" field. +func (fc *FriendshipCreate) SetID(pu pulid.ID) *FriendshipCreate { + fc.mutation.SetID(pu) + return fc +} + +// SetNillableID sets the "id" field if the given value is not nil. +func (fc *FriendshipCreate) SetNillableID(pu *pulid.ID) *FriendshipCreate { + if pu != nil { + fc.SetID(*pu) + } + return fc +} + // SetUser sets the "user" edge to the User entity. func (fc *FriendshipCreate) SetUser(u *User) *FriendshipCreate { return fc.SetUserID(u.ID) @@ -153,6 +167,10 @@ func (fc *FriendshipCreate) defaults() { v := friendship.DefaultCreatedAt() fc.mutation.SetCreatedAt(v) } + if _, ok := fc.mutation.ID(); !ok { + v := friendship.DefaultID() + fc.mutation.SetID(v) + } } // check runs all checks and user-defined validators on the builder. @@ -183,8 +201,13 @@ func (fc *FriendshipCreate) sqlSave(ctx context.Context) (*Friendship, error) { } return nil, err } - id := _spec.ID.Value.(int64) - _node.ID = int(id) + if _spec.ID.Value != nil { + if id, ok := _spec.ID.Value.(*pulid.ID); ok { + _node.ID = *id + } else if err := _node.ID.Scan(_spec.ID.Value); err != nil { + return nil, err + } + } return _node, nil } @@ -194,11 +217,15 @@ func (fc *FriendshipCreate) createSpec() (*Friendship, *sqlgraph.CreateSpec) { _spec = &sqlgraph.CreateSpec{ Table: friendship.Table, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, } ) + if id, ok := fc.mutation.ID(); ok { + _node.ID = id + _spec.ID.Value = &id + } if value, ok := fc.mutation.CreatedAt(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeTime, @@ -291,10 +318,6 @@ func (fcb *FriendshipCreateBulk) Save(ctx context.Context) ([]*Friendship, error return nil, err } mutation.id = &nodes[i].ID - if specs[i].ID.Value != nil { - id := specs[i].ID.Value.(int64) - nodes[i].ID = int(id) - } mutation.done = true return nodes[i], nil }) diff --git a/entgql/internal/todopulid/ent/friendship_delete.go b/entgql/internal/todopulid/ent/friendship_delete.go index fd79ffc09..b709a2520 100644 --- a/entgql/internal/todopulid/ent/friendship_delete.go +++ b/entgql/internal/todopulid/ent/friendship_delete.go @@ -86,7 +86,7 @@ func (fd *FriendshipDelete) sqlExec(ctx context.Context) (int, error) { Node: &sqlgraph.NodeSpec{ Table: friendship.Table, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, diff --git a/entgql/internal/todopulid/ent/friendship_query.go b/entgql/internal/todopulid/ent/friendship_query.go index bd33e3b4f..281131935 100644 --- a/entgql/internal/todopulid/ent/friendship_query.go +++ b/entgql/internal/todopulid/ent/friendship_query.go @@ -148,8 +148,8 @@ func (fq *FriendshipQuery) FirstX(ctx context.Context) *Friendship { // FirstID returns the first Friendship ID from the query. // Returns a *NotFoundError when no Friendship ID was found. -func (fq *FriendshipQuery) FirstID(ctx context.Context) (id int, err error) { - var ids []int +func (fq *FriendshipQuery) FirstID(ctx context.Context) (id pulid.ID, err error) { + var ids []pulid.ID if ids, err = fq.Limit(1).IDs(ctx); err != nil { return } @@ -161,7 +161,7 @@ func (fq *FriendshipQuery) FirstID(ctx context.Context) (id int, err error) { } // FirstIDX is like FirstID, but panics if an error occurs. -func (fq *FriendshipQuery) FirstIDX(ctx context.Context) int { +func (fq *FriendshipQuery) FirstIDX(ctx context.Context) pulid.ID { id, err := fq.FirstID(ctx) if err != nil && !IsNotFound(err) { panic(err) @@ -199,8 +199,8 @@ func (fq *FriendshipQuery) OnlyX(ctx context.Context) *Friendship { // OnlyID is like Only, but returns the only Friendship ID in the query. // Returns a *NotSingularError when more than one Friendship ID is found. // Returns a *NotFoundError when no entities are found. -func (fq *FriendshipQuery) OnlyID(ctx context.Context) (id int, err error) { - var ids []int +func (fq *FriendshipQuery) OnlyID(ctx context.Context) (id pulid.ID, err error) { + var ids []pulid.ID if ids, err = fq.Limit(2).IDs(ctx); err != nil { return } @@ -216,7 +216,7 @@ func (fq *FriendshipQuery) OnlyID(ctx context.Context) (id int, err error) { } // OnlyIDX is like OnlyID, but panics if an error occurs. -func (fq *FriendshipQuery) OnlyIDX(ctx context.Context) int { +func (fq *FriendshipQuery) OnlyIDX(ctx context.Context) pulid.ID { id, err := fq.OnlyID(ctx) if err != nil { panic(err) @@ -242,8 +242,8 @@ func (fq *FriendshipQuery) AllX(ctx context.Context) []*Friendship { } // IDs executes the query and returns a list of Friendship IDs. -func (fq *FriendshipQuery) IDs(ctx context.Context) ([]int, error) { - var ids []int +func (fq *FriendshipQuery) IDs(ctx context.Context) ([]pulid.ID, error) { + var ids []pulid.ID if err := fq.Select(friendship.FieldID).Scan(ctx, &ids); err != nil { return nil, err } @@ -251,7 +251,7 @@ func (fq *FriendshipQuery) IDs(ctx context.Context) ([]int, error) { } // IDsX is like IDs, but panics if an error occurs. -func (fq *FriendshipQuery) IDsX(ctx context.Context) []int { +func (fq *FriendshipQuery) IDsX(ctx context.Context) []pulid.ID { ids, err := fq.IDs(ctx) if err != nil { panic(err) @@ -519,7 +519,7 @@ func (fq *FriendshipQuery) querySpec() *sqlgraph.QuerySpec { Table: friendship.Table, Columns: friendship.Columns, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, diff --git a/entgql/internal/todopulid/ent/friendship_update.go b/entgql/internal/todopulid/ent/friendship_update.go index 2479cd2a8..2b53863c4 100644 --- a/entgql/internal/todopulid/ent/friendship_update.go +++ b/entgql/internal/todopulid/ent/friendship_update.go @@ -174,7 +174,7 @@ func (fu *FriendshipUpdate) sqlSave(ctx context.Context) (n int, err error) { Table: friendship.Table, Columns: friendship.Columns, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, @@ -425,7 +425,7 @@ func (fuo *FriendshipUpdateOne) sqlSave(ctx context.Context) (_node *Friendship, Table: friendship.Table, Columns: friendship.Columns, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, diff --git a/entgql/internal/todopulid/ent/gql_collection.go b/entgql/internal/todopulid/ent/gql_collection.go index bdb3fd82d..7be03b676 100644 --- a/entgql/internal/todopulid/ent/gql_collection.go +++ b/entgql/internal/todopulid/ent/gql_collection.go @@ -199,6 +199,74 @@ func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { return args } +// CollectFields tells the query-builder to eagerly load connected nodes by resolver context. +func (f *FriendshipQuery) CollectFields(ctx context.Context, satisfies ...string) (*FriendshipQuery, error) { + fc := graphql.GetFieldContext(ctx) + if fc == nil { + return f, nil + } + if err := f.collectField(ctx, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil { + return nil, err + } + return f, nil +} + +func (f *FriendshipQuery) collectField(ctx context.Context, op *graphql.OperationContext, field graphql.CollectedField, path []string, satisfies ...string) error { + path = append([]string(nil), path...) + for _, field := range graphql.CollectFields(op, field.Selections, satisfies) { + switch field.Name { + case "user": + var ( + path = append(path, field.Name) + query = &UserQuery{config: f.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + f.withUser = query + case "friend": + var ( + path = append(path, field.Name) + query = &UserQuery{config: f.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + f.withFriend = query + } + } + return nil +} + +type friendshipPaginateArgs struct { + first, last *int + after, before *Cursor + opts []FriendshipPaginateOption +} + +func newFriendshipPaginateArgs(rv map[string]interface{}) *friendshipPaginateArgs { + args := &friendshipPaginateArgs{} + if rv == nil { + return args + } + if v := rv[firstField]; v != nil { + args.first = v.(*int) + } + if v := rv[lastField]; v != nil { + args.last = v.(*int) + } + if v := rv[afterField]; v != nil { + args.after = v.(*Cursor) + } + if v := rv[beforeField]; v != nil { + args.before = v.(*Cursor) + } + if v, ok := rv[whereField].(*FriendshipWhereInput); ok { + args.opts = append(args.opts, WithFriendshipFilter(v.Filter)) + } + return args +} + // CollectFields tells the query-builder to eagerly load connected nodes by resolver context. func (gr *GroupQuery) CollectFields(ctx context.Context, satisfies ...string) (*GroupQuery, error) { fc := graphql.GetFieldContext(ctx) @@ -671,6 +739,15 @@ func (u *UserQuery) collectField(ctx context.Context, op *graphql.OperationConte return err } u.withFriends = query + case "friendships": + var ( + path = append(path, field.Name) + query = &FriendshipQuery{config: u.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + u.withFriendships = query } } return nil diff --git a/entgql/internal/todopulid/ent/gql_edge.go b/entgql/internal/todopulid/ent/gql_edge.go index c22eed8c1..a30781f2d 100644 --- a/entgql/internal/todopulid/ent/gql_edge.go +++ b/entgql/internal/todopulid/ent/gql_edge.go @@ -94,6 +94,22 @@ func (c *Category) Todos( return conn, nil } +func (f *Friendship) User(ctx context.Context) (*User, error) { + result, err := f.Edges.UserOrErr() + if IsNotLoaded(err) { + result, err = f.QueryUser().Only(ctx) + } + return result, err +} + +func (f *Friendship) Friend(ctx context.Context) (*User, error) { + result, err := f.Edges.FriendOrErr() + if IsNotLoaded(err) { + result, err = f.QueryFriend().Only(ctx) + } + return result, err +} + func (gr *Group) Users( ctx context.Context, after *Cursor, first *int, before *Cursor, last *int, where *UserWhereInput, ) (*UserConnection, error) { @@ -331,3 +347,11 @@ func (u *User) Friends(ctx context.Context) ([]*User, error) { } return result, err } + +func (u *User) Friendships(ctx context.Context) ([]*Friendship, error) { + result, err := u.Edges.FriendshipsOrErr() + if IsNotLoaded(err) { + result, err = u.QueryFriendships().All(ctx) + } + return result, err +} diff --git a/entgql/internal/todopulid/ent/gql_mutation_input.go b/entgql/internal/todopulid/ent/gql_mutation_input.go index 1df283c34..9ddab00d5 100644 --- a/entgql/internal/todopulid/ent/gql_mutation_input.go +++ b/entgql/internal/todopulid/ent/gql_mutation_input.go @@ -58,3 +58,69 @@ func (c *TodoCreate) SetInput(i CreateTodoInput) *TodoCreate { i.Mutate(c.Mutation()) return c } + +// CreateUserInput represents a mutation input for creating users. +type CreateUserInput struct { + Name *string + GroupIDs []pulid.ID + FriendIDs []pulid.ID +} + +// Mutate applies the CreateUserInput on the UserMutation builder. +func (i *CreateUserInput) Mutate(m *UserMutation) { + if v := i.Name; v != nil { + m.SetName(*v) + } + if v := i.GroupIDs; len(v) > 0 { + m.AddGroupIDs(v...) + } + if v := i.FriendIDs; len(v) > 0 { + m.AddFriendIDs(v...) + } +} + +// SetInput applies the change-set in the CreateUserInput on the UserCreate builder. +func (c *UserCreate) SetInput(i CreateUserInput) *UserCreate { + i.Mutate(c.Mutation()) + return c +} + +// UpdateUserInput represents a mutation input for updating users. +type UpdateUserInput struct { + Name *string + AddGroupIDs []pulid.ID + RemoveGroupIDs []pulid.ID + AddFriendIDs []pulid.ID + RemoveFriendIDs []pulid.ID +} + +// Mutate applies the UpdateUserInput on the UserMutation builder. +func (i *UpdateUserInput) Mutate(m *UserMutation) { + if v := i.Name; v != nil { + m.SetName(*v) + } + if v := i.AddGroupIDs; len(v) > 0 { + m.AddGroupIDs(v...) + } + if v := i.RemoveGroupIDs; len(v) > 0 { + m.RemoveGroupIDs(v...) + } + if v := i.AddFriendIDs; len(v) > 0 { + m.AddFriendIDs(v...) + } + if v := i.RemoveFriendIDs; len(v) > 0 { + m.RemoveFriendIDs(v...) + } +} + +// SetInput applies the change-set in the UpdateUserInput on the UserUpdate builder. +func (c *UserUpdate) SetInput(i UpdateUserInput) *UserUpdate { + i.Mutate(c.Mutation()) + return c +} + +// SetInput applies the change-set in the UpdateUserInput on the UserUpdateOne builder. +func (c *UserUpdateOne) SetInput(i UpdateUserInput) *UserUpdateOne { + i.Mutate(c.Mutation()) + return c +} diff --git a/entgql/internal/todopulid/ent/gql_node.go b/entgql/internal/todopulid/ent/gql_node.go index 23de36867..7238e7760 100644 --- a/entgql/internal/todopulid/ent/gql_node.go +++ b/entgql/internal/todopulid/ent/gql_node.go @@ -23,6 +23,7 @@ import ( "entgo.io/contrib/entgql" "entgo.io/contrib/entgql/internal/todopulid/ent/category" + "entgo.io/contrib/entgql/internal/todopulid/ent/friendship" "entgo.io/contrib/entgql/internal/todopulid/ent/group" "entgo.io/contrib/entgql/internal/todopulid/ent/schema/pulid" "entgo.io/contrib/entgql/internal/todopulid/ent/todo" @@ -127,6 +128,61 @@ func (c *Category) Node(ctx context.Context) (node *Node, err error) { return node, nil } +func (f *Friendship) Node(ctx context.Context) (node *Node, err error) { + node = &Node{ + ID: f.ID, + Type: "Friendship", + Fields: make([]*Field, 3), + Edges: make([]*Edge, 2), + } + var buf []byte + if buf, err = json.Marshal(f.CreatedAt); err != nil { + return nil, err + } + node.Fields[0] = &Field{ + Type: "time.Time", + Name: "created_at", + Value: string(buf), + } + if buf, err = json.Marshal(f.UserID); err != nil { + return nil, err + } + node.Fields[1] = &Field{ + Type: "pulid.ID", + Name: "user_id", + Value: string(buf), + } + if buf, err = json.Marshal(f.FriendID); err != nil { + return nil, err + } + node.Fields[2] = &Field{ + Type: "pulid.ID", + Name: "friend_id", + Value: string(buf), + } + node.Edges[0] = &Edge{ + Type: "User", + Name: "user", + } + err = f.QueryUser(). + Select(user.FieldID). + Scan(ctx, &node.Edges[0].IDs) + if err != nil { + return nil, err + } + node.Edges[1] = &Edge{ + Type: "User", + Name: "friend", + } + err = f.QueryFriend(). + Select(user.FieldID). + Scan(ctx, &node.Edges[1].IDs) + if err != nil { + return nil, err + } + return node, nil +} + func (gr *Group) Node(ctx context.Context) (node *Node, err error) { node = &Node{ ID: gr.ID, @@ -242,7 +298,7 @@ func (u *User) Node(ctx context.Context) (node *Node, err error) { ID: u.ID, Type: "User", Fields: make([]*Field, 1), - Edges: make([]*Edge, 2), + Edges: make([]*Edge, 3), } var buf []byte if buf, err = json.Marshal(u.Name); err != nil { @@ -273,6 +329,16 @@ func (u *User) Node(ctx context.Context) (node *Node, err error) { if err != nil { return nil, err } + node.Edges[2] = &Edge{ + Type: "Friendship", + Name: "friendships", + } + err = u.QueryFriendships(). + Select(friendship.FieldID). + Scan(ctx, &node.Edges[2].IDs) + if err != nil { + return nil, err + } return node, nil } @@ -359,6 +425,22 @@ func (c *Client) noder(ctx context.Context, table string, id pulid.ID) (Noder, e return nil, err } return n, nil + case friendship.Table: + var uid pulid.ID + if err := uid.UnmarshalGQL(id); err != nil { + return nil, err + } + query := c.Friendship.Query(). + Where(friendship.ID(uid)) + query, err := query.CollectFields(ctx, "Friendship") + if err != nil { + return nil, err + } + n, err := query.Only(ctx) + if err != nil { + return nil, err + } + return n, nil case group.Table: var uid pulid.ID if err := uid.UnmarshalGQL(id); err != nil { @@ -496,6 +578,22 @@ func (c *Client) noders(ctx context.Context, table string, ids []pulid.ID) ([]No *noder = node } } + case friendship.Table: + query := c.Friendship.Query(). + Where(friendship.IDIn(ids...)) + query, err := query.CollectFields(ctx, "Friendship") + if err != nil { + return nil, err + } + nodes, err := query.All(ctx) + if err != nil { + return nil, err + } + for _, node := range nodes { + for _, noder := range idmap[node.ID] { + *noder = node + } + } case group.Table: query := c.Group.Query(). Where(group.IDIn(ids...)) diff --git a/entgql/internal/todopulid/ent/gql_pagination.go b/entgql/internal/todopulid/ent/gql_pagination.go index 31e6f365a..e1670bf9a 100644 --- a/entgql/internal/todopulid/ent/gql_pagination.go +++ b/entgql/internal/todopulid/ent/gql_pagination.go @@ -26,6 +26,7 @@ import ( "strings" "entgo.io/contrib/entgql/internal/todopulid/ent/category" + "entgo.io/contrib/entgql/internal/todopulid/ent/friendship" "entgo.io/contrib/entgql/internal/todopulid/ent/group" "entgo.io/contrib/entgql/internal/todopulid/ent/schema/pulid" "entgo.io/contrib/entgql/internal/todopulid/ent/todo" @@ -551,6 +552,241 @@ func (c *Category) ToEdge(order *CategoryOrder) *CategoryEdge { } } +// FriendshipEdge is the edge representation of Friendship. +type FriendshipEdge struct { + Node *Friendship `json:"node"` + Cursor Cursor `json:"cursor"` +} + +// FriendshipConnection is the connection containing edges to Friendship. +type FriendshipConnection struct { + Edges []*FriendshipEdge `json:"edges"` + PageInfo PageInfo `json:"pageInfo"` + TotalCount int `json:"totalCount"` +} + +func (c *FriendshipConnection) build(nodes []*Friendship, pager *friendshipPager, after *Cursor, first *int, before *Cursor, last *int) { + c.PageInfo.HasNextPage = before != nil + c.PageInfo.HasPreviousPage = after != nil + if first != nil && *first+1 == len(nodes) { + c.PageInfo.HasNextPage = true + nodes = nodes[:len(nodes)-1] + } else if last != nil && *last+1 == len(nodes) { + c.PageInfo.HasPreviousPage = true + nodes = nodes[:len(nodes)-1] + } + var nodeAt func(int) *Friendship + if last != nil { + n := len(nodes) - 1 + nodeAt = func(i int) *Friendship { + return nodes[n-i] + } + } else { + nodeAt = func(i int) *Friendship { + return nodes[i] + } + } + c.Edges = make([]*FriendshipEdge, len(nodes)) + for i := range nodes { + node := nodeAt(i) + c.Edges[i] = &FriendshipEdge{ + Node: node, + Cursor: pager.toCursor(node), + } + } + if l := len(c.Edges); l > 0 { + c.PageInfo.StartCursor = &c.Edges[0].Cursor + c.PageInfo.EndCursor = &c.Edges[l-1].Cursor + } + if c.TotalCount == 0 { + c.TotalCount = len(nodes) + } +} + +// FriendshipPaginateOption enables pagination customization. +type FriendshipPaginateOption func(*friendshipPager) error + +// WithFriendshipOrder configures pagination ordering. +func WithFriendshipOrder(order *FriendshipOrder) FriendshipPaginateOption { + if order == nil { + order = DefaultFriendshipOrder + } + o := *order + return func(pager *friendshipPager) error { + if err := o.Direction.Validate(); err != nil { + return err + } + if o.Field == nil { + o.Field = DefaultFriendshipOrder.Field + } + pager.order = &o + return nil + } +} + +// WithFriendshipFilter configures pagination filter. +func WithFriendshipFilter(filter func(*FriendshipQuery) (*FriendshipQuery, error)) FriendshipPaginateOption { + return func(pager *friendshipPager) error { + if filter == nil { + return errors.New("FriendshipQuery filter cannot be nil") + } + pager.filter = filter + return nil + } +} + +type friendshipPager struct { + order *FriendshipOrder + filter func(*FriendshipQuery) (*FriendshipQuery, error) +} + +func newFriendshipPager(opts []FriendshipPaginateOption) (*friendshipPager, error) { + pager := &friendshipPager{} + for _, opt := range opts { + if err := opt(pager); err != nil { + return nil, err + } + } + if pager.order == nil { + pager.order = DefaultFriendshipOrder + } + return pager, nil +} + +func (p *friendshipPager) applyFilter(query *FriendshipQuery) (*FriendshipQuery, error) { + if p.filter != nil { + return p.filter(query) + } + return query, nil +} + +func (p *friendshipPager) toCursor(f *Friendship) Cursor { + return p.order.Field.toCursor(f) +} + +func (p *friendshipPager) applyCursors(query *FriendshipQuery, after, before *Cursor) *FriendshipQuery { + for _, predicate := range cursorsToPredicates( + p.order.Direction, after, before, + p.order.Field.field, DefaultFriendshipOrder.Field.field, + ) { + query = query.Where(predicate) + } + return query +} + +func (p *friendshipPager) applyOrder(query *FriendshipQuery, reverse bool) *FriendshipQuery { + direction := p.order.Direction + if reverse { + direction = direction.reverse() + } + query = query.Order(direction.orderFunc(p.order.Field.field)) + if p.order.Field != DefaultFriendshipOrder.Field { + query = query.Order(direction.orderFunc(DefaultFriendshipOrder.Field.field)) + } + return query +} + +func (p *friendshipPager) orderExpr(reverse bool) sql.Querier { + direction := p.order.Direction + if reverse { + direction = direction.reverse() + } + return sql.ExprFunc(func(b *sql.Builder) { + b.Ident(p.order.Field.field).Pad().WriteString(string(direction)) + if p.order.Field != DefaultFriendshipOrder.Field { + b.Comma().Ident(DefaultFriendshipOrder.Field.field).Pad().WriteString(string(direction)) + } + }) +} + +// Paginate executes the query and returns a relay based cursor connection to Friendship. +func (f *FriendshipQuery) Paginate( + ctx context.Context, after *Cursor, first *int, + before *Cursor, last *int, opts ...FriendshipPaginateOption, +) (*FriendshipConnection, error) { + if err := validateFirstLast(first, last); err != nil { + return nil, err + } + pager, err := newFriendshipPager(opts) + if err != nil { + return nil, err + } + if f, err = pager.applyFilter(f); err != nil { + return nil, err + } + conn := &FriendshipConnection{Edges: []*FriendshipEdge{}} + if !hasCollectedField(ctx, edgesField) || first != nil && *first == 0 || last != nil && *last == 0 { + if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) { + if conn.TotalCount, err = f.Count(ctx); err != nil { + return nil, err + } + conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0 + conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0 + } + return conn, nil + } + + if (after != nil || first != nil || before != nil || last != nil) && hasCollectedField(ctx, totalCountField) { + count, err := f.Clone().Count(ctx) + if err != nil { + return nil, err + } + conn.TotalCount = count + } + + f = pager.applyCursors(f, after, before) + f = pager.applyOrder(f, last != nil) + if limit := paginateLimit(first, last); limit != 0 { + f.Limit(limit) + } + if field := collectedField(ctx, edgesField, nodeField); field != nil { + if err := f.collectField(ctx, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil { + return nil, err + } + } + + nodes, err := f.All(ctx) + if err != nil || len(nodes) == 0 { + return conn, err + } + conn.build(nodes, pager, after, first, before, last) + return conn, nil +} + +// FriendshipOrderField defines the ordering field of Friendship. +type FriendshipOrderField struct { + field string + toCursor func(*Friendship) Cursor +} + +// FriendshipOrder defines the ordering of Friendship. +type FriendshipOrder struct { + Direction OrderDirection `json:"direction"` + Field *FriendshipOrderField `json:"field"` +} + +// DefaultFriendshipOrder is the default ordering of Friendship. +var DefaultFriendshipOrder = &FriendshipOrder{ + Direction: OrderDirectionAsc, + Field: &FriendshipOrderField{ + field: friendship.FieldID, + toCursor: func(f *Friendship) Cursor { + return Cursor{ID: f.ID} + }, + }, +} + +// ToEdge converts Friendship into FriendshipEdge. +func (f *Friendship) ToEdge(order *FriendshipOrder) *FriendshipEdge { + if order == nil { + order = DefaultFriendshipOrder + } + return &FriendshipEdge{ + Node: f, + Cursor: order.Field.toCursor(f), + } +} + // GroupEdge is the edge representation of Group. type GroupEdge struct { Node *Group `json:"node"` diff --git a/entgql/internal/todopulid/ent/gql_where_input.go b/entgql/internal/todopulid/ent/gql_where_input.go index 17649e90b..c27dea4db 100644 --- a/entgql/internal/todopulid/ent/gql_where_input.go +++ b/entgql/internal/todopulid/ent/gql_where_input.go @@ -23,6 +23,7 @@ import ( "entgo.io/contrib/entgql/internal/todo/ent/schema/schematype" "entgo.io/contrib/entgql/internal/todopulid/ent/category" + "entgo.io/contrib/entgql/internal/todopulid/ent/friendship" "entgo.io/contrib/entgql/internal/todopulid/ent/group" "entgo.io/contrib/entgql/internal/todopulid/ent/predicate" "entgo.io/contrib/entgql/internal/todopulid/ent/schema/pulid" @@ -374,6 +375,316 @@ func (i *CategoryWhereInput) P() (predicate.Category, error) { } } +// FriendshipWhereInput represents a where input for filtering Friendship queries. +type FriendshipWhereInput struct { + Predicates []predicate.Friendship `json:"-"` + Not *FriendshipWhereInput `json:"not,omitempty"` + Or []*FriendshipWhereInput `json:"or,omitempty"` + And []*FriendshipWhereInput `json:"and,omitempty"` + + // "id" field predicates. + ID *pulid.ID `json:"id,omitempty"` + IDNEQ *pulid.ID `json:"idNEQ,omitempty"` + IDIn []pulid.ID `json:"idIn,omitempty"` + IDNotIn []pulid.ID `json:"idNotIn,omitempty"` + IDGT *pulid.ID `json:"idGT,omitempty"` + IDGTE *pulid.ID `json:"idGTE,omitempty"` + IDLT *pulid.ID `json:"idLT,omitempty"` + IDLTE *pulid.ID `json:"idLTE,omitempty"` + + // "created_at" field predicates. + CreatedAt *time.Time `json:"createdAt,omitempty"` + CreatedAtNEQ *time.Time `json:"createdAtNEQ,omitempty"` + CreatedAtIn []time.Time `json:"createdAtIn,omitempty"` + CreatedAtNotIn []time.Time `json:"createdAtNotIn,omitempty"` + CreatedAtGT *time.Time `json:"createdAtGT,omitempty"` + CreatedAtGTE *time.Time `json:"createdAtGTE,omitempty"` + CreatedAtLT *time.Time `json:"createdAtLT,omitempty"` + CreatedAtLTE *time.Time `json:"createdAtLTE,omitempty"` + + // "user_id" field predicates. + UserID *pulid.ID `json:"userID,omitempty"` + UserIDNEQ *pulid.ID `json:"userIDNEQ,omitempty"` + UserIDIn []pulid.ID `json:"userIDIn,omitempty"` + UserIDNotIn []pulid.ID `json:"userIDNotIn,omitempty"` + UserIDGT *pulid.ID `json:"userIDGT,omitempty"` + UserIDGTE *pulid.ID `json:"userIDGTE,omitempty"` + UserIDLT *pulid.ID `json:"userIDLT,omitempty"` + UserIDLTE *pulid.ID `json:"userIDLTE,omitempty"` + UserIDContains *pulid.ID `json:"userIDContains,omitempty"` + UserIDHasPrefix *pulid.ID `json:"userIDHasPrefix,omitempty"` + UserIDHasSuffix *pulid.ID `json:"userIDHasSuffix,omitempty"` + UserIDEqualFold *pulid.ID `json:"userIDEqualFold,omitempty"` + UserIDContainsFold *pulid.ID `json:"userIDContainsFold,omitempty"` + + // "friend_id" field predicates. + FriendID *pulid.ID `json:"friendID,omitempty"` + FriendIDNEQ *pulid.ID `json:"friendIDNEQ,omitempty"` + FriendIDIn []pulid.ID `json:"friendIDIn,omitempty"` + FriendIDNotIn []pulid.ID `json:"friendIDNotIn,omitempty"` + FriendIDGT *pulid.ID `json:"friendIDGT,omitempty"` + FriendIDGTE *pulid.ID `json:"friendIDGTE,omitempty"` + FriendIDLT *pulid.ID `json:"friendIDLT,omitempty"` + FriendIDLTE *pulid.ID `json:"friendIDLTE,omitempty"` + FriendIDContains *pulid.ID `json:"friendIDContains,omitempty"` + FriendIDHasPrefix *pulid.ID `json:"friendIDHasPrefix,omitempty"` + FriendIDHasSuffix *pulid.ID `json:"friendIDHasSuffix,omitempty"` + FriendIDEqualFold *pulid.ID `json:"friendIDEqualFold,omitempty"` + FriendIDContainsFold *pulid.ID `json:"friendIDContainsFold,omitempty"` + + // "user" edge predicates. + HasUser *bool `json:"hasUser,omitempty"` + HasUserWith []*UserWhereInput `json:"hasUserWith,omitempty"` + + // "friend" edge predicates. + HasFriend *bool `json:"hasFriend,omitempty"` + HasFriendWith []*UserWhereInput `json:"hasFriendWith,omitempty"` +} + +// AddPredicates adds custom predicates to the where input to be used during the filtering phase. +func (i *FriendshipWhereInput) AddPredicates(predicates ...predicate.Friendship) { + i.Predicates = append(i.Predicates, predicates...) +} + +// Filter applies the FriendshipWhereInput filter on the FriendshipQuery builder. +func (i *FriendshipWhereInput) Filter(q *FriendshipQuery) (*FriendshipQuery, error) { + if i == nil { + return q, nil + } + p, err := i.P() + if err != nil { + if err == ErrEmptyFriendshipWhereInput { + return q, nil + } + return nil, err + } + return q.Where(p), nil +} + +// ErrEmptyFriendshipWhereInput is returned in case the FriendshipWhereInput is empty. +var ErrEmptyFriendshipWhereInput = errors.New("ent: empty predicate FriendshipWhereInput") + +// P returns a predicate for filtering friendships. +// An error is returned if the input is empty or invalid. +func (i *FriendshipWhereInput) P() (predicate.Friendship, error) { + var predicates []predicate.Friendship + if i.Not != nil { + p, err := i.Not.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'not'", err) + } + predicates = append(predicates, friendship.Not(p)) + } + switch n := len(i.Or); { + case n == 1: + p, err := i.Or[0].P() + if err != nil { + return nil, fmt.Errorf("%w: field 'or'", err) + } + predicates = append(predicates, p) + case n > 1: + or := make([]predicate.Friendship, 0, n) + for _, w := range i.Or { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'or'", err) + } + or = append(or, p) + } + predicates = append(predicates, friendship.Or(or...)) + } + switch n := len(i.And); { + case n == 1: + p, err := i.And[0].P() + if err != nil { + return nil, fmt.Errorf("%w: field 'and'", err) + } + predicates = append(predicates, p) + case n > 1: + and := make([]predicate.Friendship, 0, n) + for _, w := range i.And { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'and'", err) + } + and = append(and, p) + } + predicates = append(predicates, friendship.And(and...)) + } + predicates = append(predicates, i.Predicates...) + if i.ID != nil { + predicates = append(predicates, friendship.IDEQ(*i.ID)) + } + if i.IDNEQ != nil { + predicates = append(predicates, friendship.IDNEQ(*i.IDNEQ)) + } + if len(i.IDIn) > 0 { + predicates = append(predicates, friendship.IDIn(i.IDIn...)) + } + if len(i.IDNotIn) > 0 { + predicates = append(predicates, friendship.IDNotIn(i.IDNotIn...)) + } + if i.IDGT != nil { + predicates = append(predicates, friendship.IDGT(*i.IDGT)) + } + if i.IDGTE != nil { + predicates = append(predicates, friendship.IDGTE(*i.IDGTE)) + } + if i.IDLT != nil { + predicates = append(predicates, friendship.IDLT(*i.IDLT)) + } + if i.IDLTE != nil { + predicates = append(predicates, friendship.IDLTE(*i.IDLTE)) + } + if i.CreatedAt != nil { + predicates = append(predicates, friendship.CreatedAtEQ(*i.CreatedAt)) + } + if i.CreatedAtNEQ != nil { + predicates = append(predicates, friendship.CreatedAtNEQ(*i.CreatedAtNEQ)) + } + if len(i.CreatedAtIn) > 0 { + predicates = append(predicates, friendship.CreatedAtIn(i.CreatedAtIn...)) + } + if len(i.CreatedAtNotIn) > 0 { + predicates = append(predicates, friendship.CreatedAtNotIn(i.CreatedAtNotIn...)) + } + if i.CreatedAtGT != nil { + predicates = append(predicates, friendship.CreatedAtGT(*i.CreatedAtGT)) + } + if i.CreatedAtGTE != nil { + predicates = append(predicates, friendship.CreatedAtGTE(*i.CreatedAtGTE)) + } + if i.CreatedAtLT != nil { + predicates = append(predicates, friendship.CreatedAtLT(*i.CreatedAtLT)) + } + if i.CreatedAtLTE != nil { + predicates = append(predicates, friendship.CreatedAtLTE(*i.CreatedAtLTE)) + } + if i.UserID != nil { + predicates = append(predicates, friendship.UserIDEQ(*i.UserID)) + } + if i.UserIDNEQ != nil { + predicates = append(predicates, friendship.UserIDNEQ(*i.UserIDNEQ)) + } + if len(i.UserIDIn) > 0 { + predicates = append(predicates, friendship.UserIDIn(i.UserIDIn...)) + } + if len(i.UserIDNotIn) > 0 { + predicates = append(predicates, friendship.UserIDNotIn(i.UserIDNotIn...)) + } + if i.UserIDGT != nil { + predicates = append(predicates, friendship.UserIDGT(*i.UserIDGT)) + } + if i.UserIDGTE != nil { + predicates = append(predicates, friendship.UserIDGTE(*i.UserIDGTE)) + } + if i.UserIDLT != nil { + predicates = append(predicates, friendship.UserIDLT(*i.UserIDLT)) + } + if i.UserIDLTE != nil { + predicates = append(predicates, friendship.UserIDLTE(*i.UserIDLTE)) + } + if i.UserIDContains != nil { + predicates = append(predicates, friendship.UserIDContains(*i.UserIDContains)) + } + if i.UserIDHasPrefix != nil { + predicates = append(predicates, friendship.UserIDHasPrefix(*i.UserIDHasPrefix)) + } + if i.UserIDHasSuffix != nil { + predicates = append(predicates, friendship.UserIDHasSuffix(*i.UserIDHasSuffix)) + } + if i.UserIDEqualFold != nil { + predicates = append(predicates, friendship.UserIDEqualFold(*i.UserIDEqualFold)) + } + if i.UserIDContainsFold != nil { + predicates = append(predicates, friendship.UserIDContainsFold(*i.UserIDContainsFold)) + } + if i.FriendID != nil { + predicates = append(predicates, friendship.FriendIDEQ(*i.FriendID)) + } + if i.FriendIDNEQ != nil { + predicates = append(predicates, friendship.FriendIDNEQ(*i.FriendIDNEQ)) + } + if len(i.FriendIDIn) > 0 { + predicates = append(predicates, friendship.FriendIDIn(i.FriendIDIn...)) + } + if len(i.FriendIDNotIn) > 0 { + predicates = append(predicates, friendship.FriendIDNotIn(i.FriendIDNotIn...)) + } + if i.FriendIDGT != nil { + predicates = append(predicates, friendship.FriendIDGT(*i.FriendIDGT)) + } + if i.FriendIDGTE != nil { + predicates = append(predicates, friendship.FriendIDGTE(*i.FriendIDGTE)) + } + if i.FriendIDLT != nil { + predicates = append(predicates, friendship.FriendIDLT(*i.FriendIDLT)) + } + if i.FriendIDLTE != nil { + predicates = append(predicates, friendship.FriendIDLTE(*i.FriendIDLTE)) + } + if i.FriendIDContains != nil { + predicates = append(predicates, friendship.FriendIDContains(*i.FriendIDContains)) + } + if i.FriendIDHasPrefix != nil { + predicates = append(predicates, friendship.FriendIDHasPrefix(*i.FriendIDHasPrefix)) + } + if i.FriendIDHasSuffix != nil { + predicates = append(predicates, friendship.FriendIDHasSuffix(*i.FriendIDHasSuffix)) + } + if i.FriendIDEqualFold != nil { + predicates = append(predicates, friendship.FriendIDEqualFold(*i.FriendIDEqualFold)) + } + if i.FriendIDContainsFold != nil { + predicates = append(predicates, friendship.FriendIDContainsFold(*i.FriendIDContainsFold)) + } + + if i.HasUser != nil { + p := friendship.HasUser() + if !*i.HasUser { + p = friendship.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasUserWith) > 0 { + with := make([]predicate.User, 0, len(i.HasUserWith)) + for _, w := range i.HasUserWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasUserWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, friendship.HasUserWith(with...)) + } + if i.HasFriend != nil { + p := friendship.HasFriend() + if !*i.HasFriend { + p = friendship.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasFriendWith) > 0 { + with := make([]predicate.User, 0, len(i.HasFriendWith)) + for _, w := range i.HasFriendWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasFriendWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, friendship.HasFriendWith(with...)) + } + switch len(predicates) { + case 0: + return nil, ErrEmptyFriendshipWhereInput + case 1: + return predicates[0], nil + default: + return friendship.And(predicates...), nil + } +} + // GroupWhereInput represents a where input for filtering Group queries. type GroupWhereInput struct { Predicates []predicate.Group `json:"-"` @@ -1005,6 +1316,10 @@ type UserWhereInput struct { // "friends" edge predicates. HasFriends *bool `json:"hasFriends,omitempty"` HasFriendsWith []*UserWhereInput `json:"hasFriendsWith,omitempty"` + + // "friendships" edge predicates. + HasFriendships *bool `json:"hasFriendships,omitempty"` + HasFriendshipsWith []*FriendshipWhereInput `json:"hasFriendshipsWith,omitempty"` } // AddPredicates adds custom predicates to the where input to be used during the filtering phase. @@ -1178,6 +1493,24 @@ func (i *UserWhereInput) P() (predicate.User, error) { } predicates = append(predicates, user.HasFriendsWith(with...)) } + if i.HasFriendships != nil { + p := user.HasFriendships() + if !*i.HasFriendships { + p = user.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasFriendshipsWith) > 0 { + with := make([]predicate.Friendship, 0, len(i.HasFriendshipsWith)) + for _, w := range i.HasFriendshipsWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasFriendshipsWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, user.HasFriendshipsWith(with...)) + } switch len(predicates) { case 0: return nil, ErrEmptyUserWhereInput diff --git a/entgql/internal/todopulid/ent/migrate/schema.go b/entgql/internal/todopulid/ent/migrate/schema.go index 0033685b7..3cfe19ea3 100644 --- a/entgql/internal/todopulid/ent/migrate/schema.go +++ b/entgql/internal/todopulid/ent/migrate/schema.go @@ -40,7 +40,7 @@ var ( } // FriendshipsColumns holds the columns for the "friendships" table. FriendshipsColumns = []*schema.Column{ - {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "id", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "user_id", Type: field.TypeString}, {Name: "friend_id", Type: field.TypeString}, diff --git a/entgql/internal/todopulid/ent/mutation.go b/entgql/internal/todopulid/ent/mutation.go index 21bd9dcfc..216d179a3 100644 --- a/entgql/internal/todopulid/ent/mutation.go +++ b/entgql/internal/todopulid/ent/mutation.go @@ -888,7 +888,7 @@ type FriendshipMutation struct { config op Op typ string - id *int + id *pulid.ID created_at *time.Time clearedFields map[string]struct{} user *pulid.ID @@ -920,7 +920,7 @@ func newFriendshipMutation(c config, op Op, opts ...friendshipOption) *Friendshi } // withFriendshipID sets the ID field of the mutation. -func withFriendshipID(id int) friendshipOption { +func withFriendshipID(id pulid.ID) friendshipOption { return func(m *FriendshipMutation) { var ( err error @@ -970,9 +970,15 @@ func (m FriendshipMutation) Tx() (*Tx, error) { return tx, nil } +// SetID sets the value of the id field. Note that this +// operation is only accepted on creation of Friendship entities. +func (m *FriendshipMutation) SetID(id pulid.ID) { + m.id = &id +} + // ID returns the ID value in the mutation. Note that the ID is only available // if it was provided to the builder or after it was returned from the database. -func (m *FriendshipMutation) ID() (id int, exists bool) { +func (m *FriendshipMutation) ID() (id pulid.ID, exists bool) { if m.id == nil { return } @@ -983,12 +989,12 @@ func (m *FriendshipMutation) ID() (id int, exists bool) { // That means, if the mutation is applied within a transaction with an isolation level such // as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated // or updated by the mutation. -func (m *FriendshipMutation) IDs(ctx context.Context) ([]int, error) { +func (m *FriendshipMutation) IDs(ctx context.Context) ([]pulid.ID, error) { switch { case m.op.Is(OpUpdateOne | OpDeleteOne): id, exists := m.ID() if exists { - return []int{id}, nil + return []pulid.ID{id}, nil } fallthrough case m.op.Is(OpUpdate | OpDelete): @@ -2746,8 +2752,8 @@ type UserMutation struct { friends map[pulid.ID]struct{} removedfriends map[pulid.ID]struct{} clearedfriends bool - friendships map[int]struct{} - removedfriendships map[int]struct{} + friendships map[pulid.ID]struct{} + removedfriendships map[pulid.ID]struct{} clearedfriendships bool done bool oldValue func(context.Context) (*User, error) @@ -3003,9 +3009,9 @@ func (m *UserMutation) ResetFriends() { } // AddFriendshipIDs adds the "friendships" edge to the Friendship entity by ids. -func (m *UserMutation) AddFriendshipIDs(ids ...int) { +func (m *UserMutation) AddFriendshipIDs(ids ...pulid.ID) { if m.friendships == nil { - m.friendships = make(map[int]struct{}) + m.friendships = make(map[pulid.ID]struct{}) } for i := range ids { m.friendships[ids[i]] = struct{}{} @@ -3023,9 +3029,9 @@ func (m *UserMutation) FriendshipsCleared() bool { } // RemoveFriendshipIDs removes the "friendships" edge to the Friendship entity by IDs. -func (m *UserMutation) RemoveFriendshipIDs(ids ...int) { +func (m *UserMutation) RemoveFriendshipIDs(ids ...pulid.ID) { if m.removedfriendships == nil { - m.removedfriendships = make(map[int]struct{}) + m.removedfriendships = make(map[pulid.ID]struct{}) } for i := range ids { delete(m.friendships, ids[i]) @@ -3034,7 +3040,7 @@ func (m *UserMutation) RemoveFriendshipIDs(ids ...int) { } // RemovedFriendships returns the removed IDs of the "friendships" edge to the Friendship entity. -func (m *UserMutation) RemovedFriendshipsIDs() (ids []int) { +func (m *UserMutation) RemovedFriendshipsIDs() (ids []pulid.ID) { for id := range m.removedfriendships { ids = append(ids, id) } @@ -3042,7 +3048,7 @@ func (m *UserMutation) RemovedFriendshipsIDs() (ids []int) { } // FriendshipsIDs returns the "friendships" edge IDs in the mutation. -func (m *UserMutation) FriendshipsIDs() (ids []int) { +func (m *UserMutation) FriendshipsIDs() (ids []pulid.ID) { for id := range m.friendships { ids = append(ids, id) } diff --git a/entgql/internal/todopulid/ent/runtime.go b/entgql/internal/todopulid/ent/runtime.go index 507e3ffd4..867523a8b 100644 --- a/entgql/internal/todopulid/ent/runtime.go +++ b/entgql/internal/todopulid/ent/runtime.go @@ -48,12 +48,19 @@ func init() { categoryDescID := categoryMixinFields0[0].Descriptor() // category.DefaultID holds the default value on creation for the id field. category.DefaultID = categoryDescID.Default.(func() pulid.ID) + friendshipMixin := schema.Friendship{}.Mixin() + friendshipMixinFields0 := friendshipMixin[0].Fields() + _ = friendshipMixinFields0 friendshipFields := schema.Friendship{}.Fields() _ = friendshipFields // friendshipDescCreatedAt is the schema descriptor for created_at field. friendshipDescCreatedAt := friendshipFields[0].Descriptor() // friendship.DefaultCreatedAt holds the default value on creation for the created_at field. friendship.DefaultCreatedAt = friendshipDescCreatedAt.Default.(func() time.Time) + // friendshipDescID is the schema descriptor for id field. + friendshipDescID := friendshipMixinFields0[0].Descriptor() + // friendship.DefaultID holds the default value on creation for the id field. + friendship.DefaultID = friendshipDescID.Default.(func() pulid.ID) groupMixin := schema.Group{}.Mixin() groupMixinFields0 := groupMixin[0].Fields() _ = groupMixinFields0 diff --git a/entgql/internal/todopulid/ent/schema/friendship.go b/entgql/internal/todopulid/ent/schema/friendship.go index e2e631b4c..5d61dd865 100644 --- a/entgql/internal/todopulid/ent/schema/friendship.go +++ b/entgql/internal/todopulid/ent/schema/friendship.go @@ -28,6 +28,13 @@ type Friendship struct { ent.Schema } +// Mixin returns user mixed-in schema. +func (Friendship) Mixin() []ent.Mixin { + return []ent.Mixin{ + pulid.MixinWithPrefix("FS"), + } +} + // Fields of the Friendship. func (Friendship) Fields() []ent.Field { return []ent.Field{ diff --git a/entgql/internal/todopulid/ent/user.go b/entgql/internal/todopulid/ent/user.go index 48b79ab0d..7ba6a9872 100644 --- a/entgql/internal/todopulid/ent/user.go +++ b/entgql/internal/todopulid/ent/user.go @@ -49,7 +49,7 @@ type UserEdges struct { // type was loaded (or requested) in eager-loading or not. loadedTypes [3]bool // totalCount holds the count of the edges above. - totalCount [2]*int + totalCount [3]*int } // GroupsOrErr returns the Groups value or an error if the edge diff --git a/entgql/internal/todopulid/ent/user_create.go b/entgql/internal/todopulid/ent/user_create.go index 4fa662e07..40e1ce566 100644 --- a/entgql/internal/todopulid/ent/user_create.go +++ b/entgql/internal/todopulid/ent/user_create.go @@ -95,14 +95,14 @@ func (uc *UserCreate) AddFriends(u ...*User) *UserCreate { } // AddFriendshipIDs adds the "friendships" edge to the Friendship entity by IDs. -func (uc *UserCreate) AddFriendshipIDs(ids ...int) *UserCreate { +func (uc *UserCreate) AddFriendshipIDs(ids ...pulid.ID) *UserCreate { uc.mutation.AddFriendshipIDs(ids...) return uc } // AddFriendships adds the "friendships" edges to the Friendship entity. func (uc *UserCreate) AddFriendships(f ...*Friendship) *UserCreate { - ids := make([]int, len(f)) + ids := make([]pulid.ID, len(f)) for i := range f { ids[i] = f[i].ID } @@ -296,7 +296,7 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, diff --git a/entgql/internal/todopulid/ent/user_update.go b/entgql/internal/todopulid/ent/user_update.go index d0ef7fbf1..7ded6c8b3 100644 --- a/entgql/internal/todopulid/ent/user_update.go +++ b/entgql/internal/todopulid/ent/user_update.go @@ -89,14 +89,14 @@ func (uu *UserUpdate) AddFriends(u ...*User) *UserUpdate { } // AddFriendshipIDs adds the "friendships" edge to the Friendship entity by IDs. -func (uu *UserUpdate) AddFriendshipIDs(ids ...int) *UserUpdate { +func (uu *UserUpdate) AddFriendshipIDs(ids ...pulid.ID) *UserUpdate { uu.mutation.AddFriendshipIDs(ids...) return uu } // AddFriendships adds the "friendships" edges to the Friendship entity. func (uu *UserUpdate) AddFriendships(f ...*Friendship) *UserUpdate { - ids := make([]int, len(f)) + ids := make([]pulid.ID, len(f)) for i := range f { ids[i] = f[i].ID } @@ -157,14 +157,14 @@ func (uu *UserUpdate) ClearFriendships() *UserUpdate { } // RemoveFriendshipIDs removes the "friendships" edge to Friendship entities by IDs. -func (uu *UserUpdate) RemoveFriendshipIDs(ids ...int) *UserUpdate { +func (uu *UserUpdate) RemoveFriendshipIDs(ids ...pulid.ID) *UserUpdate { uu.mutation.RemoveFriendshipIDs(ids...) return uu } // RemoveFriendships removes "friendships" edges to Friendship entities. func (uu *UserUpdate) RemoveFriendships(f ...*Friendship) *UserUpdate { - ids := make([]int, len(f)) + ids := make([]pulid.ID, len(f)) for i := range f { ids[i] = f[i].ID } @@ -379,7 +379,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, @@ -395,7 +395,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, @@ -414,7 +414,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, @@ -488,14 +488,14 @@ func (uuo *UserUpdateOne) AddFriends(u ...*User) *UserUpdateOne { } // AddFriendshipIDs adds the "friendships" edge to the Friendship entity by IDs. -func (uuo *UserUpdateOne) AddFriendshipIDs(ids ...int) *UserUpdateOne { +func (uuo *UserUpdateOne) AddFriendshipIDs(ids ...pulid.ID) *UserUpdateOne { uuo.mutation.AddFriendshipIDs(ids...) return uuo } // AddFriendships adds the "friendships" edges to the Friendship entity. func (uuo *UserUpdateOne) AddFriendships(f ...*Friendship) *UserUpdateOne { - ids := make([]int, len(f)) + ids := make([]pulid.ID, len(f)) for i := range f { ids[i] = f[i].ID } @@ -556,14 +556,14 @@ func (uuo *UserUpdateOne) ClearFriendships() *UserUpdateOne { } // RemoveFriendshipIDs removes the "friendships" edge to Friendship entities by IDs. -func (uuo *UserUpdateOne) RemoveFriendshipIDs(ids ...int) *UserUpdateOne { +func (uuo *UserUpdateOne) RemoveFriendshipIDs(ids ...pulid.ID) *UserUpdateOne { uuo.mutation.RemoveFriendshipIDs(ids...) return uuo } // RemoveFriendships removes "friendships" edges to Friendship entities. func (uuo *UserUpdateOne) RemoveFriendships(f ...*Friendship) *UserUpdateOne { - ids := make([]int, len(f)) + ids := make([]pulid.ID, len(f)) for i := range f { ids[i] = f[i].ID } @@ -808,7 +808,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, @@ -824,7 +824,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, @@ -843,7 +843,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeString, Column: friendship.FieldID, }, }, diff --git a/entgql/internal/todopulid/generated.go b/entgql/internal/todopulid/generated.go index 5c09213e5..cd73b1bce 100644 --- a/entgql/internal/todopulid/generated.go +++ b/entgql/internal/todopulid/generated.go @@ -68,6 +68,15 @@ type ComplexityRoot struct { MaxMembers func(childComplexity int) int } + Friendship struct { + CreatedAt func(childComplexity int) int + Friend func(childComplexity int) int + FriendID func(childComplexity int) int + ID func(childComplexity int) int + User func(childComplexity int) int + UserID func(childComplexity int) int + } + Group struct { ID func(childComplexity int) int Name func(childComplexity int) int @@ -130,10 +139,11 @@ type ComplexityRoot struct { } User struct { - Friends func(childComplexity int) int - Groups func(childComplexity int, after *ent.Cursor, first *int, before *ent.Cursor, last *int, where *ent.GroupWhereInput) int - ID func(childComplexity int) int - Name func(childComplexity int) int + Friends func(childComplexity int) int + Friendships func(childComplexity int) int + Groups func(childComplexity int, after *ent.Cursor, first *int, before *ent.Cursor, last *int, where *ent.GroupWhereInput) int + ID func(childComplexity int) int + Name func(childComplexity int) int } UserConnection struct { @@ -259,6 +269,48 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.CategoryConfig.MaxMembers(childComplexity), true + case "Friendship.createdAt": + if e.complexity.Friendship.CreatedAt == nil { + break + } + + return e.complexity.Friendship.CreatedAt(childComplexity), true + + case "Friendship.friend": + if e.complexity.Friendship.Friend == nil { + break + } + + return e.complexity.Friendship.Friend(childComplexity), true + + case "Friendship.friendID": + if e.complexity.Friendship.FriendID == nil { + break + } + + return e.complexity.Friendship.FriendID(childComplexity), true + + case "Friendship.id": + if e.complexity.Friendship.ID == nil { + break + } + + return e.complexity.Friendship.ID(childComplexity), true + + case "Friendship.user": + if e.complexity.Friendship.User == nil { + break + } + + return e.complexity.Friendship.User(childComplexity), true + + case "Friendship.userID": + if e.complexity.Friendship.UserID == nil { + break + } + + return e.complexity.Friendship.UserID(childComplexity), true + case "Group.id": if e.complexity.Group.ID == nil { break @@ -544,6 +596,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.User.Friends(childComplexity), true + case "User.friendships": + if e.complexity.User.Friendships == nil { + break + } + + return e.complexity.User.Friendships(childComplexity), true + case "User.groups": if e.complexity.User.Groups == nil { break @@ -617,9 +676,12 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler { ec.unmarshalInputCategoryOrder, ec.unmarshalInputCategoryWhereInput, ec.unmarshalInputCreateTodoInput, + ec.unmarshalInputCreateUserInput, + ec.unmarshalInputFriendshipWhereInput, ec.unmarshalInputGroupWhereInput, ec.unmarshalInputTodoOrder, ec.unmarshalInputTodoWhereInput, + ec.unmarshalInputUpdateUserInput, ec.unmarshalInputUserWhereInput, ) first := true @@ -843,10 +905,54 @@ input CreateTodoInput { secretID: ID } """ +CreateUserInput is used for create User object. +Input was generated by ent. +""" +input CreateUserInput { + name: String + groupIDs: [ID!] + friendIDs: [ID!] +} +""" Define a Relay Cursor type: https://relay.dev/graphql/connections.htm#sec-Cursor """ scalar Cursor +type Friendship implements Node { + id: ID! + createdAt: Time! + userID: ID! + friendID: ID! + user: User! + friend: User! +} +""" +FriendshipWhereInput is used for filtering Friendship objects. +Input was generated by ent. +""" +input FriendshipWhereInput { + not: FriendshipWhereInput + and: [FriendshipWhereInput!] + or: [FriendshipWhereInput!] + """id field predicates""" + id: ID + idNEQ: ID + idIn: [ID!] + idNotIn: [ID!] + idGT: ID + idGTE: ID + idLT: ID + idLTE: ID + """created_at field predicates""" + createdAt: Time + createdAtNEQ: Time + createdAtIn: [Time!] + createdAtNotIn: [Time!] + createdAtGT: Time + createdAtGTE: Time + createdAtLT: Time + createdAtLTE: Time +} type Group implements Node { id: ID! name: String! @@ -1145,6 +1251,17 @@ input TodoWhereInput { hasCategory: Boolean hasCategoryWith: [CategoryWhereInput!] } +""" +UpdateUserInput is used for update User object. +Input was generated by ent. +""" +input UpdateUserInput { + name: String + addGroupIDs: [ID!] + removeGroupIDs: [ID!] + addFriendIDs: [ID!] + removeFriendIDs: [ID!] +} type User implements Node { id: ID! name: String! @@ -1165,6 +1282,7 @@ type User implements Node { where: GroupWhereInput ): GroupConnection! friends: [User!] + friendships: [Friendship!] } """A connection to a list of items.""" type UserConnection { @@ -1219,6 +1337,9 @@ input UserWhereInput { """friends edge predicates""" hasFriends: Boolean hasFriendsWith: [UserWhereInput!] + """friendships edge predicates""" + hasFriendships: Boolean + hasFriendshipsWith: [FriendshipWhereInput!] } `, BuiltIn: false}, } @@ -2073,8 +2194,281 @@ func (ec *executionContext) fieldContext_Category_todos(ctx context.Context, fie return fc, nil } -func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField, obj *schematype.CategoryConfig) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_CategoryConfig_maxMembers(ctx, field) +func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField, obj *schematype.CategoryConfig) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_CategoryConfig_maxMembers(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.MaxMembers, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(int) + fc.Result = res + return ec.marshalOInt2int(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "CategoryConfig", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Int does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_id(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_id(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.ID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(pulid.ID) + fc.Result = res + return ec.marshalNID2entgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_id(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_createdAt(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_createdAt(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.CreatedAt, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(time.Time) + fc.Result = res + return ec.marshalNTime2timeᚐTime(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_createdAt(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Time does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_userID(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_userID(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.UserID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(pulid.ID) + fc.Result = res + return ec.marshalNID2entgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_userID(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_friendID(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_friendID(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.FriendID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(pulid.ID) + fc.Result = res + return ec.marshalNID2entgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_friendID(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_user(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_user(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.User(ctx) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(*ent.User) + fc.Result = res + return ec.marshalNUser2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐUser(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_user(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: true, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "id": + return ec.fieldContext_User_id(ctx, field) + case "name": + return ec.fieldContext_User_name(ctx, field) + case "groups": + return ec.fieldContext_User_groups(ctx, field) + case "friends": + return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type User", field.Name) + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_friend(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_friend(ctx, field) if err != nil { return graphql.Null } @@ -2087,28 +2481,43 @@ func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, fiel }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.MaxMembers, nil + return obj.Friend(ctx) }) if err != nil { ec.Error(ctx, err) return graphql.Null } if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } return graphql.Null } - res := resTmp.(int) + res := resTmp.(*ent.User) fc.Result = res - return ec.marshalOInt2int(ctx, field.Selections, res) + return ec.marshalNUser2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐUser(ctx, field.Selections, res) } -func (ec *executionContext) fieldContext_CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { +func (ec *executionContext) fieldContext_Friendship_friend(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { fc = &graphql.FieldContext{ - Object: "CategoryConfig", + Object: "Friendship", Field: field, - IsMethod: false, + IsMethod: true, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type Int does not have child fields") + switch field.Name { + case "id": + return ec.fieldContext_User_id(ctx, field) + case "name": + return ec.fieldContext_User_name(ctx, field) + case "groups": + return ec.fieldContext_User_groups(ctx, field) + case "friends": + return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, } return fc, nil @@ -4150,6 +4559,8 @@ func (ec *executionContext) fieldContext_User_friends(ctx context.Context, field return ec.fieldContext_User_groups(ctx, field) case "friends": return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, @@ -4157,6 +4568,61 @@ func (ec *executionContext) fieldContext_User_friends(ctx context.Context, field return fc, nil } +func (ec *executionContext) _User_friendships(ctx context.Context, field graphql.CollectedField, obj *ent.User) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_User_friendships(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Friendships(ctx) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]*ent.Friendship) + fc.Result = res + return ec.marshalOFriendship2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendshipᚄ(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_User_friendships(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "User", + Field: field, + IsMethod: true, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "id": + return ec.fieldContext_Friendship_id(ctx, field) + case "createdAt": + return ec.fieldContext_Friendship_createdAt(ctx, field) + case "userID": + return ec.fieldContext_Friendship_userID(ctx, field) + case "friendID": + return ec.fieldContext_Friendship_friendID(ctx, field) + case "user": + return ec.fieldContext_Friendship_user(ctx, field) + case "friend": + return ec.fieldContext_Friendship_friend(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type Friendship", field.Name) + }, + } + return fc, nil +} + func (ec *executionContext) _UserConnection_edges(ctx context.Context, field graphql.CollectedField, obj *ent.UserConnection) (ret graphql.Marshaler) { fc, err := ec.fieldContext_UserConnection_edges(ctx, field) if err != nil { @@ -4346,6 +4812,8 @@ func (ec *executionContext) fieldContext_UserEdge_node(ctx context.Context, fiel return ec.fieldContext_User_groups(ctx, field) case "friends": return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, @@ -6751,43 +7219,249 @@ func (ec *executionContext) unmarshalInputCreateTodoInput(ctx context.Context, o if err != nil { return it, err } - case "text": + case "text": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("text")) + it.Text, err = ec.unmarshalNString2string(ctx, v) + if err != nil { + return it, err + } + case "parentID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("parentID")) + it.ParentID, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + if err != nil { + return it, err + } + case "childIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("childIDs")) + it.ChildIDs, err = ec.unmarshalOID2ᚕentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "categoryID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("categoryID")) + it.CategoryID, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + if err != nil { + return it, err + } + case "secretID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("secretID")) + it.SecretID, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + +func (ec *executionContext) unmarshalInputCreateUserInput(ctx context.Context, obj interface{}) (ent.CreateUserInput, error) { + var it ent.CreateUserInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "name": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name")) + it.Name, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "groupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("groupIDs")) + it.GroupIDs, err = ec.unmarshalOID2ᚕentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "friendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("friendIDs")) + it.FriendIDs, err = ec.unmarshalOID2ᚕentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐIDᚄ(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + +func (ec *executionContext) unmarshalInputFriendshipWhereInput(ctx context.Context, obj interface{}) (ent.FriendshipWhereInput, error) { + var it ent.FriendshipWhereInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "not": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("not")) + it.Not, err = ec.unmarshalOFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendshipWhereInput(ctx, v) + if err != nil { + return it, err + } + case "and": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("and")) + it.And, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } + case "or": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("or")) + it.Or, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } + case "id": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("id")) + it.ID, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + if err != nil { + return it, err + } + case "idNEQ": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idNEQ")) + it.IDNEQ, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + if err != nil { + return it, err + } + case "idIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idIn")) + it.IDIn, err = ec.unmarshalOID2ᚕentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "idNotIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idNotIn")) + it.IDNotIn, err = ec.unmarshalOID2ᚕentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "idGT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idGT")) + it.IDGT, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + if err != nil { + return it, err + } + case "idGTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idGTE")) + it.IDGTE, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + if err != nil { + return it, err + } + case "idLT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idLT")) + it.IDLT, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + if err != nil { + return it, err + } + case "idLTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idLTE")) + it.IDLTE, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + if err != nil { + return it, err + } + case "createdAt": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAt")) + it.CreatedAt, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + case "createdAtNEQ": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtNEQ")) + it.CreatedAtNEQ, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + case "createdAtIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtIn")) + it.CreatedAtIn, err = ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) + if err != nil { + return it, err + } + case "createdAtNotIn": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("text")) - it.Text, err = ec.unmarshalNString2string(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtNotIn")) + it.CreatedAtNotIn, err = ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) if err != nil { return it, err } - case "parentID": + case "createdAtGT": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("parentID")) - it.ParentID, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtGT")) + it.CreatedAtGT, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "childIDs": + case "createdAtGTE": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("childIDs")) - it.ChildIDs, err = ec.unmarshalOID2ᚕentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐIDᚄ(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtGTE")) + it.CreatedAtGTE, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "categoryID": + case "createdAtLT": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("categoryID")) - it.CategoryID, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtLT")) + it.CreatedAtLT, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "secretID": + case "createdAtLTE": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("secretID")) - it.SecretID, err = ec.unmarshalOID2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐID(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtLTE")) + it.CreatedAtLTE, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } @@ -7541,6 +8215,61 @@ func (ec *executionContext) unmarshalInputTodoWhereInput(ctx context.Context, ob return it, nil } +func (ec *executionContext) unmarshalInputUpdateUserInput(ctx context.Context, obj interface{}) (ent.UpdateUserInput, error) { + var it ent.UpdateUserInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "name": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name")) + it.Name, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "addGroupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("addGroupIDs")) + it.AddGroupIDs, err = ec.unmarshalOID2ᚕentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "removeGroupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("removeGroupIDs")) + it.RemoveGroupIDs, err = ec.unmarshalOID2ᚕentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "addFriendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("addFriendIDs")) + it.AddFriendIDs, err = ec.unmarshalOID2ᚕentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "removeFriendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("removeFriendIDs")) + it.RemoveFriendIDs, err = ec.unmarshalOID2ᚕentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚋschemaᚋpulidᚐIDᚄ(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + func (ec *executionContext) unmarshalInputUserWhereInput(ctx context.Context, obj interface{}) (ent.UserWhereInput, error) { var it ent.UserWhereInput asMap := map[string]interface{}{} @@ -7774,6 +8503,22 @@ func (ec *executionContext) unmarshalInputUserWhereInput(ctx context.Context, ob if err != nil { return it, err } + case "hasFriendships": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("hasFriendships")) + it.HasFriendships, err = ec.unmarshalOBoolean2ᚖbool(ctx, v) + if err != nil { + return it, err + } + case "hasFriendshipsWith": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("hasFriendshipsWith")) + it.HasFriendshipsWith, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } } } @@ -7793,6 +8538,11 @@ func (ec *executionContext) _Node(ctx context.Context, sel ast.SelectionSet, obj return graphql.Null } return ec._Category(ctx, sel, obj) + case *ent.Friendship: + if obj == nil { + return graphql.Null + } + return ec._Friendship(ctx, sel, obj) case *ent.Group: if obj == nil { return graphql.Null @@ -7920,6 +8670,95 @@ func (ec *executionContext) _CategoryConfig(ctx context.Context, sel ast.Selecti return out } +var friendshipImplementors = []string{"Friendship", "Node"} + +func (ec *executionContext) _Friendship(ctx context.Context, sel ast.SelectionSet, obj *ent.Friendship) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, friendshipImplementors) + out := graphql.NewFieldSet(fields) + var invalids uint32 + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("Friendship") + case "id": + + out.Values[i] = ec._Friendship_id(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "createdAt": + + out.Values[i] = ec._Friendship_createdAt(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "userID": + + out.Values[i] = ec._Friendship_userID(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "friendID": + + out.Values[i] = ec._Friendship_friendID(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "user": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Friendship_user(ctx, field, obj) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + return res + } + + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + case "friend": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Friendship_friend(ctx, field, obj) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + return res + } + + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch() + if invalids > 0 { + return graphql.Null + } + return out +} + var groupImplementors = []string{"Group", "Node"} func (ec *executionContext) _Group(ctx context.Context, sel ast.SelectionSet, obj *ent.Group) graphql.Marshaler { @@ -8569,6 +9408,23 @@ func (ec *executionContext) _User(ctx context.Context, sel ast.SelectionSet, obj return res } + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + case "friendships": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._User_friendships(ctx, field, obj) + return res + } + out.Concurrently(i, func() graphql.Marshaler { return innerFunc(ctx) @@ -9054,6 +9910,21 @@ func (ec *executionContext) marshalNDuration2timeᚐDuration(ctx context.Context return res } +func (ec *executionContext) marshalNFriendship2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendship(ctx context.Context, sel ast.SelectionSet, v *ent.Friendship) graphql.Marshaler { + if v == nil { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + return graphql.Null + } + return ec._Friendship(ctx, sel, v) +} + +func (ec *executionContext) unmarshalNFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendshipWhereInput(ctx context.Context, v interface{}) (*ent.FriendshipWhereInput, error) { + res, err := ec.unmarshalInputFriendshipWhereInput(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) marshalNGroupConnection2entgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐGroupConnection(ctx context.Context, sel ast.SelectionSet, v ent.GroupConnection) graphql.Marshaler { return ec._GroupConnection(ctx, sel, &v) } @@ -9827,6 +10698,81 @@ func (ec *executionContext) marshalODuration2ᚖtimeᚐDuration(ctx context.Cont return res } +func (ec *executionContext) marshalOFriendship2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendshipᚄ(ctx context.Context, sel ast.SelectionSet, v []*ent.Friendship) graphql.Marshaler { + if v == nil { + return graphql.Null + } + ret := make(graphql.Array, len(v)) + var wg sync.WaitGroup + isLen1 := len(v) == 1 + if !isLen1 { + wg.Add(len(v)) + } + for i := range v { + i := i + fc := &graphql.FieldContext{ + Index: &i, + Result: &v[i], + } + ctx := graphql.WithFieldContext(ctx, fc) + f := func(i int) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = nil + } + }() + if !isLen1 { + defer wg.Done() + } + ret[i] = ec.marshalNFriendship2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendship(ctx, sel, v[i]) + } + if isLen1 { + f(i) + } else { + go f(i) + } + + } + wg.Wait() + + for _, e := range ret { + if e == graphql.Null { + return graphql.Null + } + } + + return ret +} + +func (ec *executionContext) unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendshipWhereInputᚄ(ctx context.Context, v interface{}) ([]*ent.FriendshipWhereInput, error) { + if v == nil { + return nil, nil + } + var vSlice []interface{} + if v != nil { + vSlice = graphql.CoerceList(v) + } + var err error + res := make([]*ent.FriendshipWhereInput, len(vSlice)) + for i := range vSlice { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithIndex(i)) + res[i], err = ec.unmarshalNFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendshipWhereInput(ctx, vSlice[i]) + if err != nil { + return nil, err + } + } + return res, nil +} + +func (ec *executionContext) unmarshalOFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐFriendshipWhereInput(ctx context.Context, v interface{}) (*ent.FriendshipWhereInput, error) { + if v == nil { + return nil, nil + } + res, err := ec.unmarshalInputFriendshipWhereInput(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) marshalOGroup2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodopulidᚋentᚐGroup(ctx context.Context, sel ast.SelectionSet, v *ent.Group) graphql.Marshaler { if v == nil { return graphql.Null diff --git a/entgql/internal/todouuid/ent/client.go b/entgql/internal/todouuid/ent/client.go index 4d88602ab..5b83666b2 100644 --- a/entgql/internal/todouuid/ent/client.go +++ b/entgql/internal/todouuid/ent/client.go @@ -319,7 +319,7 @@ func (c *FriendshipClient) UpdateOne(f *Friendship) *FriendshipUpdateOne { } // UpdateOneID returns an update builder for the given id. -func (c *FriendshipClient) UpdateOneID(id int) *FriendshipUpdateOne { +func (c *FriendshipClient) UpdateOneID(id uuid.UUID) *FriendshipUpdateOne { mutation := newFriendshipMutation(c.config, OpUpdateOne, withFriendshipID(id)) return &FriendshipUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} } @@ -336,7 +336,7 @@ func (c *FriendshipClient) DeleteOne(f *Friendship) *FriendshipDeleteOne { } // DeleteOne returns a builder for deleting the given entity by its id. -func (c *FriendshipClient) DeleteOneID(id int) *FriendshipDeleteOne { +func (c *FriendshipClient) DeleteOneID(id uuid.UUID) *FriendshipDeleteOne { builder := c.Delete().Where(friendship.ID(id)) builder.mutation.id = &id builder.mutation.op = OpDeleteOne @@ -351,12 +351,12 @@ func (c *FriendshipClient) Query() *FriendshipQuery { } // Get returns a Friendship entity by its id. -func (c *FriendshipClient) Get(ctx context.Context, id int) (*Friendship, error) { +func (c *FriendshipClient) Get(ctx context.Context, id uuid.UUID) (*Friendship, error) { return c.Query().Where(friendship.ID(id)).Only(ctx) } // GetX is like Get, but panics if an error occurs. -func (c *FriendshipClient) GetX(ctx context.Context, id int) *Friendship { +func (c *FriendshipClient) GetX(ctx context.Context, id uuid.UUID) *Friendship { obj, err := c.Get(ctx, id) if err != nil { panic(err) diff --git a/entgql/internal/todouuid/ent/friendship.go b/entgql/internal/todouuid/ent/friendship.go index 1a5e584fe..881241847 100644 --- a/entgql/internal/todouuid/ent/friendship.go +++ b/entgql/internal/todouuid/ent/friendship.go @@ -31,7 +31,7 @@ import ( type Friendship struct { config `json:"-"` // ID of the ent. - ID int `json:"id,omitempty"` + ID uuid.UUID `json:"id,omitempty"` // CreatedAt holds the value of the "created_at" field. CreatedAt time.Time `json:"created_at,omitempty"` // UserID holds the value of the "user_id" field. @@ -89,11 +89,9 @@ func (*Friendship) scanValues(columns []string) ([]interface{}, error) { values := make([]interface{}, len(columns)) for i := range columns { switch columns[i] { - case friendship.FieldID: - values[i] = new(sql.NullInt64) case friendship.FieldCreatedAt: values[i] = new(sql.NullTime) - case friendship.FieldUserID, friendship.FieldFriendID: + case friendship.FieldID, friendship.FieldUserID, friendship.FieldFriendID: values[i] = new(uuid.UUID) default: return nil, fmt.Errorf("unexpected column %q for type Friendship", columns[i]) @@ -111,11 +109,11 @@ func (f *Friendship) assignValues(columns []string, values []interface{}) error for i := range columns { switch columns[i] { case friendship.FieldID: - value, ok := values[i].(*sql.NullInt64) - if !ok { - return fmt.Errorf("unexpected type %T for field id", value) + if value, ok := values[i].(*uuid.UUID); !ok { + return fmt.Errorf("unexpected type %T for field id", values[i]) + } else if value != nil { + f.ID = *value } - f.ID = int(value.Int64) case friendship.FieldCreatedAt: if value, ok := values[i].(*sql.NullTime); !ok { return fmt.Errorf("unexpected type %T for field created_at", values[i]) diff --git a/entgql/internal/todouuid/ent/friendship/friendship.go b/entgql/internal/todouuid/ent/friendship/friendship.go index d6420dffe..558df264f 100644 --- a/entgql/internal/todouuid/ent/friendship/friendship.go +++ b/entgql/internal/todouuid/ent/friendship/friendship.go @@ -18,6 +18,8 @@ package friendship import ( "time" + + "github.com/google/uuid" ) const ( @@ -74,4 +76,6 @@ func ValidColumn(column string) bool { var ( // DefaultCreatedAt holds the default value on creation for the "created_at" field. DefaultCreatedAt func() time.Time + // DefaultID holds the default value on creation for the "id" field. + DefaultID func() uuid.UUID ) diff --git a/entgql/internal/todouuid/ent/friendship/where.go b/entgql/internal/todouuid/ent/friendship/where.go index 8d21a37bb..a8d3f8674 100644 --- a/entgql/internal/todouuid/ent/friendship/where.go +++ b/entgql/internal/todouuid/ent/friendship/where.go @@ -26,28 +26,28 @@ import ( ) // ID filters vertices based on their ID field. -func ID(id int) predicate.Friendship { +func ID(id uuid.UUID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.EQ(s.C(FieldID), id)) }) } // IDEQ applies the EQ predicate on the ID field. -func IDEQ(id int) predicate.Friendship { +func IDEQ(id uuid.UUID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.EQ(s.C(FieldID), id)) }) } // IDNEQ applies the NEQ predicate on the ID field. -func IDNEQ(id int) predicate.Friendship { +func IDNEQ(id uuid.UUID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.NEQ(s.C(FieldID), id)) }) } // IDIn applies the In predicate on the ID field. -func IDIn(ids ...int) predicate.Friendship { +func IDIn(ids ...uuid.UUID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { // if not arguments were provided, append the FALSE constants, // since we can't apply "IN ()". This will make this predicate falsy. @@ -64,7 +64,7 @@ func IDIn(ids ...int) predicate.Friendship { } // IDNotIn applies the NotIn predicate on the ID field. -func IDNotIn(ids ...int) predicate.Friendship { +func IDNotIn(ids ...uuid.UUID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { // if not arguments were provided, append the FALSE constants, // since we can't apply "IN ()". This will make this predicate falsy. @@ -81,28 +81,28 @@ func IDNotIn(ids ...int) predicate.Friendship { } // IDGT applies the GT predicate on the ID field. -func IDGT(id int) predicate.Friendship { +func IDGT(id uuid.UUID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.GT(s.C(FieldID), id)) }) } // IDGTE applies the GTE predicate on the ID field. -func IDGTE(id int) predicate.Friendship { +func IDGTE(id uuid.UUID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.GTE(s.C(FieldID), id)) }) } // IDLT applies the LT predicate on the ID field. -func IDLT(id int) predicate.Friendship { +func IDLT(id uuid.UUID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.LT(s.C(FieldID), id)) }) } // IDLTE applies the LTE predicate on the ID field. -func IDLTE(id int) predicate.Friendship { +func IDLTE(id uuid.UUID) predicate.Friendship { return predicate.Friendship(func(s *sql.Selector) { s.Where(sql.LTE(s.C(FieldID), id)) }) diff --git a/entgql/internal/todouuid/ent/friendship_create.go b/entgql/internal/todouuid/ent/friendship_create.go index db7da4467..dcc21b09e 100644 --- a/entgql/internal/todouuid/ent/friendship_create.go +++ b/entgql/internal/todouuid/ent/friendship_create.go @@ -62,6 +62,20 @@ func (fc *FriendshipCreate) SetFriendID(u uuid.UUID) *FriendshipCreate { return fc } +// SetID sets the "id" field. +func (fc *FriendshipCreate) SetID(u uuid.UUID) *FriendshipCreate { + fc.mutation.SetID(u) + return fc +} + +// SetNillableID sets the "id" field if the given value is not nil. +func (fc *FriendshipCreate) SetNillableID(u *uuid.UUID) *FriendshipCreate { + if u != nil { + fc.SetID(*u) + } + return fc +} + // SetUser sets the "user" edge to the User entity. func (fc *FriendshipCreate) SetUser(u *User) *FriendshipCreate { return fc.SetUserID(u.ID) @@ -153,6 +167,10 @@ func (fc *FriendshipCreate) defaults() { v := friendship.DefaultCreatedAt() fc.mutation.SetCreatedAt(v) } + if _, ok := fc.mutation.ID(); !ok { + v := friendship.DefaultID() + fc.mutation.SetID(v) + } } // check runs all checks and user-defined validators on the builder. @@ -183,8 +201,13 @@ func (fc *FriendshipCreate) sqlSave(ctx context.Context) (*Friendship, error) { } return nil, err } - id := _spec.ID.Value.(int64) - _node.ID = int(id) + if _spec.ID.Value != nil { + if id, ok := _spec.ID.Value.(*uuid.UUID); ok { + _node.ID = *id + } else if err := _node.ID.Scan(_spec.ID.Value); err != nil { + return nil, err + } + } return _node, nil } @@ -194,11 +217,15 @@ func (fc *FriendshipCreate) createSpec() (*Friendship, *sqlgraph.CreateSpec) { _spec = &sqlgraph.CreateSpec{ Table: friendship.Table, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, } ) + if id, ok := fc.mutation.ID(); ok { + _node.ID = id + _spec.ID.Value = &id + } if value, ok := fc.mutation.CreatedAt(); ok { _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ Type: field.TypeTime, @@ -291,10 +318,6 @@ func (fcb *FriendshipCreateBulk) Save(ctx context.Context) ([]*Friendship, error return nil, err } mutation.id = &nodes[i].ID - if specs[i].ID.Value != nil { - id := specs[i].ID.Value.(int64) - nodes[i].ID = int(id) - } mutation.done = true return nodes[i], nil }) diff --git a/entgql/internal/todouuid/ent/friendship_delete.go b/entgql/internal/todouuid/ent/friendship_delete.go index 13ec33cda..e51bcf678 100644 --- a/entgql/internal/todouuid/ent/friendship_delete.go +++ b/entgql/internal/todouuid/ent/friendship_delete.go @@ -86,7 +86,7 @@ func (fd *FriendshipDelete) sqlExec(ctx context.Context) (int, error) { Node: &sqlgraph.NodeSpec{ Table: friendship.Table, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, diff --git a/entgql/internal/todouuid/ent/friendship_query.go b/entgql/internal/todouuid/ent/friendship_query.go index f724fc942..965cc7651 100644 --- a/entgql/internal/todouuid/ent/friendship_query.go +++ b/entgql/internal/todouuid/ent/friendship_query.go @@ -148,8 +148,8 @@ func (fq *FriendshipQuery) FirstX(ctx context.Context) *Friendship { // FirstID returns the first Friendship ID from the query. // Returns a *NotFoundError when no Friendship ID was found. -func (fq *FriendshipQuery) FirstID(ctx context.Context) (id int, err error) { - var ids []int +func (fq *FriendshipQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) { + var ids []uuid.UUID if ids, err = fq.Limit(1).IDs(ctx); err != nil { return } @@ -161,7 +161,7 @@ func (fq *FriendshipQuery) FirstID(ctx context.Context) (id int, err error) { } // FirstIDX is like FirstID, but panics if an error occurs. -func (fq *FriendshipQuery) FirstIDX(ctx context.Context) int { +func (fq *FriendshipQuery) FirstIDX(ctx context.Context) uuid.UUID { id, err := fq.FirstID(ctx) if err != nil && !IsNotFound(err) { panic(err) @@ -199,8 +199,8 @@ func (fq *FriendshipQuery) OnlyX(ctx context.Context) *Friendship { // OnlyID is like Only, but returns the only Friendship ID in the query. // Returns a *NotSingularError when more than one Friendship ID is found. // Returns a *NotFoundError when no entities are found. -func (fq *FriendshipQuery) OnlyID(ctx context.Context) (id int, err error) { - var ids []int +func (fq *FriendshipQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) { + var ids []uuid.UUID if ids, err = fq.Limit(2).IDs(ctx); err != nil { return } @@ -216,7 +216,7 @@ func (fq *FriendshipQuery) OnlyID(ctx context.Context) (id int, err error) { } // OnlyIDX is like OnlyID, but panics if an error occurs. -func (fq *FriendshipQuery) OnlyIDX(ctx context.Context) int { +func (fq *FriendshipQuery) OnlyIDX(ctx context.Context) uuid.UUID { id, err := fq.OnlyID(ctx) if err != nil { panic(err) @@ -242,8 +242,8 @@ func (fq *FriendshipQuery) AllX(ctx context.Context) []*Friendship { } // IDs executes the query and returns a list of Friendship IDs. -func (fq *FriendshipQuery) IDs(ctx context.Context) ([]int, error) { - var ids []int +func (fq *FriendshipQuery) IDs(ctx context.Context) ([]uuid.UUID, error) { + var ids []uuid.UUID if err := fq.Select(friendship.FieldID).Scan(ctx, &ids); err != nil { return nil, err } @@ -251,7 +251,7 @@ func (fq *FriendshipQuery) IDs(ctx context.Context) ([]int, error) { } // IDsX is like IDs, but panics if an error occurs. -func (fq *FriendshipQuery) IDsX(ctx context.Context) []int { +func (fq *FriendshipQuery) IDsX(ctx context.Context) []uuid.UUID { ids, err := fq.IDs(ctx) if err != nil { panic(err) @@ -519,7 +519,7 @@ func (fq *FriendshipQuery) querySpec() *sqlgraph.QuerySpec { Table: friendship.Table, Columns: friendship.Columns, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, diff --git a/entgql/internal/todouuid/ent/friendship_update.go b/entgql/internal/todouuid/ent/friendship_update.go index 0eeac1e39..1e42ebaa6 100644 --- a/entgql/internal/todouuid/ent/friendship_update.go +++ b/entgql/internal/todouuid/ent/friendship_update.go @@ -174,7 +174,7 @@ func (fu *FriendshipUpdate) sqlSave(ctx context.Context) (n int, err error) { Table: friendship.Table, Columns: friendship.Columns, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, @@ -425,7 +425,7 @@ func (fuo *FriendshipUpdateOne) sqlSave(ctx context.Context) (_node *Friendship, Table: friendship.Table, Columns: friendship.Columns, ID: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, diff --git a/entgql/internal/todouuid/ent/gql_collection.go b/entgql/internal/todouuid/ent/gql_collection.go index 647916e2d..c9eea13f2 100644 --- a/entgql/internal/todouuid/ent/gql_collection.go +++ b/entgql/internal/todouuid/ent/gql_collection.go @@ -199,6 +199,74 @@ func newCategoryPaginateArgs(rv map[string]interface{}) *categoryPaginateArgs { return args } +// CollectFields tells the query-builder to eagerly load connected nodes by resolver context. +func (f *FriendshipQuery) CollectFields(ctx context.Context, satisfies ...string) (*FriendshipQuery, error) { + fc := graphql.GetFieldContext(ctx) + if fc == nil { + return f, nil + } + if err := f.collectField(ctx, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil { + return nil, err + } + return f, nil +} + +func (f *FriendshipQuery) collectField(ctx context.Context, op *graphql.OperationContext, field graphql.CollectedField, path []string, satisfies ...string) error { + path = append([]string(nil), path...) + for _, field := range graphql.CollectFields(op, field.Selections, satisfies) { + switch field.Name { + case "user": + var ( + path = append(path, field.Name) + query = &UserQuery{config: f.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + f.withUser = query + case "friend": + var ( + path = append(path, field.Name) + query = &UserQuery{config: f.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + f.withFriend = query + } + } + return nil +} + +type friendshipPaginateArgs struct { + first, last *int + after, before *Cursor + opts []FriendshipPaginateOption +} + +func newFriendshipPaginateArgs(rv map[string]interface{}) *friendshipPaginateArgs { + args := &friendshipPaginateArgs{} + if rv == nil { + return args + } + if v := rv[firstField]; v != nil { + args.first = v.(*int) + } + if v := rv[lastField]; v != nil { + args.last = v.(*int) + } + if v := rv[afterField]; v != nil { + args.after = v.(*Cursor) + } + if v := rv[beforeField]; v != nil { + args.before = v.(*Cursor) + } + if v, ok := rv[whereField].(*FriendshipWhereInput); ok { + args.opts = append(args.opts, WithFriendshipFilter(v.Filter)) + } + return args +} + // CollectFields tells the query-builder to eagerly load connected nodes by resolver context. func (gr *GroupQuery) CollectFields(ctx context.Context, satisfies ...string) (*GroupQuery, error) { fc := graphql.GetFieldContext(ctx) @@ -671,6 +739,15 @@ func (u *UserQuery) collectField(ctx context.Context, op *graphql.OperationConte return err } u.withFriends = query + case "friendships": + var ( + path = append(path, field.Name) + query = &FriendshipQuery{config: u.config} + ) + if err := query.collectField(ctx, op, field, path, satisfies...); err != nil { + return err + } + u.withFriendships = query } } return nil diff --git a/entgql/internal/todouuid/ent/gql_edge.go b/entgql/internal/todouuid/ent/gql_edge.go index c22eed8c1..a30781f2d 100644 --- a/entgql/internal/todouuid/ent/gql_edge.go +++ b/entgql/internal/todouuid/ent/gql_edge.go @@ -94,6 +94,22 @@ func (c *Category) Todos( return conn, nil } +func (f *Friendship) User(ctx context.Context) (*User, error) { + result, err := f.Edges.UserOrErr() + if IsNotLoaded(err) { + result, err = f.QueryUser().Only(ctx) + } + return result, err +} + +func (f *Friendship) Friend(ctx context.Context) (*User, error) { + result, err := f.Edges.FriendOrErr() + if IsNotLoaded(err) { + result, err = f.QueryFriend().Only(ctx) + } + return result, err +} + func (gr *Group) Users( ctx context.Context, after *Cursor, first *int, before *Cursor, last *int, where *UserWhereInput, ) (*UserConnection, error) { @@ -331,3 +347,11 @@ func (u *User) Friends(ctx context.Context) ([]*User, error) { } return result, err } + +func (u *User) Friendships(ctx context.Context) ([]*Friendship, error) { + result, err := u.Edges.FriendshipsOrErr() + if IsNotLoaded(err) { + result, err = u.QueryFriendships().All(ctx) + } + return result, err +} diff --git a/entgql/internal/todouuid/ent/gql_mutation_input.go b/entgql/internal/todouuid/ent/gql_mutation_input.go index 156b1b46b..888d5f7ec 100644 --- a/entgql/internal/todouuid/ent/gql_mutation_input.go +++ b/entgql/internal/todouuid/ent/gql_mutation_input.go @@ -58,3 +58,69 @@ func (c *TodoCreate) SetInput(i CreateTodoInput) *TodoCreate { i.Mutate(c.Mutation()) return c } + +// CreateUserInput represents a mutation input for creating users. +type CreateUserInput struct { + Name *string + GroupIDs []uuid.UUID + FriendIDs []uuid.UUID +} + +// Mutate applies the CreateUserInput on the UserMutation builder. +func (i *CreateUserInput) Mutate(m *UserMutation) { + if v := i.Name; v != nil { + m.SetName(*v) + } + if v := i.GroupIDs; len(v) > 0 { + m.AddGroupIDs(v...) + } + if v := i.FriendIDs; len(v) > 0 { + m.AddFriendIDs(v...) + } +} + +// SetInput applies the change-set in the CreateUserInput on the UserCreate builder. +func (c *UserCreate) SetInput(i CreateUserInput) *UserCreate { + i.Mutate(c.Mutation()) + return c +} + +// UpdateUserInput represents a mutation input for updating users. +type UpdateUserInput struct { + Name *string + AddGroupIDs []uuid.UUID + RemoveGroupIDs []uuid.UUID + AddFriendIDs []uuid.UUID + RemoveFriendIDs []uuid.UUID +} + +// Mutate applies the UpdateUserInput on the UserMutation builder. +func (i *UpdateUserInput) Mutate(m *UserMutation) { + if v := i.Name; v != nil { + m.SetName(*v) + } + if v := i.AddGroupIDs; len(v) > 0 { + m.AddGroupIDs(v...) + } + if v := i.RemoveGroupIDs; len(v) > 0 { + m.RemoveGroupIDs(v...) + } + if v := i.AddFriendIDs; len(v) > 0 { + m.AddFriendIDs(v...) + } + if v := i.RemoveFriendIDs; len(v) > 0 { + m.RemoveFriendIDs(v...) + } +} + +// SetInput applies the change-set in the UpdateUserInput on the UserUpdate builder. +func (c *UserUpdate) SetInput(i UpdateUserInput) *UserUpdate { + i.Mutate(c.Mutation()) + return c +} + +// SetInput applies the change-set in the UpdateUserInput on the UserUpdateOne builder. +func (c *UserUpdateOne) SetInput(i UpdateUserInput) *UserUpdateOne { + i.Mutate(c.Mutation()) + return c +} diff --git a/entgql/internal/todouuid/ent/gql_node.go b/entgql/internal/todouuid/ent/gql_node.go index f1d290661..9ba68131c 100644 --- a/entgql/internal/todouuid/ent/gql_node.go +++ b/entgql/internal/todouuid/ent/gql_node.go @@ -23,6 +23,7 @@ import ( "entgo.io/contrib/entgql" "entgo.io/contrib/entgql/internal/todouuid/ent/category" + "entgo.io/contrib/entgql/internal/todouuid/ent/friendship" "entgo.io/contrib/entgql/internal/todouuid/ent/group" "entgo.io/contrib/entgql/internal/todouuid/ent/todo" "entgo.io/contrib/entgql/internal/todouuid/ent/user" @@ -127,6 +128,61 @@ func (c *Category) Node(ctx context.Context) (node *Node, err error) { return node, nil } +func (f *Friendship) Node(ctx context.Context) (node *Node, err error) { + node = &Node{ + ID: f.ID, + Type: "Friendship", + Fields: make([]*Field, 3), + Edges: make([]*Edge, 2), + } + var buf []byte + if buf, err = json.Marshal(f.CreatedAt); err != nil { + return nil, err + } + node.Fields[0] = &Field{ + Type: "time.Time", + Name: "created_at", + Value: string(buf), + } + if buf, err = json.Marshal(f.UserID); err != nil { + return nil, err + } + node.Fields[1] = &Field{ + Type: "uuid.UUID", + Name: "user_id", + Value: string(buf), + } + if buf, err = json.Marshal(f.FriendID); err != nil { + return nil, err + } + node.Fields[2] = &Field{ + Type: "uuid.UUID", + Name: "friend_id", + Value: string(buf), + } + node.Edges[0] = &Edge{ + Type: "User", + Name: "user", + } + err = f.QueryUser(). + Select(user.FieldID). + Scan(ctx, &node.Edges[0].IDs) + if err != nil { + return nil, err + } + node.Edges[1] = &Edge{ + Type: "User", + Name: "friend", + } + err = f.QueryFriend(). + Select(user.FieldID). + Scan(ctx, &node.Edges[1].IDs) + if err != nil { + return nil, err + } + return node, nil +} + func (gr *Group) Node(ctx context.Context) (node *Node, err error) { node = &Node{ ID: gr.ID, @@ -242,7 +298,7 @@ func (u *User) Node(ctx context.Context) (node *Node, err error) { ID: u.ID, Type: "User", Fields: make([]*Field, 1), - Edges: make([]*Edge, 2), + Edges: make([]*Edge, 3), } var buf []byte if buf, err = json.Marshal(u.Name); err != nil { @@ -273,6 +329,16 @@ func (u *User) Node(ctx context.Context) (node *Node, err error) { if err != nil { return nil, err } + node.Edges[2] = &Edge{ + Type: "Friendship", + Name: "friendships", + } + err = u.QueryFriendships(). + Select(friendship.FieldID). + Scan(ctx, &node.Edges[2].IDs) + if err != nil { + return nil, err + } return node, nil } @@ -355,6 +421,18 @@ func (c *Client) noder(ctx context.Context, table string, id uuid.UUID) (Noder, return nil, err } return n, nil + case friendship.Table: + query := c.Friendship.Query(). + Where(friendship.ID(id)) + query, err := query.CollectFields(ctx, "Friendship") + if err != nil { + return nil, err + } + n, err := query.Only(ctx) + if err != nil { + return nil, err + } + return n, nil case group.Table: query := c.Group.Query(). Where(group.ID(id)) @@ -480,6 +558,22 @@ func (c *Client) noders(ctx context.Context, table string, ids []uuid.UUID) ([]N *noder = node } } + case friendship.Table: + query := c.Friendship.Query(). + Where(friendship.IDIn(ids...)) + query, err := query.CollectFields(ctx, "Friendship") + if err != nil { + return nil, err + } + nodes, err := query.All(ctx) + if err != nil { + return nil, err + } + for _, node := range nodes { + for _, noder := range idmap[node.ID] { + *noder = node + } + } case group.Table: query := c.Group.Query(). Where(group.IDIn(ids...)) diff --git a/entgql/internal/todouuid/ent/gql_pagination.go b/entgql/internal/todouuid/ent/gql_pagination.go index 2d4173a0a..bed9013af 100644 --- a/entgql/internal/todouuid/ent/gql_pagination.go +++ b/entgql/internal/todouuid/ent/gql_pagination.go @@ -26,6 +26,7 @@ import ( "strings" "entgo.io/contrib/entgql/internal/todouuid/ent/category" + "entgo.io/contrib/entgql/internal/todouuid/ent/friendship" "entgo.io/contrib/entgql/internal/todouuid/ent/group" "entgo.io/contrib/entgql/internal/todouuid/ent/todo" "entgo.io/contrib/entgql/internal/todouuid/ent/user" @@ -551,6 +552,241 @@ func (c *Category) ToEdge(order *CategoryOrder) *CategoryEdge { } } +// FriendshipEdge is the edge representation of Friendship. +type FriendshipEdge struct { + Node *Friendship `json:"node"` + Cursor Cursor `json:"cursor"` +} + +// FriendshipConnection is the connection containing edges to Friendship. +type FriendshipConnection struct { + Edges []*FriendshipEdge `json:"edges"` + PageInfo PageInfo `json:"pageInfo"` + TotalCount int `json:"totalCount"` +} + +func (c *FriendshipConnection) build(nodes []*Friendship, pager *friendshipPager, after *Cursor, first *int, before *Cursor, last *int) { + c.PageInfo.HasNextPage = before != nil + c.PageInfo.HasPreviousPage = after != nil + if first != nil && *first+1 == len(nodes) { + c.PageInfo.HasNextPage = true + nodes = nodes[:len(nodes)-1] + } else if last != nil && *last+1 == len(nodes) { + c.PageInfo.HasPreviousPage = true + nodes = nodes[:len(nodes)-1] + } + var nodeAt func(int) *Friendship + if last != nil { + n := len(nodes) - 1 + nodeAt = func(i int) *Friendship { + return nodes[n-i] + } + } else { + nodeAt = func(i int) *Friendship { + return nodes[i] + } + } + c.Edges = make([]*FriendshipEdge, len(nodes)) + for i := range nodes { + node := nodeAt(i) + c.Edges[i] = &FriendshipEdge{ + Node: node, + Cursor: pager.toCursor(node), + } + } + if l := len(c.Edges); l > 0 { + c.PageInfo.StartCursor = &c.Edges[0].Cursor + c.PageInfo.EndCursor = &c.Edges[l-1].Cursor + } + if c.TotalCount == 0 { + c.TotalCount = len(nodes) + } +} + +// FriendshipPaginateOption enables pagination customization. +type FriendshipPaginateOption func(*friendshipPager) error + +// WithFriendshipOrder configures pagination ordering. +func WithFriendshipOrder(order *FriendshipOrder) FriendshipPaginateOption { + if order == nil { + order = DefaultFriendshipOrder + } + o := *order + return func(pager *friendshipPager) error { + if err := o.Direction.Validate(); err != nil { + return err + } + if o.Field == nil { + o.Field = DefaultFriendshipOrder.Field + } + pager.order = &o + return nil + } +} + +// WithFriendshipFilter configures pagination filter. +func WithFriendshipFilter(filter func(*FriendshipQuery) (*FriendshipQuery, error)) FriendshipPaginateOption { + return func(pager *friendshipPager) error { + if filter == nil { + return errors.New("FriendshipQuery filter cannot be nil") + } + pager.filter = filter + return nil + } +} + +type friendshipPager struct { + order *FriendshipOrder + filter func(*FriendshipQuery) (*FriendshipQuery, error) +} + +func newFriendshipPager(opts []FriendshipPaginateOption) (*friendshipPager, error) { + pager := &friendshipPager{} + for _, opt := range opts { + if err := opt(pager); err != nil { + return nil, err + } + } + if pager.order == nil { + pager.order = DefaultFriendshipOrder + } + return pager, nil +} + +func (p *friendshipPager) applyFilter(query *FriendshipQuery) (*FriendshipQuery, error) { + if p.filter != nil { + return p.filter(query) + } + return query, nil +} + +func (p *friendshipPager) toCursor(f *Friendship) Cursor { + return p.order.Field.toCursor(f) +} + +func (p *friendshipPager) applyCursors(query *FriendshipQuery, after, before *Cursor) *FriendshipQuery { + for _, predicate := range cursorsToPredicates( + p.order.Direction, after, before, + p.order.Field.field, DefaultFriendshipOrder.Field.field, + ) { + query = query.Where(predicate) + } + return query +} + +func (p *friendshipPager) applyOrder(query *FriendshipQuery, reverse bool) *FriendshipQuery { + direction := p.order.Direction + if reverse { + direction = direction.reverse() + } + query = query.Order(direction.orderFunc(p.order.Field.field)) + if p.order.Field != DefaultFriendshipOrder.Field { + query = query.Order(direction.orderFunc(DefaultFriendshipOrder.Field.field)) + } + return query +} + +func (p *friendshipPager) orderExpr(reverse bool) sql.Querier { + direction := p.order.Direction + if reverse { + direction = direction.reverse() + } + return sql.ExprFunc(func(b *sql.Builder) { + b.Ident(p.order.Field.field).Pad().WriteString(string(direction)) + if p.order.Field != DefaultFriendshipOrder.Field { + b.Comma().Ident(DefaultFriendshipOrder.Field.field).Pad().WriteString(string(direction)) + } + }) +} + +// Paginate executes the query and returns a relay based cursor connection to Friendship. +func (f *FriendshipQuery) Paginate( + ctx context.Context, after *Cursor, first *int, + before *Cursor, last *int, opts ...FriendshipPaginateOption, +) (*FriendshipConnection, error) { + if err := validateFirstLast(first, last); err != nil { + return nil, err + } + pager, err := newFriendshipPager(opts) + if err != nil { + return nil, err + } + if f, err = pager.applyFilter(f); err != nil { + return nil, err + } + conn := &FriendshipConnection{Edges: []*FriendshipEdge{}} + if !hasCollectedField(ctx, edgesField) || first != nil && *first == 0 || last != nil && *last == 0 { + if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) { + if conn.TotalCount, err = f.Count(ctx); err != nil { + return nil, err + } + conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0 + conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0 + } + return conn, nil + } + + if (after != nil || first != nil || before != nil || last != nil) && hasCollectedField(ctx, totalCountField) { + count, err := f.Clone().Count(ctx) + if err != nil { + return nil, err + } + conn.TotalCount = count + } + + f = pager.applyCursors(f, after, before) + f = pager.applyOrder(f, last != nil) + if limit := paginateLimit(first, last); limit != 0 { + f.Limit(limit) + } + if field := collectedField(ctx, edgesField, nodeField); field != nil { + if err := f.collectField(ctx, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil { + return nil, err + } + } + + nodes, err := f.All(ctx) + if err != nil || len(nodes) == 0 { + return conn, err + } + conn.build(nodes, pager, after, first, before, last) + return conn, nil +} + +// FriendshipOrderField defines the ordering field of Friendship. +type FriendshipOrderField struct { + field string + toCursor func(*Friendship) Cursor +} + +// FriendshipOrder defines the ordering of Friendship. +type FriendshipOrder struct { + Direction OrderDirection `json:"direction"` + Field *FriendshipOrderField `json:"field"` +} + +// DefaultFriendshipOrder is the default ordering of Friendship. +var DefaultFriendshipOrder = &FriendshipOrder{ + Direction: OrderDirectionAsc, + Field: &FriendshipOrderField{ + field: friendship.FieldID, + toCursor: func(f *Friendship) Cursor { + return Cursor{ID: f.ID} + }, + }, +} + +// ToEdge converts Friendship into FriendshipEdge. +func (f *Friendship) ToEdge(order *FriendshipOrder) *FriendshipEdge { + if order == nil { + order = DefaultFriendshipOrder + } + return &FriendshipEdge{ + Node: f, + Cursor: order.Field.toCursor(f), + } +} + // GroupEdge is the edge representation of Group. type GroupEdge struct { Node *Group `json:"node"` diff --git a/entgql/internal/todouuid/ent/gql_where_input.go b/entgql/internal/todouuid/ent/gql_where_input.go index b1fea36f5..35d389109 100644 --- a/entgql/internal/todouuid/ent/gql_where_input.go +++ b/entgql/internal/todouuid/ent/gql_where_input.go @@ -23,6 +23,7 @@ import ( "entgo.io/contrib/entgql/internal/todo/ent/schema/schematype" "entgo.io/contrib/entgql/internal/todouuid/ent/category" + "entgo.io/contrib/entgql/internal/todouuid/ent/friendship" "entgo.io/contrib/entgql/internal/todouuid/ent/group" "entgo.io/contrib/entgql/internal/todouuid/ent/predicate" "entgo.io/contrib/entgql/internal/todouuid/ent/todo" @@ -374,6 +375,244 @@ func (i *CategoryWhereInput) P() (predicate.Category, error) { } } +// FriendshipWhereInput represents a where input for filtering Friendship queries. +type FriendshipWhereInput struct { + Predicates []predicate.Friendship `json:"-"` + Not *FriendshipWhereInput `json:"not,omitempty"` + Or []*FriendshipWhereInput `json:"or,omitempty"` + And []*FriendshipWhereInput `json:"and,omitempty"` + + // "id" field predicates. + ID *uuid.UUID `json:"id,omitempty"` + IDNEQ *uuid.UUID `json:"idNEQ,omitempty"` + IDIn []uuid.UUID `json:"idIn,omitempty"` + IDNotIn []uuid.UUID `json:"idNotIn,omitempty"` + IDGT *uuid.UUID `json:"idGT,omitempty"` + IDGTE *uuid.UUID `json:"idGTE,omitempty"` + IDLT *uuid.UUID `json:"idLT,omitempty"` + IDLTE *uuid.UUID `json:"idLTE,omitempty"` + + // "created_at" field predicates. + CreatedAt *time.Time `json:"createdAt,omitempty"` + CreatedAtNEQ *time.Time `json:"createdAtNEQ,omitempty"` + CreatedAtIn []time.Time `json:"createdAtIn,omitempty"` + CreatedAtNotIn []time.Time `json:"createdAtNotIn,omitempty"` + CreatedAtGT *time.Time `json:"createdAtGT,omitempty"` + CreatedAtGTE *time.Time `json:"createdAtGTE,omitempty"` + CreatedAtLT *time.Time `json:"createdAtLT,omitempty"` + CreatedAtLTE *time.Time `json:"createdAtLTE,omitempty"` + + // "user_id" field predicates. + UserID *uuid.UUID `json:"userID,omitempty"` + UserIDNEQ *uuid.UUID `json:"userIDNEQ,omitempty"` + UserIDIn []uuid.UUID `json:"userIDIn,omitempty"` + UserIDNotIn []uuid.UUID `json:"userIDNotIn,omitempty"` + + // "friend_id" field predicates. + FriendID *uuid.UUID `json:"friendID,omitempty"` + FriendIDNEQ *uuid.UUID `json:"friendIDNEQ,omitempty"` + FriendIDIn []uuid.UUID `json:"friendIDIn,omitempty"` + FriendIDNotIn []uuid.UUID `json:"friendIDNotIn,omitempty"` + + // "user" edge predicates. + HasUser *bool `json:"hasUser,omitempty"` + HasUserWith []*UserWhereInput `json:"hasUserWith,omitempty"` + + // "friend" edge predicates. + HasFriend *bool `json:"hasFriend,omitempty"` + HasFriendWith []*UserWhereInput `json:"hasFriendWith,omitempty"` +} + +// AddPredicates adds custom predicates to the where input to be used during the filtering phase. +func (i *FriendshipWhereInput) AddPredicates(predicates ...predicate.Friendship) { + i.Predicates = append(i.Predicates, predicates...) +} + +// Filter applies the FriendshipWhereInput filter on the FriendshipQuery builder. +func (i *FriendshipWhereInput) Filter(q *FriendshipQuery) (*FriendshipQuery, error) { + if i == nil { + return q, nil + } + p, err := i.P() + if err != nil { + if err == ErrEmptyFriendshipWhereInput { + return q, nil + } + return nil, err + } + return q.Where(p), nil +} + +// ErrEmptyFriendshipWhereInput is returned in case the FriendshipWhereInput is empty. +var ErrEmptyFriendshipWhereInput = errors.New("ent: empty predicate FriendshipWhereInput") + +// P returns a predicate for filtering friendships. +// An error is returned if the input is empty or invalid. +func (i *FriendshipWhereInput) P() (predicate.Friendship, error) { + var predicates []predicate.Friendship + if i.Not != nil { + p, err := i.Not.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'not'", err) + } + predicates = append(predicates, friendship.Not(p)) + } + switch n := len(i.Or); { + case n == 1: + p, err := i.Or[0].P() + if err != nil { + return nil, fmt.Errorf("%w: field 'or'", err) + } + predicates = append(predicates, p) + case n > 1: + or := make([]predicate.Friendship, 0, n) + for _, w := range i.Or { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'or'", err) + } + or = append(or, p) + } + predicates = append(predicates, friendship.Or(or...)) + } + switch n := len(i.And); { + case n == 1: + p, err := i.And[0].P() + if err != nil { + return nil, fmt.Errorf("%w: field 'and'", err) + } + predicates = append(predicates, p) + case n > 1: + and := make([]predicate.Friendship, 0, n) + for _, w := range i.And { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'and'", err) + } + and = append(and, p) + } + predicates = append(predicates, friendship.And(and...)) + } + predicates = append(predicates, i.Predicates...) + if i.ID != nil { + predicates = append(predicates, friendship.IDEQ(*i.ID)) + } + if i.IDNEQ != nil { + predicates = append(predicates, friendship.IDNEQ(*i.IDNEQ)) + } + if len(i.IDIn) > 0 { + predicates = append(predicates, friendship.IDIn(i.IDIn...)) + } + if len(i.IDNotIn) > 0 { + predicates = append(predicates, friendship.IDNotIn(i.IDNotIn...)) + } + if i.IDGT != nil { + predicates = append(predicates, friendship.IDGT(*i.IDGT)) + } + if i.IDGTE != nil { + predicates = append(predicates, friendship.IDGTE(*i.IDGTE)) + } + if i.IDLT != nil { + predicates = append(predicates, friendship.IDLT(*i.IDLT)) + } + if i.IDLTE != nil { + predicates = append(predicates, friendship.IDLTE(*i.IDLTE)) + } + if i.CreatedAt != nil { + predicates = append(predicates, friendship.CreatedAtEQ(*i.CreatedAt)) + } + if i.CreatedAtNEQ != nil { + predicates = append(predicates, friendship.CreatedAtNEQ(*i.CreatedAtNEQ)) + } + if len(i.CreatedAtIn) > 0 { + predicates = append(predicates, friendship.CreatedAtIn(i.CreatedAtIn...)) + } + if len(i.CreatedAtNotIn) > 0 { + predicates = append(predicates, friendship.CreatedAtNotIn(i.CreatedAtNotIn...)) + } + if i.CreatedAtGT != nil { + predicates = append(predicates, friendship.CreatedAtGT(*i.CreatedAtGT)) + } + if i.CreatedAtGTE != nil { + predicates = append(predicates, friendship.CreatedAtGTE(*i.CreatedAtGTE)) + } + if i.CreatedAtLT != nil { + predicates = append(predicates, friendship.CreatedAtLT(*i.CreatedAtLT)) + } + if i.CreatedAtLTE != nil { + predicates = append(predicates, friendship.CreatedAtLTE(*i.CreatedAtLTE)) + } + if i.UserID != nil { + predicates = append(predicates, friendship.UserIDEQ(*i.UserID)) + } + if i.UserIDNEQ != nil { + predicates = append(predicates, friendship.UserIDNEQ(*i.UserIDNEQ)) + } + if len(i.UserIDIn) > 0 { + predicates = append(predicates, friendship.UserIDIn(i.UserIDIn...)) + } + if len(i.UserIDNotIn) > 0 { + predicates = append(predicates, friendship.UserIDNotIn(i.UserIDNotIn...)) + } + if i.FriendID != nil { + predicates = append(predicates, friendship.FriendIDEQ(*i.FriendID)) + } + if i.FriendIDNEQ != nil { + predicates = append(predicates, friendship.FriendIDNEQ(*i.FriendIDNEQ)) + } + if len(i.FriendIDIn) > 0 { + predicates = append(predicates, friendship.FriendIDIn(i.FriendIDIn...)) + } + if len(i.FriendIDNotIn) > 0 { + predicates = append(predicates, friendship.FriendIDNotIn(i.FriendIDNotIn...)) + } + + if i.HasUser != nil { + p := friendship.HasUser() + if !*i.HasUser { + p = friendship.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasUserWith) > 0 { + with := make([]predicate.User, 0, len(i.HasUserWith)) + for _, w := range i.HasUserWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasUserWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, friendship.HasUserWith(with...)) + } + if i.HasFriend != nil { + p := friendship.HasFriend() + if !*i.HasFriend { + p = friendship.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasFriendWith) > 0 { + with := make([]predicate.User, 0, len(i.HasFriendWith)) + for _, w := range i.HasFriendWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasFriendWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, friendship.HasFriendWith(with...)) + } + switch len(predicates) { + case 0: + return nil, ErrEmptyFriendshipWhereInput + case 1: + return predicates[0], nil + default: + return friendship.And(predicates...), nil + } +} + // GroupWhereInput represents a where input for filtering Group queries. type GroupWhereInput struct { Predicates []predicate.Group `json:"-"` @@ -969,6 +1208,10 @@ type UserWhereInput struct { // "friends" edge predicates. HasFriends *bool `json:"hasFriends,omitempty"` HasFriendsWith []*UserWhereInput `json:"hasFriendsWith,omitempty"` + + // "friendships" edge predicates. + HasFriendships *bool `json:"hasFriendships,omitempty"` + HasFriendshipsWith []*FriendshipWhereInput `json:"hasFriendshipsWith,omitempty"` } // AddPredicates adds custom predicates to the where input to be used during the filtering phase. @@ -1142,6 +1385,24 @@ func (i *UserWhereInput) P() (predicate.User, error) { } predicates = append(predicates, user.HasFriendsWith(with...)) } + if i.HasFriendships != nil { + p := user.HasFriendships() + if !*i.HasFriendships { + p = user.Not(p) + } + predicates = append(predicates, p) + } + if len(i.HasFriendshipsWith) > 0 { + with := make([]predicate.Friendship, 0, len(i.HasFriendshipsWith)) + for _, w := range i.HasFriendshipsWith { + p, err := w.P() + if err != nil { + return nil, fmt.Errorf("%w: field 'HasFriendshipsWith'", err) + } + with = append(with, p) + } + predicates = append(predicates, user.HasFriendshipsWith(with...)) + } switch len(predicates) { case 0: return nil, ErrEmptyUserWhereInput diff --git a/entgql/internal/todouuid/ent/migrate/schema.go b/entgql/internal/todouuid/ent/migrate/schema.go index 946c0a7df..639747c01 100644 --- a/entgql/internal/todouuid/ent/migrate/schema.go +++ b/entgql/internal/todouuid/ent/migrate/schema.go @@ -40,7 +40,7 @@ var ( } // FriendshipsColumns holds the columns for the "friendships" table. FriendshipsColumns = []*schema.Column{ - {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "id", Type: field.TypeUUID}, {Name: "created_at", Type: field.TypeTime}, {Name: "user_id", Type: field.TypeUUID}, {Name: "friend_id", Type: field.TypeUUID}, diff --git a/entgql/internal/todouuid/ent/mutation.go b/entgql/internal/todouuid/ent/mutation.go index e63a97b71..ca43fc712 100644 --- a/entgql/internal/todouuid/ent/mutation.go +++ b/entgql/internal/todouuid/ent/mutation.go @@ -888,7 +888,7 @@ type FriendshipMutation struct { config op Op typ string - id *int + id *uuid.UUID created_at *time.Time clearedFields map[string]struct{} user *uuid.UUID @@ -920,7 +920,7 @@ func newFriendshipMutation(c config, op Op, opts ...friendshipOption) *Friendshi } // withFriendshipID sets the ID field of the mutation. -func withFriendshipID(id int) friendshipOption { +func withFriendshipID(id uuid.UUID) friendshipOption { return func(m *FriendshipMutation) { var ( err error @@ -970,9 +970,15 @@ func (m FriendshipMutation) Tx() (*Tx, error) { return tx, nil } +// SetID sets the value of the id field. Note that this +// operation is only accepted on creation of Friendship entities. +func (m *FriendshipMutation) SetID(id uuid.UUID) { + m.id = &id +} + // ID returns the ID value in the mutation. Note that the ID is only available // if it was provided to the builder or after it was returned from the database. -func (m *FriendshipMutation) ID() (id int, exists bool) { +func (m *FriendshipMutation) ID() (id uuid.UUID, exists bool) { if m.id == nil { return } @@ -983,12 +989,12 @@ func (m *FriendshipMutation) ID() (id int, exists bool) { // That means, if the mutation is applied within a transaction with an isolation level such // as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated // or updated by the mutation. -func (m *FriendshipMutation) IDs(ctx context.Context) ([]int, error) { +func (m *FriendshipMutation) IDs(ctx context.Context) ([]uuid.UUID, error) { switch { case m.op.Is(OpUpdateOne | OpDeleteOne): id, exists := m.ID() if exists { - return []int{id}, nil + return []uuid.UUID{id}, nil } fallthrough case m.op.Is(OpUpdate | OpDelete): @@ -2746,8 +2752,8 @@ type UserMutation struct { friends map[uuid.UUID]struct{} removedfriends map[uuid.UUID]struct{} clearedfriends bool - friendships map[int]struct{} - removedfriendships map[int]struct{} + friendships map[uuid.UUID]struct{} + removedfriendships map[uuid.UUID]struct{} clearedfriendships bool done bool oldValue func(context.Context) (*User, error) @@ -3003,9 +3009,9 @@ func (m *UserMutation) ResetFriends() { } // AddFriendshipIDs adds the "friendships" edge to the Friendship entity by ids. -func (m *UserMutation) AddFriendshipIDs(ids ...int) { +func (m *UserMutation) AddFriendshipIDs(ids ...uuid.UUID) { if m.friendships == nil { - m.friendships = make(map[int]struct{}) + m.friendships = make(map[uuid.UUID]struct{}) } for i := range ids { m.friendships[ids[i]] = struct{}{} @@ -3023,9 +3029,9 @@ func (m *UserMutation) FriendshipsCleared() bool { } // RemoveFriendshipIDs removes the "friendships" edge to the Friendship entity by IDs. -func (m *UserMutation) RemoveFriendshipIDs(ids ...int) { +func (m *UserMutation) RemoveFriendshipIDs(ids ...uuid.UUID) { if m.removedfriendships == nil { - m.removedfriendships = make(map[int]struct{}) + m.removedfriendships = make(map[uuid.UUID]struct{}) } for i := range ids { delete(m.friendships, ids[i]) @@ -3034,7 +3040,7 @@ func (m *UserMutation) RemoveFriendshipIDs(ids ...int) { } // RemovedFriendships returns the removed IDs of the "friendships" edge to the Friendship entity. -func (m *UserMutation) RemovedFriendshipsIDs() (ids []int) { +func (m *UserMutation) RemovedFriendshipsIDs() (ids []uuid.UUID) { for id := range m.removedfriendships { ids = append(ids, id) } @@ -3042,7 +3048,7 @@ func (m *UserMutation) RemovedFriendshipsIDs() (ids []int) { } // FriendshipsIDs returns the "friendships" edge IDs in the mutation. -func (m *UserMutation) FriendshipsIDs() (ids []int) { +func (m *UserMutation) FriendshipsIDs() (ids []uuid.UUID) { for id := range m.friendships { ids = append(ids, id) } diff --git a/entgql/internal/todouuid/ent/runtime.go b/entgql/internal/todouuid/ent/runtime.go index 56c83a5e5..a0c9359b3 100644 --- a/entgql/internal/todouuid/ent/runtime.go +++ b/entgql/internal/todouuid/ent/runtime.go @@ -49,9 +49,13 @@ func init() { friendshipFields := schema.Friendship{}.Fields() _ = friendshipFields // friendshipDescCreatedAt is the schema descriptor for created_at field. - friendshipDescCreatedAt := friendshipFields[0].Descriptor() + friendshipDescCreatedAt := friendshipFields[1].Descriptor() // friendship.DefaultCreatedAt holds the default value on creation for the created_at field. friendship.DefaultCreatedAt = friendshipDescCreatedAt.Default.(func() time.Time) + // friendshipDescID is the schema descriptor for id field. + friendshipDescID := friendshipFields[0].Descriptor() + // friendship.DefaultID holds the default value on creation for the id field. + friendship.DefaultID = friendshipDescID.Default.(func() uuid.UUID) groupMixin := schema.Group{}.Mixin() groupMixinFields0 := groupMixin[0].Fields() _ = groupMixinFields0 diff --git a/entgql/internal/todouuid/ent/schema/friendship.go b/entgql/internal/todouuid/ent/schema/friendship.go index 4f3351b7e..1dbb9eaa5 100644 --- a/entgql/internal/todouuid/ent/schema/friendship.go +++ b/entgql/internal/todouuid/ent/schema/friendship.go @@ -31,6 +31,8 @@ type Friendship struct { // Fields of the Friendship. func (Friendship) Fields() []ent.Field { return []ent.Field{ + field.UUID("id", uuid.UUID{}). + Default(uuid.New), field.Time("created_at"). Default(time.Now), field.UUID("user_id", uuid.UUID{}), diff --git a/entgql/internal/todouuid/ent/user.go b/entgql/internal/todouuid/ent/user.go index 2d82365dd..b978590c0 100644 --- a/entgql/internal/todouuid/ent/user.go +++ b/entgql/internal/todouuid/ent/user.go @@ -49,7 +49,7 @@ type UserEdges struct { // type was loaded (or requested) in eager-loading or not. loadedTypes [3]bool // totalCount holds the count of the edges above. - totalCount [2]*int + totalCount [3]*int } // GroupsOrErr returns the Groups value or an error if the edge diff --git a/entgql/internal/todouuid/ent/user_create.go b/entgql/internal/todouuid/ent/user_create.go index 63af2cd82..5e09ba97c 100644 --- a/entgql/internal/todouuid/ent/user_create.go +++ b/entgql/internal/todouuid/ent/user_create.go @@ -95,14 +95,14 @@ func (uc *UserCreate) AddFriends(u ...*User) *UserCreate { } // AddFriendshipIDs adds the "friendships" edge to the Friendship entity by IDs. -func (uc *UserCreate) AddFriendshipIDs(ids ...int) *UserCreate { +func (uc *UserCreate) AddFriendshipIDs(ids ...uuid.UUID) *UserCreate { uc.mutation.AddFriendshipIDs(ids...) return uc } // AddFriendships adds the "friendships" edges to the Friendship entity. func (uc *UserCreate) AddFriendships(f ...*Friendship) *UserCreate { - ids := make([]int, len(f)) + ids := make([]uuid.UUID, len(f)) for i := range f { ids[i] = f[i].ID } @@ -296,7 +296,7 @@ func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) { Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, diff --git a/entgql/internal/todouuid/ent/user_update.go b/entgql/internal/todouuid/ent/user_update.go index 93ea7670b..b21ec6c1e 100644 --- a/entgql/internal/todouuid/ent/user_update.go +++ b/entgql/internal/todouuid/ent/user_update.go @@ -89,14 +89,14 @@ func (uu *UserUpdate) AddFriends(u ...*User) *UserUpdate { } // AddFriendshipIDs adds the "friendships" edge to the Friendship entity by IDs. -func (uu *UserUpdate) AddFriendshipIDs(ids ...int) *UserUpdate { +func (uu *UserUpdate) AddFriendshipIDs(ids ...uuid.UUID) *UserUpdate { uu.mutation.AddFriendshipIDs(ids...) return uu } // AddFriendships adds the "friendships" edges to the Friendship entity. func (uu *UserUpdate) AddFriendships(f ...*Friendship) *UserUpdate { - ids := make([]int, len(f)) + ids := make([]uuid.UUID, len(f)) for i := range f { ids[i] = f[i].ID } @@ -157,14 +157,14 @@ func (uu *UserUpdate) ClearFriendships() *UserUpdate { } // RemoveFriendshipIDs removes the "friendships" edge to Friendship entities by IDs. -func (uu *UserUpdate) RemoveFriendshipIDs(ids ...int) *UserUpdate { +func (uu *UserUpdate) RemoveFriendshipIDs(ids ...uuid.UUID) *UserUpdate { uu.mutation.RemoveFriendshipIDs(ids...) return uu } // RemoveFriendships removes "friendships" edges to Friendship entities. func (uu *UserUpdate) RemoveFriendships(f ...*Friendship) *UserUpdate { - ids := make([]int, len(f)) + ids := make([]uuid.UUID, len(f)) for i := range f { ids[i] = f[i].ID } @@ -379,7 +379,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, @@ -395,7 +395,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, @@ -414,7 +414,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) { Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, @@ -488,14 +488,14 @@ func (uuo *UserUpdateOne) AddFriends(u ...*User) *UserUpdateOne { } // AddFriendshipIDs adds the "friendships" edge to the Friendship entity by IDs. -func (uuo *UserUpdateOne) AddFriendshipIDs(ids ...int) *UserUpdateOne { +func (uuo *UserUpdateOne) AddFriendshipIDs(ids ...uuid.UUID) *UserUpdateOne { uuo.mutation.AddFriendshipIDs(ids...) return uuo } // AddFriendships adds the "friendships" edges to the Friendship entity. func (uuo *UserUpdateOne) AddFriendships(f ...*Friendship) *UserUpdateOne { - ids := make([]int, len(f)) + ids := make([]uuid.UUID, len(f)) for i := range f { ids[i] = f[i].ID } @@ -556,14 +556,14 @@ func (uuo *UserUpdateOne) ClearFriendships() *UserUpdateOne { } // RemoveFriendshipIDs removes the "friendships" edge to Friendship entities by IDs. -func (uuo *UserUpdateOne) RemoveFriendshipIDs(ids ...int) *UserUpdateOne { +func (uuo *UserUpdateOne) RemoveFriendshipIDs(ids ...uuid.UUID) *UserUpdateOne { uuo.mutation.RemoveFriendshipIDs(ids...) return uuo } // RemoveFriendships removes "friendships" edges to Friendship entities. func (uuo *UserUpdateOne) RemoveFriendships(f ...*Friendship) *UserUpdateOne { - ids := make([]int, len(f)) + ids := make([]uuid.UUID, len(f)) for i := range f { ids[i] = f[i].ID } @@ -808,7 +808,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, @@ -824,7 +824,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, @@ -843,7 +843,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (_node *User, err error) Bidi: false, Target: &sqlgraph.EdgeTarget{ IDSpec: &sqlgraph.FieldSpec{ - Type: field.TypeInt, + Type: field.TypeUUID, Column: friendship.FieldID, }, }, diff --git a/entgql/internal/todouuid/generated.go b/entgql/internal/todouuid/generated.go index d82c1312b..443c0a40f 100644 --- a/entgql/internal/todouuid/generated.go +++ b/entgql/internal/todouuid/generated.go @@ -69,6 +69,15 @@ type ComplexityRoot struct { MaxMembers func(childComplexity int) int } + Friendship struct { + CreatedAt func(childComplexity int) int + Friend func(childComplexity int) int + FriendID func(childComplexity int) int + ID func(childComplexity int) int + User func(childComplexity int) int + UserID func(childComplexity int) int + } + Group struct { ID func(childComplexity int) int Name func(childComplexity int) int @@ -131,10 +140,11 @@ type ComplexityRoot struct { } User struct { - Friends func(childComplexity int) int - Groups func(childComplexity int, after *ent.Cursor, first *int, before *ent.Cursor, last *int, where *ent.GroupWhereInput) int - ID func(childComplexity int) int - Name func(childComplexity int) int + Friends func(childComplexity int) int + Friendships func(childComplexity int) int + Groups func(childComplexity int, after *ent.Cursor, first *int, before *ent.Cursor, last *int, where *ent.GroupWhereInput) int + ID func(childComplexity int) int + Name func(childComplexity int) int } UserConnection struct { @@ -260,6 +270,48 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.CategoryConfig.MaxMembers(childComplexity), true + case "Friendship.createdAt": + if e.complexity.Friendship.CreatedAt == nil { + break + } + + return e.complexity.Friendship.CreatedAt(childComplexity), true + + case "Friendship.friend": + if e.complexity.Friendship.Friend == nil { + break + } + + return e.complexity.Friendship.Friend(childComplexity), true + + case "Friendship.friendID": + if e.complexity.Friendship.FriendID == nil { + break + } + + return e.complexity.Friendship.FriendID(childComplexity), true + + case "Friendship.id": + if e.complexity.Friendship.ID == nil { + break + } + + return e.complexity.Friendship.ID(childComplexity), true + + case "Friendship.user": + if e.complexity.Friendship.User == nil { + break + } + + return e.complexity.Friendship.User(childComplexity), true + + case "Friendship.userID": + if e.complexity.Friendship.UserID == nil { + break + } + + return e.complexity.Friendship.UserID(childComplexity), true + case "Group.id": if e.complexity.Group.ID == nil { break @@ -545,6 +597,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.User.Friends(childComplexity), true + case "User.friendships": + if e.complexity.User.Friendships == nil { + break + } + + return e.complexity.User.Friendships(childComplexity), true + case "User.groups": if e.complexity.User.Groups == nil { break @@ -618,9 +677,12 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler { ec.unmarshalInputCategoryOrder, ec.unmarshalInputCategoryWhereInput, ec.unmarshalInputCreateTodoInput, + ec.unmarshalInputCreateUserInput, + ec.unmarshalInputFriendshipWhereInput, ec.unmarshalInputGroupWhereInput, ec.unmarshalInputTodoOrder, ec.unmarshalInputTodoWhereInput, + ec.unmarshalInputUpdateUserInput, ec.unmarshalInputUserWhereInput, ) first := true @@ -844,10 +906,54 @@ input CreateTodoInput { secretID: ID } """ +CreateUserInput is used for create User object. +Input was generated by ent. +""" +input CreateUserInput { + name: String + groupIDs: [ID!] + friendIDs: [ID!] +} +""" Define a Relay Cursor type: https://relay.dev/graphql/connections.htm#sec-Cursor """ scalar Cursor +type Friendship implements Node { + id: ID! + createdAt: Time! + userID: ID! + friendID: ID! + user: User! + friend: User! +} +""" +FriendshipWhereInput is used for filtering Friendship objects. +Input was generated by ent. +""" +input FriendshipWhereInput { + not: FriendshipWhereInput + and: [FriendshipWhereInput!] + or: [FriendshipWhereInput!] + """id field predicates""" + id: ID + idNEQ: ID + idIn: [ID!] + idNotIn: [ID!] + idGT: ID + idGTE: ID + idLT: ID + idLTE: ID + """created_at field predicates""" + createdAt: Time + createdAtNEQ: Time + createdAtIn: [Time!] + createdAtNotIn: [Time!] + createdAtGT: Time + createdAtGTE: Time + createdAtLT: Time + createdAtLTE: Time +} type Group implements Node { id: ID! name: String! @@ -1146,6 +1252,17 @@ input TodoWhereInput { hasCategory: Boolean hasCategoryWith: [CategoryWhereInput!] } +""" +UpdateUserInput is used for update User object. +Input was generated by ent. +""" +input UpdateUserInput { + name: String + addGroupIDs: [ID!] + removeGroupIDs: [ID!] + addFriendIDs: [ID!] + removeFriendIDs: [ID!] +} type User implements Node { id: ID! name: String! @@ -1166,6 +1283,7 @@ type User implements Node { where: GroupWhereInput ): GroupConnection! friends: [User!] + friendships: [Friendship!] } """A connection to a list of items.""" type UserConnection { @@ -1220,6 +1338,9 @@ input UserWhereInput { """friends edge predicates""" hasFriends: Boolean hasFriendsWith: [UserWhereInput!] + """friendships edge predicates""" + hasFriendships: Boolean + hasFriendshipsWith: [FriendshipWhereInput!] } `, BuiltIn: false}, } @@ -2074,8 +2195,281 @@ func (ec *executionContext) fieldContext_Category_todos(ctx context.Context, fie return fc, nil } -func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField, obj *schematype.CategoryConfig) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_CategoryConfig_maxMembers(ctx, field) +func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField, obj *schematype.CategoryConfig) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_CategoryConfig_maxMembers(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.MaxMembers, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(int) + fc.Result = res + return ec.marshalOInt2int(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "CategoryConfig", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Int does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_id(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_id(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.ID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(uuid.UUID) + fc.Result = res + return ec.marshalNID2githubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_id(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_createdAt(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_createdAt(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.CreatedAt, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(time.Time) + fc.Result = res + return ec.marshalNTime2timeᚐTime(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_createdAt(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Time does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_userID(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_userID(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.UserID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(uuid.UUID) + fc.Result = res + return ec.marshalNID2githubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_userID(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_friendID(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_friendID(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.FriendID, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(uuid.UUID) + fc.Result = res + return ec.marshalNID2githubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_friendID(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type ID does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_user(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_user(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.User(ctx) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(*ent.User) + fc.Result = res + return ec.marshalNUser2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐUser(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Friendship_user(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Friendship", + Field: field, + IsMethod: true, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "id": + return ec.fieldContext_User_id(ctx, field) + case "name": + return ec.fieldContext_User_name(ctx, field) + case "groups": + return ec.fieldContext_User_groups(ctx, field) + case "friends": + return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type User", field.Name) + }, + } + return fc, nil +} + +func (ec *executionContext) _Friendship_friend(ctx context.Context, field graphql.CollectedField, obj *ent.Friendship) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Friendship_friend(ctx, field) if err != nil { return graphql.Null } @@ -2088,28 +2482,43 @@ func (ec *executionContext) _CategoryConfig_maxMembers(ctx context.Context, fiel }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return obj.MaxMembers, nil + return obj.Friend(ctx) }) if err != nil { ec.Error(ctx, err) return graphql.Null } if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } return graphql.Null } - res := resTmp.(int) + res := resTmp.(*ent.User) fc.Result = res - return ec.marshalOInt2int(ctx, field.Selections, res) + return ec.marshalNUser2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐUser(ctx, field.Selections, res) } -func (ec *executionContext) fieldContext_CategoryConfig_maxMembers(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { +func (ec *executionContext) fieldContext_Friendship_friend(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { fc = &graphql.FieldContext{ - Object: "CategoryConfig", + Object: "Friendship", Field: field, - IsMethod: false, + IsMethod: true, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type Int does not have child fields") + switch field.Name { + case "id": + return ec.fieldContext_User_id(ctx, field) + case "name": + return ec.fieldContext_User_name(ctx, field) + case "groups": + return ec.fieldContext_User_groups(ctx, field) + case "friends": + return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, } return fc, nil @@ -4151,6 +4560,8 @@ func (ec *executionContext) fieldContext_User_friends(ctx context.Context, field return ec.fieldContext_User_groups(ctx, field) case "friends": return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, @@ -4158,6 +4569,61 @@ func (ec *executionContext) fieldContext_User_friends(ctx context.Context, field return fc, nil } +func (ec *executionContext) _User_friendships(ctx context.Context, field graphql.CollectedField, obj *ent.User) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_User_friendships(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Friendships(ctx) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.([]*ent.Friendship) + fc.Result = res + return ec.marshalOFriendship2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendshipᚄ(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_User_friendships(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "User", + Field: field, + IsMethod: true, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "id": + return ec.fieldContext_Friendship_id(ctx, field) + case "createdAt": + return ec.fieldContext_Friendship_createdAt(ctx, field) + case "userID": + return ec.fieldContext_Friendship_userID(ctx, field) + case "friendID": + return ec.fieldContext_Friendship_friendID(ctx, field) + case "user": + return ec.fieldContext_Friendship_user(ctx, field) + case "friend": + return ec.fieldContext_Friendship_friend(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type Friendship", field.Name) + }, + } + return fc, nil +} + func (ec *executionContext) _UserConnection_edges(ctx context.Context, field graphql.CollectedField, obj *ent.UserConnection) (ret graphql.Marshaler) { fc, err := ec.fieldContext_UserConnection_edges(ctx, field) if err != nil { @@ -4347,6 +4813,8 @@ func (ec *executionContext) fieldContext_UserEdge_node(ctx context.Context, fiel return ec.fieldContext_User_groups(ctx, field) case "friends": return ec.fieldContext_User_friends(ctx, field) + case "friendships": + return ec.fieldContext_User_friendships(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type User", field.Name) }, @@ -6752,43 +7220,249 @@ func (ec *executionContext) unmarshalInputCreateTodoInput(ctx context.Context, o if err != nil { return it, err } - case "text": + case "text": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("text")) + it.Text, err = ec.unmarshalNString2string(ctx, v) + if err != nil { + return it, err + } + case "parentID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("parentID")) + it.ParentID, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + if err != nil { + return it, err + } + case "childIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("childIDs")) + it.ChildIDs, err = ec.unmarshalOID2ᚕgithubᚗcomᚋgoogleᚋuuidᚐUUIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "categoryID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("categoryID")) + it.CategoryID, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + if err != nil { + return it, err + } + case "secretID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("secretID")) + it.SecretID, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + +func (ec *executionContext) unmarshalInputCreateUserInput(ctx context.Context, obj interface{}) (ent.CreateUserInput, error) { + var it ent.CreateUserInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "name": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name")) + it.Name, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "groupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("groupIDs")) + it.GroupIDs, err = ec.unmarshalOID2ᚕgithubᚗcomᚋgoogleᚋuuidᚐUUIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "friendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("friendIDs")) + it.FriendIDs, err = ec.unmarshalOID2ᚕgithubᚗcomᚋgoogleᚋuuidᚐUUIDᚄ(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + +func (ec *executionContext) unmarshalInputFriendshipWhereInput(ctx context.Context, obj interface{}) (ent.FriendshipWhereInput, error) { + var it ent.FriendshipWhereInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "not": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("not")) + it.Not, err = ec.unmarshalOFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendshipWhereInput(ctx, v) + if err != nil { + return it, err + } + case "and": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("and")) + it.And, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } + case "or": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("or")) + it.Or, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } + case "id": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("id")) + it.ID, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + if err != nil { + return it, err + } + case "idNEQ": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idNEQ")) + it.IDNEQ, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + if err != nil { + return it, err + } + case "idIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idIn")) + it.IDIn, err = ec.unmarshalOID2ᚕgithubᚗcomᚋgoogleᚋuuidᚐUUIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "idNotIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idNotIn")) + it.IDNotIn, err = ec.unmarshalOID2ᚕgithubᚗcomᚋgoogleᚋuuidᚐUUIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "idGT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idGT")) + it.IDGT, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + if err != nil { + return it, err + } + case "idGTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idGTE")) + it.IDGTE, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + if err != nil { + return it, err + } + case "idLT": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idLT")) + it.IDLT, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + if err != nil { + return it, err + } + case "idLTE": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("idLTE")) + it.IDLTE, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + if err != nil { + return it, err + } + case "createdAt": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAt")) + it.CreatedAt, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + case "createdAtNEQ": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtNEQ")) + it.CreatedAtNEQ, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) + if err != nil { + return it, err + } + case "createdAtIn": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtIn")) + it.CreatedAtIn, err = ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) + if err != nil { + return it, err + } + case "createdAtNotIn": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("text")) - it.Text, err = ec.unmarshalNString2string(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtNotIn")) + it.CreatedAtNotIn, err = ec.unmarshalOTime2ᚕtimeᚐTimeᚄ(ctx, v) if err != nil { return it, err } - case "parentID": + case "createdAtGT": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("parentID")) - it.ParentID, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtGT")) + it.CreatedAtGT, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "childIDs": + case "createdAtGTE": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("childIDs")) - it.ChildIDs, err = ec.unmarshalOID2ᚕgithubᚗcomᚋgoogleᚋuuidᚐUUIDᚄ(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtGTE")) + it.CreatedAtGTE, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "categoryID": + case "createdAtLT": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("categoryID")) - it.CategoryID, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtLT")) + it.CreatedAtLT, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } - case "secretID": + case "createdAtLTE": var err error - ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("secretID")) - it.SecretID, err = ec.unmarshalOID2ᚖgithubᚗcomᚋgoogleᚋuuidᚐUUID(ctx, v) + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("createdAtLTE")) + it.CreatedAtLTE, err = ec.unmarshalOTime2ᚖtimeᚐTime(ctx, v) if err != nil { return it, err } @@ -7542,6 +8216,61 @@ func (ec *executionContext) unmarshalInputTodoWhereInput(ctx context.Context, ob return it, nil } +func (ec *executionContext) unmarshalInputUpdateUserInput(ctx context.Context, obj interface{}) (ent.UpdateUserInput, error) { + var it ent.UpdateUserInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "name": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name")) + it.Name, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "addGroupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("addGroupIDs")) + it.AddGroupIDs, err = ec.unmarshalOID2ᚕgithubᚗcomᚋgoogleᚋuuidᚐUUIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "removeGroupIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("removeGroupIDs")) + it.RemoveGroupIDs, err = ec.unmarshalOID2ᚕgithubᚗcomᚋgoogleᚋuuidᚐUUIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "addFriendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("addFriendIDs")) + it.AddFriendIDs, err = ec.unmarshalOID2ᚕgithubᚗcomᚋgoogleᚋuuidᚐUUIDᚄ(ctx, v) + if err != nil { + return it, err + } + case "removeFriendIDs": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("removeFriendIDs")) + it.RemoveFriendIDs, err = ec.unmarshalOID2ᚕgithubᚗcomᚋgoogleᚋuuidᚐUUIDᚄ(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + func (ec *executionContext) unmarshalInputUserWhereInput(ctx context.Context, obj interface{}) (ent.UserWhereInput, error) { var it ent.UserWhereInput asMap := map[string]interface{}{} @@ -7775,6 +8504,22 @@ func (ec *executionContext) unmarshalInputUserWhereInput(ctx context.Context, ob if err != nil { return it, err } + case "hasFriendships": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("hasFriendships")) + it.HasFriendships, err = ec.unmarshalOBoolean2ᚖbool(ctx, v) + if err != nil { + return it, err + } + case "hasFriendshipsWith": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("hasFriendshipsWith")) + it.HasFriendshipsWith, err = ec.unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendshipWhereInputᚄ(ctx, v) + if err != nil { + return it, err + } } } @@ -7794,6 +8539,11 @@ func (ec *executionContext) _Node(ctx context.Context, sel ast.SelectionSet, obj return graphql.Null } return ec._Category(ctx, sel, obj) + case *ent.Friendship: + if obj == nil { + return graphql.Null + } + return ec._Friendship(ctx, sel, obj) case *ent.Group: if obj == nil { return graphql.Null @@ -7921,6 +8671,95 @@ func (ec *executionContext) _CategoryConfig(ctx context.Context, sel ast.Selecti return out } +var friendshipImplementors = []string{"Friendship", "Node"} + +func (ec *executionContext) _Friendship(ctx context.Context, sel ast.SelectionSet, obj *ent.Friendship) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, friendshipImplementors) + out := graphql.NewFieldSet(fields) + var invalids uint32 + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("Friendship") + case "id": + + out.Values[i] = ec._Friendship_id(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "createdAt": + + out.Values[i] = ec._Friendship_createdAt(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "userID": + + out.Values[i] = ec._Friendship_userID(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "friendID": + + out.Values[i] = ec._Friendship_friendID(ctx, field, obj) + + if out.Values[i] == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + case "user": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Friendship_user(ctx, field, obj) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + return res + } + + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + case "friend": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Friendship_friend(ctx, field, obj) + if res == graphql.Null { + atomic.AddUint32(&invalids, 1) + } + return res + } + + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch() + if invalids > 0 { + return graphql.Null + } + return out +} + var groupImplementors = []string{"Group", "Node"} func (ec *executionContext) _Group(ctx context.Context, sel ast.SelectionSet, obj *ent.Group) graphql.Marshaler { @@ -8570,6 +9409,23 @@ func (ec *executionContext) _User(ctx context.Context, sel ast.SelectionSet, obj return res } + out.Concurrently(i, func() graphql.Marshaler { + return innerFunc(ctx) + + }) + case "friendships": + field := field + + innerFunc := func(ctx context.Context) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._User_friendships(ctx, field, obj) + return res + } + out.Concurrently(i, func() graphql.Marshaler { return innerFunc(ctx) @@ -9055,6 +9911,21 @@ func (ec *executionContext) marshalNDuration2timeᚐDuration(ctx context.Context return res } +func (ec *executionContext) marshalNFriendship2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendship(ctx context.Context, sel ast.SelectionSet, v *ent.Friendship) graphql.Marshaler { + if v == nil { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + return graphql.Null + } + return ec._Friendship(ctx, sel, v) +} + +func (ec *executionContext) unmarshalNFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendshipWhereInput(ctx context.Context, v interface{}) (*ent.FriendshipWhereInput, error) { + res, err := ec.unmarshalInputFriendshipWhereInput(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) marshalNGroupConnection2entgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐGroupConnection(ctx context.Context, sel ast.SelectionSet, v ent.GroupConnection) graphql.Marshaler { return ec._GroupConnection(ctx, sel, &v) } @@ -9833,6 +10704,81 @@ func (ec *executionContext) marshalODuration2ᚖtimeᚐDuration(ctx context.Cont return res } +func (ec *executionContext) marshalOFriendship2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendshipᚄ(ctx context.Context, sel ast.SelectionSet, v []*ent.Friendship) graphql.Marshaler { + if v == nil { + return graphql.Null + } + ret := make(graphql.Array, len(v)) + var wg sync.WaitGroup + isLen1 := len(v) == 1 + if !isLen1 { + wg.Add(len(v)) + } + for i := range v { + i := i + fc := &graphql.FieldContext{ + Index: &i, + Result: &v[i], + } + ctx := graphql.WithFieldContext(ctx, fc) + f := func(i int) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = nil + } + }() + if !isLen1 { + defer wg.Done() + } + ret[i] = ec.marshalNFriendship2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendship(ctx, sel, v[i]) + } + if isLen1 { + f(i) + } else { + go f(i) + } + + } + wg.Wait() + + for _, e := range ret { + if e == graphql.Null { + return graphql.Null + } + } + + return ret +} + +func (ec *executionContext) unmarshalOFriendshipWhereInput2ᚕᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendshipWhereInputᚄ(ctx context.Context, v interface{}) ([]*ent.FriendshipWhereInput, error) { + if v == nil { + return nil, nil + } + var vSlice []interface{} + if v != nil { + vSlice = graphql.CoerceList(v) + } + var err error + res := make([]*ent.FriendshipWhereInput, len(vSlice)) + for i := range vSlice { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithIndex(i)) + res[i], err = ec.unmarshalNFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendshipWhereInput(ctx, vSlice[i]) + if err != nil { + return nil, err + } + } + return res, nil +} + +func (ec *executionContext) unmarshalOFriendshipWhereInput2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐFriendshipWhereInput(ctx context.Context, v interface{}) (*ent.FriendshipWhereInput, error) { + if v == nil { + return nil, nil + } + res, err := ec.unmarshalInputFriendshipWhereInput(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) marshalOGroup2ᚖentgoᚗioᚋcontribᚋentgqlᚋinternalᚋtodouuidᚋentᚐGroup(ctx context.Context, sel ast.SelectionSet, v *ent.Group) graphql.Marshaler { if v == nil { return graphql.Null diff --git a/entgql/schema.go b/entgql/schema.go index c5800e92c..3900f73f6 100644 --- a/entgql/schema.go +++ b/entgql/schema.go @@ -143,7 +143,7 @@ func (e *schemaGenerator) buildTypes(g *gen.Graph, s *ast.Schema) error { } for _, node := range g.Nodes { - if node.IsEdgeSchema() { + if node.HasCompositeID() { continue } gqlType, ant, err := gqlTypeFromNode(node) @@ -386,7 +386,7 @@ func (e *schemaGenerator) buildFieldEnum(f *gen.Field, gqlType, goType string) ( } func (e *schemaGenerator) buildEdge(node *gen.Type, edge *gen.Edge, edgeAnt *Annotation) ([]*ast.FieldDefinition, error) { - if edge.Type.IsEdgeSchema() { + if edge.Type.HasCompositeID() { return nil, nil } gqlType, ant, err := gqlTypeFromNode(edge.Type) @@ -461,6 +461,9 @@ func (e *schemaGenerator) buildWhereInput(t *gen.Type, nodeGQLType, gqlType stri fields := allFields(t) for _, f := range fields { + if t.IsEdgeSchema() && f.IsEdgeField() { + continue + } ant, err := annotation(f.Annotations) if err != nil { return nil, err @@ -477,6 +480,9 @@ func (e *schemaGenerator) buildWhereInput(t *gen.Type, nodeGQLType, gqlType stri } } + if t.IsEdgeSchema() { + return def, nil + } edges, err := filterEdges(t.Edges, SkipWhereInput) if err != nil { return nil, err diff --git a/entgql/schema_test.go b/entgql/schema_test.go index 077456435..8ad278d53 100644 --- a/entgql/schema_test.go +++ b/entgql/schema_test.go @@ -83,6 +83,23 @@ input CreateTodoInput { categoryID: ID secretID: ID } +""" +CreateUserInput is used for create User object. +Input was generated by ent. +""" +input CreateUserInput { + name: String + groupIDs: [ID!] + friendIDs: [ID!] +} +type Friendship { + id: ID! + createdAt: Time! + userID: ID! + friendID: ID! + user: User! + friend: User! +} type Group { id: ID! name: String! @@ -123,11 +140,23 @@ enum TodoStatus @goModel(model: "entgo.io/contrib/entgql/internal/todo/ent/todo. IN_PROGRESS COMPLETED } +""" +UpdateUserInput is used for update User object. +Input was generated by ent. +""" +input UpdateUserInput { + name: String + addGroupIDs: [ID!] + removeGroupIDs: [ID!] + addFriendIDs: [ID!] + removeFriendIDs: [ID!] +} type User { id: ID! name: String! groups: [Group!] friends: [User!] + friendships: [Friendship!] } `, printSchema(schema)) } @@ -280,6 +309,50 @@ input CreateTodoInput { categoryID: ID secretID: ID } +""" +CreateUserInput is used for create User object. +Input was generated by ent. +""" +input CreateUserInput { + name: String + groupIDs: [ID!] + friendIDs: [ID!] +} +type Friendship implements Node { + id: ID! + createdAt: Time! + userID: ID! + friendID: ID! + user: User! + friend: User! +} +""" +FriendshipWhereInput is used for filtering Friendship objects. +Input was generated by ent. +""" +input FriendshipWhereInput { + not: FriendshipWhereInput + and: [FriendshipWhereInput!] + or: [FriendshipWhereInput!] + """id field predicates""" + id: ID + idNEQ: ID + idIn: [ID!] + idNotIn: [ID!] + idGT: ID + idGTE: ID + idLT: ID + idLTE: ID + """created_at field predicates""" + createdAt: Time + createdAtNEQ: Time + createdAtIn: [Time!] + createdAtNotIn: [Time!] + createdAtGT: Time + createdAtGTE: Time + createdAtLT: Time + createdAtLTE: Time +} type Group implements Node { id: ID! name: String! @@ -549,6 +622,17 @@ input TodoWhereInput { hasCategory: Boolean hasCategoryWith: [CategoryWhereInput!] } +""" +UpdateUserInput is used for update User object. +Input was generated by ent. +""" +input UpdateUserInput { + name: String + addGroupIDs: [ID!] + removeGroupIDs: [ID!] + addFriendIDs: [ID!] + removeFriendIDs: [ID!] +} type User implements Node { id: ID! name: String! @@ -569,6 +653,7 @@ type User implements Node { where: GroupWhereInput ): GroupConnection! friends: [User!] + friendships: [Friendship!] } """A connection to a list of items.""" type UserConnection { @@ -623,6 +708,9 @@ input UserWhereInput { """friends edge predicates""" hasFriends: Boolean hasFriendsWith: [UserWhereInput!] + """friendships edge predicates""" + hasFriendships: Boolean + hasFriendshipsWith: [FriendshipWhereInput!] } `, printSchema(schema)) } diff --git a/entgql/template.go b/entgql/template.go index 96e151fef..20d5c8acc 100644 --- a/entgql/template.go +++ b/entgql/template.go @@ -267,7 +267,8 @@ func (m *MutationDescriptor) InputEdges() ([]*gen.Edge, error) { if err != nil { return nil, err } - if (m.IsCreate && ant.Skip.Is(SkipMutationCreateInput)) || + if e.Type.IsEdgeSchema() || + (m.IsCreate && ant.Skip.Is(SkipMutationCreateInput)) || (!m.IsCreate && ant.Skip.Is(SkipMutationUpdateInput)) { continue } @@ -304,7 +305,7 @@ func mutationInputs(nodes []*gen.Type) ([]*MutationDescriptor, error) { func filterNodes(nodes []*gen.Type, skip SkipMode) ([]*gen.Type, error) { filteredNodes := make([]*gen.Type, 0, len(nodes)) for _, n := range nodes { - if n.IsEdgeSchema() { + if n.HasCompositeID() { continue } ant, err := annotation(n.Annotations) @@ -322,7 +323,7 @@ func filterNodes(nodes []*gen.Type, skip SkipMode) ([]*gen.Type, error) { func filterEdges(edges []*gen.Edge, skip SkipMode) ([]*gen.Edge, error) { filteredEdges := make([]*gen.Edge, 0, len(edges)) for _, e := range edges { - if e.Type.IsEdgeSchema() { + if e.Type.HasCompositeID() { continue } antE, err := annotation(e.Annotations)