// Code generated by ent, DO NOT EDIT. package ent import ( "context" "fmt" "code.icod.de/dalu/ka/ent/category" "code.icod.de/dalu/ka/ent/post" "code.icod.de/dalu/ka/ent/profile" "entgo.io/contrib/entgql" "github.com/99designs/gqlgen/graphql" "github.com/google/uuid" "github.com/hashicorp/go-multierror" ) // Noder wraps the basic Node method. type Noder interface { IsNode() } var categoryImplementors = []string{"Category", "Node"} // IsNode implements the Node interface check for GQLGen. func (*Category) IsNode() {} var postImplementors = []string{"Post", "Node"} // IsNode implements the Node interface check for GQLGen. func (*Post) IsNode() {} var profileImplementors = []string{"Profile", "Node"} // IsNode implements the Node interface check for GQLGen. func (*Profile) IsNode() {} var errNodeInvalidID = &NotFoundError{"node"} // NodeOption allows configuring the Noder execution using functional options. type NodeOption func(*nodeOptions) // WithNodeType sets the node Type resolver function (i.e. the table to query). // If was not provided, the table will be derived from the universal-id // configuration as described in: https://entgo.io/docs/migrate/#universal-ids. func WithNodeType(f func(context.Context, uuid.UUID) (string, error)) NodeOption { return func(o *nodeOptions) { o.nodeType = f } } // WithFixedNodeType sets the Type of the node to a fixed value. func WithFixedNodeType(t string) NodeOption { return WithNodeType(func(context.Context, uuid.UUID) (string, error) { return t, nil }) } type nodeOptions struct { nodeType func(context.Context, uuid.UUID) (string, error) } func (c *Client) newNodeOpts(opts []NodeOption) *nodeOptions { nopts := &nodeOptions{} for _, opt := range opts { opt(nopts) } if nopts.nodeType == nil { nopts.nodeType = func(ctx context.Context, id uuid.UUID) (string, error) { return "", fmt.Errorf("cannot resolve noder (%v) without its type", id) } } return nopts } // Noder returns a Node by its id. If the NodeType was not provided, it will // be derived from the id value according to the universal-id configuration. // // c.Noder(ctx, id) // c.Noder(ctx, id, ent.WithNodeType(typeResolver)) func (c *Client) Noder(ctx context.Context, id uuid.UUID, opts ...NodeOption) (_ Noder, err error) { defer func() { if IsNotFound(err) { err = multierror.Append(err, entgql.ErrNodeNotFound(id)) } }() table, err := c.newNodeOpts(opts).nodeType(ctx, id) if err != nil { return nil, err } return c.noder(ctx, table, id) } func (c *Client) noder(ctx context.Context, table string, id uuid.UUID) (Noder, error) { switch table { case category.Table: query := c.Category.Query(). Where(category.ID(id)) if fc := graphql.GetFieldContext(ctx); fc != nil { if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, categoryImplementors...); err != nil { return nil, err } } return query.Only(ctx) case post.Table: query := c.Post.Query(). Where(post.ID(id)) if fc := graphql.GetFieldContext(ctx); fc != nil { if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, postImplementors...); err != nil { return nil, err } } return query.Only(ctx) case profile.Table: query := c.Profile.Query(). Where(profile.ID(id)) if fc := graphql.GetFieldContext(ctx); fc != nil { if err := query.collectField(ctx, true, graphql.GetOperationContext(ctx), fc.Field, nil, profileImplementors...); err != nil { return nil, err } } return query.Only(ctx) default: return nil, fmt.Errorf("cannot resolve noder from table %q: %w", table, errNodeInvalidID) } } func (c *Client) Noders(ctx context.Context, ids []uuid.UUID, opts ...NodeOption) ([]Noder, error) { switch len(ids) { case 1: noder, err := c.Noder(ctx, ids[0], opts...) if err != nil { return nil, err } return []Noder{noder}, nil case 0: return []Noder{}, nil } noders := make([]Noder, len(ids)) errors := make([]error, len(ids)) tables := make(map[string][]uuid.UUID) id2idx := make(map[uuid.UUID][]int, len(ids)) nopts := c.newNodeOpts(opts) for i, id := range ids { table, err := nopts.nodeType(ctx, id) if err != nil { errors[i] = err continue } tables[table] = append(tables[table], id) id2idx[id] = append(id2idx[id], i) } for table, ids := range tables { nodes, err := c.noders(ctx, table, ids) if err != nil { for _, id := range ids { for _, idx := range id2idx[id] { errors[idx] = err } } } else { for i, id := range ids { for _, idx := range id2idx[id] { noders[idx] = nodes[i] } } } } for i, id := range ids { if errors[i] == nil { if noders[i] != nil { continue } errors[i] = entgql.ErrNodeNotFound(id) } else if IsNotFound(errors[i]) { errors[i] = multierror.Append(errors[i], entgql.ErrNodeNotFound(id)) } ctx := graphql.WithPathContext(ctx, graphql.NewPathWithIndex(i), ) graphql.AddError(ctx, errors[i]) } return noders, nil } func (c *Client) noders(ctx context.Context, table string, ids []uuid.UUID) ([]Noder, error) { noders := make([]Noder, len(ids)) idmap := make(map[uuid.UUID][]*Noder, len(ids)) for i, id := range ids { idmap[id] = append(idmap[id], &noders[i]) } switch table { case category.Table: query := c.Category.Query(). Where(category.IDIn(ids...)) query, err := query.CollectFields(ctx, categoryImplementors...) 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 post.Table: query := c.Post.Query(). Where(post.IDIn(ids...)) query, err := query.CollectFields(ctx, postImplementors...) 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 profile.Table: query := c.Profile.Query(). Where(profile.IDIn(ids...)) query, err := query.CollectFields(ctx, profileImplementors...) 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 } } default: return nil, fmt.Errorf("cannot resolve noders from table %q: %w", table, errNodeInvalidID) } return noders, nil }