// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"errors"
	"fmt"
	"io"
	"strconv"

	"entgo.io/contrib/entgql"
	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/99designs/gqlgen/graphql"
	"github.com/99designs/gqlgen/graphql/errcode"
	"github.com/google/uuid"
	"github.com/in-toto/archivista/ent/attestation"
	"github.com/in-toto/archivista/ent/attestationcollection"
	"github.com/in-toto/archivista/ent/attestationpolicy"
	"github.com/in-toto/archivista/ent/dsse"
	"github.com/in-toto/archivista/ent/payloaddigest"
	"github.com/in-toto/archivista/ent/signature"
	"github.com/in-toto/archivista/ent/statement"
	"github.com/in-toto/archivista/ent/subject"
	"github.com/in-toto/archivista/ent/subjectdigest"
	"github.com/in-toto/archivista/ent/timestamp"
	"github.com/vektah/gqlparser/v2/gqlerror"
)

// Common entgql types.
type (
	Cursor         = entgql.Cursor[uuid.UUID]
	PageInfo       = entgql.PageInfo[uuid.UUID]
	OrderDirection = entgql.OrderDirection
)

func orderFunc(o OrderDirection, field string) func(*sql.Selector) {
	if o == entgql.OrderDirectionDesc {
		return Desc(field)
	}
	return Asc(field)
}

const errInvalidPagination = "INVALID_PAGINATION"

func validateFirstLast(first, last *int) (err *gqlerror.Error) {
	switch {
	case first != nil && last != nil:
		err = &gqlerror.Error{
			Message: "Passing both `first` and `last` to paginate a connection is not supported.",
		}
	case first != nil && *first < 0:
		err = &gqlerror.Error{
			Message: "`first` on a connection cannot be less than zero.",
		}
		errcode.Set(err, errInvalidPagination)
	case last != nil && *last < 0:
		err = &gqlerror.Error{
			Message: "`last` on a connection cannot be less than zero.",
		}
		errcode.Set(err, errInvalidPagination)
	}
	return err
}

func collectedField(ctx context.Context, path ...string) *graphql.CollectedField {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return nil
	}
	field := fc.Field
	oc := graphql.GetOperationContext(ctx)
walk:
	for _, name := range path {
		for _, f := range graphql.CollectFields(oc, field.Selections, nil) {
			if f.Alias == name {
				field = f
				continue walk
			}
		}
		return nil
	}
	return &field
}

func hasCollectedField(ctx context.Context, path ...string) bool {
	if graphql.GetFieldContext(ctx) == nil {
		return true
	}
	return collectedField(ctx, path...) != nil
}

const (
	edgesField      = "edges"
	nodeField       = "node"
	pageInfoField   = "pageInfo"
	totalCountField = "totalCount"
)

func paginateLimit(first, last *int) int {
	var limit int
	if first != nil {
		limit = *first + 1
	} else if last != nil {
		limit = *last + 1
	}
	return limit
}

// AttestationEdge is the edge representation of Attestation.
type AttestationEdge struct {
	Node   *Attestation `json:"node"`
	Cursor Cursor       `json:"cursor"`
}

// AttestationConnection is the connection containing edges to Attestation.
type AttestationConnection struct {
	Edges      []*AttestationEdge `json:"edges"`
	PageInfo   PageInfo           `json:"pageInfo"`
	TotalCount int                `json:"totalCount"`
}

func (c *AttestationConnection) build(nodes []*Attestation, pager *attestationPager, 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) *Attestation
	if last != nil {
		n := len(nodes) - 1
		nodeAt = func(i int) *Attestation {
			return nodes[n-i]
		}
	} else {
		nodeAt = func(i int) *Attestation {
			return nodes[i]
		}
	}
	c.Edges = make([]*AttestationEdge, len(nodes))
	for i := range nodes {
		node := nodeAt(i)
		c.Edges[i] = &AttestationEdge{
			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)
	}
}

// AttestationPaginateOption enables pagination customization.
type AttestationPaginateOption func(*attestationPager) error

// WithAttestationOrder configures pagination ordering.
func WithAttestationOrder(order *AttestationOrder) AttestationPaginateOption {
	if order == nil {
		order = DefaultAttestationOrder
	}
	o := *order
	return func(pager *attestationPager) error {
		if err := o.Direction.Validate(); err != nil {
			return err
		}
		if o.Field == nil {
			o.Field = DefaultAttestationOrder.Field
		}
		pager.order = &o
		return nil
	}
}

// WithAttestationFilter configures pagination filter.
func WithAttestationFilter(filter func(*AttestationQuery) (*AttestationQuery, error)) AttestationPaginateOption {
	return func(pager *attestationPager) error {
		if filter == nil {
			return errors.New("AttestationQuery filter cannot be nil")
		}
		pager.filter = filter
		return nil
	}
}

type attestationPager struct {
	reverse bool
	order   *AttestationOrder
	filter  func(*AttestationQuery) (*AttestationQuery, error)
}

func newAttestationPager(opts []AttestationPaginateOption, reverse bool) (*attestationPager, error) {
	pager := &attestationPager{reverse: reverse}
	for _, opt := range opts {
		if err := opt(pager); err != nil {
			return nil, err
		}
	}
	if pager.order == nil {
		pager.order = DefaultAttestationOrder
	}
	return pager, nil
}

func (p *attestationPager) applyFilter(query *AttestationQuery) (*AttestationQuery, error) {
	if p.filter != nil {
		return p.filter(query)
	}
	return query, nil
}

func (p *attestationPager) toCursor(_m *Attestation) Cursor {
	return p.order.Field.toCursor(_m)
}

func (p *attestationPager) applyCursors(query *AttestationQuery, after, before *Cursor) (*AttestationQuery, error) {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	for _, predicate := range entgql.CursorsPredicate(after, before, DefaultAttestationOrder.Field.column, p.order.Field.column, direction) {
		query = query.Where(predicate)
	}
	return query, nil
}

func (p *attestationPager) applyOrder(query *AttestationQuery) *AttestationQuery {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	query = query.Order(p.order.Field.toTerm(direction.OrderTermOption()))
	if p.order.Field != DefaultAttestationOrder.Field {
		query = query.Order(DefaultAttestationOrder.Field.toTerm(direction.OrderTermOption()))
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return query
}

func (p *attestationPager) orderExpr(query *AttestationQuery) sql.Querier {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return sql.ExprFunc(func(b *sql.Builder) {
		b.Ident(p.order.Field.column).Pad().WriteString(string(direction))
		if p.order.Field != DefaultAttestationOrder.Field {
			b.Comma().Ident(DefaultAttestationOrder.Field.column).Pad().WriteString(string(direction))
		}
	})
}

// Paginate executes the query and returns a relay based cursor connection to Attestation.
func (_m *AttestationQuery) Paginate(
	ctx context.Context, after *Cursor, first *int,
	before *Cursor, last *int, opts ...AttestationPaginateOption,
) (*AttestationConnection, error) {
	if err := validateFirstLast(first, last); err != nil {
		return nil, err
	}
	pager, err := newAttestationPager(opts, last != nil)
	if err != nil {
		return nil, err
	}
	if _m, err = pager.applyFilter(_m); err != nil {
		return nil, err
	}
	conn := &AttestationConnection{Edges: []*AttestationEdge{}}
	ignoredEdges := !hasCollectedField(ctx, edgesField)
	if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) {
		hasPagination := after != nil || first != nil || before != nil || last != nil
		if hasPagination || ignoredEdges {
			c := _m.Clone()
			c.ctx.Fields = nil
			if conn.TotalCount, err = c.Count(ctx); err != nil {
				return nil, err
			}
			conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0
			conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0
		}
	}
	if ignoredEdges || (first != nil && *first == 0) || (last != nil && *last == 0) {
		return conn, nil
	}
	if _m, err = pager.applyCursors(_m, after, before); err != nil {
		return nil, err
	}
	limit := paginateLimit(first, last)
	if limit != 0 {
		_m.Limit(limit)
	}
	if field := collectedField(ctx, edgesField, nodeField); field != nil {
		if err := _m.collectField(ctx, limit == 1, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil {
			return nil, err
		}
	}
	_m = pager.applyOrder(_m)
	nodes, err := _m.All(ctx)
	if err != nil {
		return nil, err
	}
	conn.build(nodes, pager, after, first, before, last)
	return conn, nil
}

// AttestationOrderField defines the ordering field of Attestation.
type AttestationOrderField struct {
	// Value extracts the ordering value from the given Attestation.
	Value    func(*Attestation) (ent.Value, error)
	column   string // field or computed.
	toTerm   func(...sql.OrderTermOption) attestation.OrderOption
	toCursor func(*Attestation) Cursor
}

// AttestationOrder defines the ordering of Attestation.
type AttestationOrder struct {
	Direction OrderDirection         `json:"direction"`
	Field     *AttestationOrderField `json:"field"`
}

// DefaultAttestationOrder is the default ordering of Attestation.
var DefaultAttestationOrder = &AttestationOrder{
	Direction: entgql.OrderDirectionAsc,
	Field: &AttestationOrderField{
		Value: func(_m *Attestation) (ent.Value, error) {
			return _m.ID, nil
		},
		column: attestation.FieldID,
		toTerm: attestation.ByID,
		toCursor: func(_m *Attestation) Cursor {
			return Cursor{ID: _m.ID}
		},
	},
}

// ToEdge converts Attestation into AttestationEdge.
func (_m *Attestation) ToEdge(order *AttestationOrder) *AttestationEdge {
	if order == nil {
		order = DefaultAttestationOrder
	}
	return &AttestationEdge{
		Node:   _m,
		Cursor: order.Field.toCursor(_m),
	}
}

// AttestationCollectionEdge is the edge representation of AttestationCollection.
type AttestationCollectionEdge struct {
	Node   *AttestationCollection `json:"node"`
	Cursor Cursor                 `json:"cursor"`
}

// AttestationCollectionConnection is the connection containing edges to AttestationCollection.
type AttestationCollectionConnection struct {
	Edges      []*AttestationCollectionEdge `json:"edges"`
	PageInfo   PageInfo                     `json:"pageInfo"`
	TotalCount int                          `json:"totalCount"`
}

func (c *AttestationCollectionConnection) build(nodes []*AttestationCollection, pager *attestationcollectionPager, 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) *AttestationCollection
	if last != nil {
		n := len(nodes) - 1
		nodeAt = func(i int) *AttestationCollection {
			return nodes[n-i]
		}
	} else {
		nodeAt = func(i int) *AttestationCollection {
			return nodes[i]
		}
	}
	c.Edges = make([]*AttestationCollectionEdge, len(nodes))
	for i := range nodes {
		node := nodeAt(i)
		c.Edges[i] = &AttestationCollectionEdge{
			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)
	}
}

// AttestationCollectionPaginateOption enables pagination customization.
type AttestationCollectionPaginateOption func(*attestationcollectionPager) error

// WithAttestationCollectionOrder configures pagination ordering.
func WithAttestationCollectionOrder(order *AttestationCollectionOrder) AttestationCollectionPaginateOption {
	if order == nil {
		order = DefaultAttestationCollectionOrder
	}
	o := *order
	return func(pager *attestationcollectionPager) error {
		if err := o.Direction.Validate(); err != nil {
			return err
		}
		if o.Field == nil {
			o.Field = DefaultAttestationCollectionOrder.Field
		}
		pager.order = &o
		return nil
	}
}

// WithAttestationCollectionFilter configures pagination filter.
func WithAttestationCollectionFilter(filter func(*AttestationCollectionQuery) (*AttestationCollectionQuery, error)) AttestationCollectionPaginateOption {
	return func(pager *attestationcollectionPager) error {
		if filter == nil {
			return errors.New("AttestationCollectionQuery filter cannot be nil")
		}
		pager.filter = filter
		return nil
	}
}

type attestationcollectionPager struct {
	reverse bool
	order   *AttestationCollectionOrder
	filter  func(*AttestationCollectionQuery) (*AttestationCollectionQuery, error)
}

func newAttestationCollectionPager(opts []AttestationCollectionPaginateOption, reverse bool) (*attestationcollectionPager, error) {
	pager := &attestationcollectionPager{reverse: reverse}
	for _, opt := range opts {
		if err := opt(pager); err != nil {
			return nil, err
		}
	}
	if pager.order == nil {
		pager.order = DefaultAttestationCollectionOrder
	}
	return pager, nil
}

func (p *attestationcollectionPager) applyFilter(query *AttestationCollectionQuery) (*AttestationCollectionQuery, error) {
	if p.filter != nil {
		return p.filter(query)
	}
	return query, nil
}

func (p *attestationcollectionPager) toCursor(_m *AttestationCollection) Cursor {
	return p.order.Field.toCursor(_m)
}

func (p *attestationcollectionPager) applyCursors(query *AttestationCollectionQuery, after, before *Cursor) (*AttestationCollectionQuery, error) {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	for _, predicate := range entgql.CursorsPredicate(after, before, DefaultAttestationCollectionOrder.Field.column, p.order.Field.column, direction) {
		query = query.Where(predicate)
	}
	return query, nil
}

func (p *attestationcollectionPager) applyOrder(query *AttestationCollectionQuery) *AttestationCollectionQuery {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	query = query.Order(p.order.Field.toTerm(direction.OrderTermOption()))
	if p.order.Field != DefaultAttestationCollectionOrder.Field {
		query = query.Order(DefaultAttestationCollectionOrder.Field.toTerm(direction.OrderTermOption()))
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return query
}

func (p *attestationcollectionPager) orderExpr(query *AttestationCollectionQuery) sql.Querier {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return sql.ExprFunc(func(b *sql.Builder) {
		b.Ident(p.order.Field.column).Pad().WriteString(string(direction))
		if p.order.Field != DefaultAttestationCollectionOrder.Field {
			b.Comma().Ident(DefaultAttestationCollectionOrder.Field.column).Pad().WriteString(string(direction))
		}
	})
}

// Paginate executes the query and returns a relay based cursor connection to AttestationCollection.
func (_m *AttestationCollectionQuery) Paginate(
	ctx context.Context, after *Cursor, first *int,
	before *Cursor, last *int, opts ...AttestationCollectionPaginateOption,
) (*AttestationCollectionConnection, error) {
	if err := validateFirstLast(first, last); err != nil {
		return nil, err
	}
	pager, err := newAttestationCollectionPager(opts, last != nil)
	if err != nil {
		return nil, err
	}
	if _m, err = pager.applyFilter(_m); err != nil {
		return nil, err
	}
	conn := &AttestationCollectionConnection{Edges: []*AttestationCollectionEdge{}}
	ignoredEdges := !hasCollectedField(ctx, edgesField)
	if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) {
		hasPagination := after != nil || first != nil || before != nil || last != nil
		if hasPagination || ignoredEdges {
			c := _m.Clone()
			c.ctx.Fields = nil
			if conn.TotalCount, err = c.Count(ctx); err != nil {
				return nil, err
			}
			conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0
			conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0
		}
	}
	if ignoredEdges || (first != nil && *first == 0) || (last != nil && *last == 0) {
		return conn, nil
	}
	if _m, err = pager.applyCursors(_m, after, before); err != nil {
		return nil, err
	}
	limit := paginateLimit(first, last)
	if limit != 0 {
		_m.Limit(limit)
	}
	if field := collectedField(ctx, edgesField, nodeField); field != nil {
		if err := _m.collectField(ctx, limit == 1, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil {
			return nil, err
		}
	}
	_m = pager.applyOrder(_m)
	nodes, err := _m.All(ctx)
	if err != nil {
		return nil, err
	}
	conn.build(nodes, pager, after, first, before, last)
	return conn, nil
}

// AttestationCollectionOrderField defines the ordering field of AttestationCollection.
type AttestationCollectionOrderField struct {
	// Value extracts the ordering value from the given AttestationCollection.
	Value    func(*AttestationCollection) (ent.Value, error)
	column   string // field or computed.
	toTerm   func(...sql.OrderTermOption) attestationcollection.OrderOption
	toCursor func(*AttestationCollection) Cursor
}

// AttestationCollectionOrder defines the ordering of AttestationCollection.
type AttestationCollectionOrder struct {
	Direction OrderDirection                   `json:"direction"`
	Field     *AttestationCollectionOrderField `json:"field"`
}

// DefaultAttestationCollectionOrder is the default ordering of AttestationCollection.
var DefaultAttestationCollectionOrder = &AttestationCollectionOrder{
	Direction: entgql.OrderDirectionAsc,
	Field: &AttestationCollectionOrderField{
		Value: func(_m *AttestationCollection) (ent.Value, error) {
			return _m.ID, nil
		},
		column: attestationcollection.FieldID,
		toTerm: attestationcollection.ByID,
		toCursor: func(_m *AttestationCollection) Cursor {
			return Cursor{ID: _m.ID}
		},
	},
}

// ToEdge converts AttestationCollection into AttestationCollectionEdge.
func (_m *AttestationCollection) ToEdge(order *AttestationCollectionOrder) *AttestationCollectionEdge {
	if order == nil {
		order = DefaultAttestationCollectionOrder
	}
	return &AttestationCollectionEdge{
		Node:   _m,
		Cursor: order.Field.toCursor(_m),
	}
}

// AttestationPolicyEdge is the edge representation of AttestationPolicy.
type AttestationPolicyEdge struct {
	Node   *AttestationPolicy `json:"node"`
	Cursor Cursor             `json:"cursor"`
}

// AttestationPolicyConnection is the connection containing edges to AttestationPolicy.
type AttestationPolicyConnection struct {
	Edges      []*AttestationPolicyEdge `json:"edges"`
	PageInfo   PageInfo                 `json:"pageInfo"`
	TotalCount int                      `json:"totalCount"`
}

func (c *AttestationPolicyConnection) build(nodes []*AttestationPolicy, pager *attestationpolicyPager, 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) *AttestationPolicy
	if last != nil {
		n := len(nodes) - 1
		nodeAt = func(i int) *AttestationPolicy {
			return nodes[n-i]
		}
	} else {
		nodeAt = func(i int) *AttestationPolicy {
			return nodes[i]
		}
	}
	c.Edges = make([]*AttestationPolicyEdge, len(nodes))
	for i := range nodes {
		node := nodeAt(i)
		c.Edges[i] = &AttestationPolicyEdge{
			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)
	}
}

// AttestationPolicyPaginateOption enables pagination customization.
type AttestationPolicyPaginateOption func(*attestationpolicyPager) error

// WithAttestationPolicyOrder configures pagination ordering.
func WithAttestationPolicyOrder(order *AttestationPolicyOrder) AttestationPolicyPaginateOption {
	if order == nil {
		order = DefaultAttestationPolicyOrder
	}
	o := *order
	return func(pager *attestationpolicyPager) error {
		if err := o.Direction.Validate(); err != nil {
			return err
		}
		if o.Field == nil {
			o.Field = DefaultAttestationPolicyOrder.Field
		}
		pager.order = &o
		return nil
	}
}

// WithAttestationPolicyFilter configures pagination filter.
func WithAttestationPolicyFilter(filter func(*AttestationPolicyQuery) (*AttestationPolicyQuery, error)) AttestationPolicyPaginateOption {
	return func(pager *attestationpolicyPager) error {
		if filter == nil {
			return errors.New("AttestationPolicyQuery filter cannot be nil")
		}
		pager.filter = filter
		return nil
	}
}

type attestationpolicyPager struct {
	reverse bool
	order   *AttestationPolicyOrder
	filter  func(*AttestationPolicyQuery) (*AttestationPolicyQuery, error)
}

func newAttestationPolicyPager(opts []AttestationPolicyPaginateOption, reverse bool) (*attestationpolicyPager, error) {
	pager := &attestationpolicyPager{reverse: reverse}
	for _, opt := range opts {
		if err := opt(pager); err != nil {
			return nil, err
		}
	}
	if pager.order == nil {
		pager.order = DefaultAttestationPolicyOrder
	}
	return pager, nil
}

func (p *attestationpolicyPager) applyFilter(query *AttestationPolicyQuery) (*AttestationPolicyQuery, error) {
	if p.filter != nil {
		return p.filter(query)
	}
	return query, nil
}

func (p *attestationpolicyPager) toCursor(_m *AttestationPolicy) Cursor {
	return p.order.Field.toCursor(_m)
}

func (p *attestationpolicyPager) applyCursors(query *AttestationPolicyQuery, after, before *Cursor) (*AttestationPolicyQuery, error) {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	for _, predicate := range entgql.CursorsPredicate(after, before, DefaultAttestationPolicyOrder.Field.column, p.order.Field.column, direction) {
		query = query.Where(predicate)
	}
	return query, nil
}

func (p *attestationpolicyPager) applyOrder(query *AttestationPolicyQuery) *AttestationPolicyQuery {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	query = query.Order(p.order.Field.toTerm(direction.OrderTermOption()))
	if p.order.Field != DefaultAttestationPolicyOrder.Field {
		query = query.Order(DefaultAttestationPolicyOrder.Field.toTerm(direction.OrderTermOption()))
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return query
}

func (p *attestationpolicyPager) orderExpr(query *AttestationPolicyQuery) sql.Querier {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return sql.ExprFunc(func(b *sql.Builder) {
		b.Ident(p.order.Field.column).Pad().WriteString(string(direction))
		if p.order.Field != DefaultAttestationPolicyOrder.Field {
			b.Comma().Ident(DefaultAttestationPolicyOrder.Field.column).Pad().WriteString(string(direction))
		}
	})
}

// Paginate executes the query and returns a relay based cursor connection to AttestationPolicy.
func (_m *AttestationPolicyQuery) Paginate(
	ctx context.Context, after *Cursor, first *int,
	before *Cursor, last *int, opts ...AttestationPolicyPaginateOption,
) (*AttestationPolicyConnection, error) {
	if err := validateFirstLast(first, last); err != nil {
		return nil, err
	}
	pager, err := newAttestationPolicyPager(opts, last != nil)
	if err != nil {
		return nil, err
	}
	if _m, err = pager.applyFilter(_m); err != nil {
		return nil, err
	}
	conn := &AttestationPolicyConnection{Edges: []*AttestationPolicyEdge{}}
	ignoredEdges := !hasCollectedField(ctx, edgesField)
	if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) {
		hasPagination := after != nil || first != nil || before != nil || last != nil
		if hasPagination || ignoredEdges {
			c := _m.Clone()
			c.ctx.Fields = nil
			if conn.TotalCount, err = c.Count(ctx); err != nil {
				return nil, err
			}
			conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0
			conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0
		}
	}
	if ignoredEdges || (first != nil && *first == 0) || (last != nil && *last == 0) {
		return conn, nil
	}
	if _m, err = pager.applyCursors(_m, after, before); err != nil {
		return nil, err
	}
	limit := paginateLimit(first, last)
	if limit != 0 {
		_m.Limit(limit)
	}
	if field := collectedField(ctx, edgesField, nodeField); field != nil {
		if err := _m.collectField(ctx, limit == 1, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil {
			return nil, err
		}
	}
	_m = pager.applyOrder(_m)
	nodes, err := _m.All(ctx)
	if err != nil {
		return nil, err
	}
	conn.build(nodes, pager, after, first, before, last)
	return conn, nil
}

// AttestationPolicyOrderField defines the ordering field of AttestationPolicy.
type AttestationPolicyOrderField struct {
	// Value extracts the ordering value from the given AttestationPolicy.
	Value    func(*AttestationPolicy) (ent.Value, error)
	column   string // field or computed.
	toTerm   func(...sql.OrderTermOption) attestationpolicy.OrderOption
	toCursor func(*AttestationPolicy) Cursor
}

// AttestationPolicyOrder defines the ordering of AttestationPolicy.
type AttestationPolicyOrder struct {
	Direction OrderDirection               `json:"direction"`
	Field     *AttestationPolicyOrderField `json:"field"`
}

// DefaultAttestationPolicyOrder is the default ordering of AttestationPolicy.
var DefaultAttestationPolicyOrder = &AttestationPolicyOrder{
	Direction: entgql.OrderDirectionAsc,
	Field: &AttestationPolicyOrderField{
		Value: func(_m *AttestationPolicy) (ent.Value, error) {
			return _m.ID, nil
		},
		column: attestationpolicy.FieldID,
		toTerm: attestationpolicy.ByID,
		toCursor: func(_m *AttestationPolicy) Cursor {
			return Cursor{ID: _m.ID}
		},
	},
}

// ToEdge converts AttestationPolicy into AttestationPolicyEdge.
func (_m *AttestationPolicy) ToEdge(order *AttestationPolicyOrder) *AttestationPolicyEdge {
	if order == nil {
		order = DefaultAttestationPolicyOrder
	}
	return &AttestationPolicyEdge{
		Node:   _m,
		Cursor: order.Field.toCursor(_m),
	}
}

// DsseEdge is the edge representation of Dsse.
type DsseEdge struct {
	Node   *Dsse  `json:"node"`
	Cursor Cursor `json:"cursor"`
}

// DsseConnection is the connection containing edges to Dsse.
type DsseConnection struct {
	Edges      []*DsseEdge `json:"edges"`
	PageInfo   PageInfo    `json:"pageInfo"`
	TotalCount int         `json:"totalCount"`
}

func (c *DsseConnection) build(nodes []*Dsse, pager *dssePager, 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) *Dsse
	if last != nil {
		n := len(nodes) - 1
		nodeAt = func(i int) *Dsse {
			return nodes[n-i]
		}
	} else {
		nodeAt = func(i int) *Dsse {
			return nodes[i]
		}
	}
	c.Edges = make([]*DsseEdge, len(nodes))
	for i := range nodes {
		node := nodeAt(i)
		c.Edges[i] = &DsseEdge{
			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)
	}
}

// DssePaginateOption enables pagination customization.
type DssePaginateOption func(*dssePager) error

// WithDsseOrder configures pagination ordering.
func WithDsseOrder(order *DsseOrder) DssePaginateOption {
	if order == nil {
		order = DefaultDsseOrder
	}
	o := *order
	return func(pager *dssePager) error {
		if err := o.Direction.Validate(); err != nil {
			return err
		}
		if o.Field == nil {
			o.Field = DefaultDsseOrder.Field
		}
		pager.order = &o
		return nil
	}
}

// WithDsseFilter configures pagination filter.
func WithDsseFilter(filter func(*DsseQuery) (*DsseQuery, error)) DssePaginateOption {
	return func(pager *dssePager) error {
		if filter == nil {
			return errors.New("DsseQuery filter cannot be nil")
		}
		pager.filter = filter
		return nil
	}
}

type dssePager struct {
	reverse bool
	order   *DsseOrder
	filter  func(*DsseQuery) (*DsseQuery, error)
}

func newDssePager(opts []DssePaginateOption, reverse bool) (*dssePager, error) {
	pager := &dssePager{reverse: reverse}
	for _, opt := range opts {
		if err := opt(pager); err != nil {
			return nil, err
		}
	}
	if pager.order == nil {
		pager.order = DefaultDsseOrder
	}
	return pager, nil
}

func (p *dssePager) applyFilter(query *DsseQuery) (*DsseQuery, error) {
	if p.filter != nil {
		return p.filter(query)
	}
	return query, nil
}

func (p *dssePager) toCursor(_m *Dsse) Cursor {
	return p.order.Field.toCursor(_m)
}

func (p *dssePager) applyCursors(query *DsseQuery, after, before *Cursor) (*DsseQuery, error) {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	for _, predicate := range entgql.CursorsPredicate(after, before, DefaultDsseOrder.Field.column, p.order.Field.column, direction) {
		query = query.Where(predicate)
	}
	return query, nil
}

func (p *dssePager) applyOrder(query *DsseQuery) *DsseQuery {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	query = query.Order(p.order.Field.toTerm(direction.OrderTermOption()))
	if p.order.Field != DefaultDsseOrder.Field {
		query = query.Order(DefaultDsseOrder.Field.toTerm(direction.OrderTermOption()))
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return query
}

func (p *dssePager) orderExpr(query *DsseQuery) sql.Querier {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return sql.ExprFunc(func(b *sql.Builder) {
		b.Ident(p.order.Field.column).Pad().WriteString(string(direction))
		if p.order.Field != DefaultDsseOrder.Field {
			b.Comma().Ident(DefaultDsseOrder.Field.column).Pad().WriteString(string(direction))
		}
	})
}

// Paginate executes the query and returns a relay based cursor connection to Dsse.
func (_m *DsseQuery) Paginate(
	ctx context.Context, after *Cursor, first *int,
	before *Cursor, last *int, opts ...DssePaginateOption,
) (*DsseConnection, error) {
	if err := validateFirstLast(first, last); err != nil {
		return nil, err
	}
	pager, err := newDssePager(opts, last != nil)
	if err != nil {
		return nil, err
	}
	if _m, err = pager.applyFilter(_m); err != nil {
		return nil, err
	}
	conn := &DsseConnection{Edges: []*DsseEdge{}}
	ignoredEdges := !hasCollectedField(ctx, edgesField)
	if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) {
		hasPagination := after != nil || first != nil || before != nil || last != nil
		if hasPagination || ignoredEdges {
			c := _m.Clone()
			c.ctx.Fields = nil
			if conn.TotalCount, err = c.Count(ctx); err != nil {
				return nil, err
			}
			conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0
			conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0
		}
	}
	if ignoredEdges || (first != nil && *first == 0) || (last != nil && *last == 0) {
		return conn, nil
	}
	if _m, err = pager.applyCursors(_m, after, before); err != nil {
		return nil, err
	}
	limit := paginateLimit(first, last)
	if limit != 0 {
		_m.Limit(limit)
	}
	if field := collectedField(ctx, edgesField, nodeField); field != nil {
		if err := _m.collectField(ctx, limit == 1, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil {
			return nil, err
		}
	}
	_m = pager.applyOrder(_m)
	nodes, err := _m.All(ctx)
	if err != nil {
		return nil, err
	}
	conn.build(nodes, pager, after, first, before, last)
	return conn, nil
}

var (
	// DsseOrderFieldCreatedAt orders Dsse by created_at.
	DsseOrderFieldCreatedAt = &DsseOrderField{
		Value: func(_m *Dsse) (ent.Value, error) {
			return _m.CreatedAt, nil
		},
		column: dsse.FieldCreatedAt,
		toTerm: dsse.ByCreatedAt,
		toCursor: func(_m *Dsse) Cursor {
			return Cursor{
				ID:    _m.ID,
				Value: _m.CreatedAt,
			}
		},
	}
)

// String implement fmt.Stringer interface.
func (f DsseOrderField) String() string {
	var str string
	switch f.column {
	case DsseOrderFieldCreatedAt.column:
		str = "CREATED_AT"
	}
	return str
}

// MarshalGQL implements graphql.Marshaler interface.
func (f DsseOrderField) MarshalGQL(w io.Writer) {
	io.WriteString(w, strconv.Quote(f.String()))
}

// UnmarshalGQL implements graphql.Unmarshaler interface.
func (f *DsseOrderField) UnmarshalGQL(v interface{}) error {
	str, ok := v.(string)
	if !ok {
		return fmt.Errorf("DsseOrderField %T must be a string", v)
	}
	switch str {
	case "CREATED_AT":
		*f = *DsseOrderFieldCreatedAt
	default:
		return fmt.Errorf("%s is not a valid DsseOrderField", str)
	}
	return nil
}

// DsseOrderField defines the ordering field of Dsse.
type DsseOrderField struct {
	// Value extracts the ordering value from the given Dsse.
	Value    func(*Dsse) (ent.Value, error)
	column   string // field or computed.
	toTerm   func(...sql.OrderTermOption) dsse.OrderOption
	toCursor func(*Dsse) Cursor
}

// DsseOrder defines the ordering of Dsse.
type DsseOrder struct {
	Direction OrderDirection  `json:"direction"`
	Field     *DsseOrderField `json:"field"`
}

// DefaultDsseOrder is the default ordering of Dsse.
var DefaultDsseOrder = &DsseOrder{
	Direction: entgql.OrderDirectionAsc,
	Field: &DsseOrderField{
		Value: func(_m *Dsse) (ent.Value, error) {
			return _m.ID, nil
		},
		column: dsse.FieldID,
		toTerm: dsse.ByID,
		toCursor: func(_m *Dsse) Cursor {
			return Cursor{ID: _m.ID}
		},
	},
}

// ToEdge converts Dsse into DsseEdge.
func (_m *Dsse) ToEdge(order *DsseOrder) *DsseEdge {
	if order == nil {
		order = DefaultDsseOrder
	}
	return &DsseEdge{
		Node:   _m,
		Cursor: order.Field.toCursor(_m),
	}
}

// PayloadDigestEdge is the edge representation of PayloadDigest.
type PayloadDigestEdge struct {
	Node   *PayloadDigest `json:"node"`
	Cursor Cursor         `json:"cursor"`
}

// PayloadDigestConnection is the connection containing edges to PayloadDigest.
type PayloadDigestConnection struct {
	Edges      []*PayloadDigestEdge `json:"edges"`
	PageInfo   PageInfo             `json:"pageInfo"`
	TotalCount int                  `json:"totalCount"`
}

func (c *PayloadDigestConnection) build(nodes []*PayloadDigest, pager *payloaddigestPager, 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) *PayloadDigest
	if last != nil {
		n := len(nodes) - 1
		nodeAt = func(i int) *PayloadDigest {
			return nodes[n-i]
		}
	} else {
		nodeAt = func(i int) *PayloadDigest {
			return nodes[i]
		}
	}
	c.Edges = make([]*PayloadDigestEdge, len(nodes))
	for i := range nodes {
		node := nodeAt(i)
		c.Edges[i] = &PayloadDigestEdge{
			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)
	}
}

// PayloadDigestPaginateOption enables pagination customization.
type PayloadDigestPaginateOption func(*payloaddigestPager) error

// WithPayloadDigestOrder configures pagination ordering.
func WithPayloadDigestOrder(order *PayloadDigestOrder) PayloadDigestPaginateOption {
	if order == nil {
		order = DefaultPayloadDigestOrder
	}
	o := *order
	return func(pager *payloaddigestPager) error {
		if err := o.Direction.Validate(); err != nil {
			return err
		}
		if o.Field == nil {
			o.Field = DefaultPayloadDigestOrder.Field
		}
		pager.order = &o
		return nil
	}
}

// WithPayloadDigestFilter configures pagination filter.
func WithPayloadDigestFilter(filter func(*PayloadDigestQuery) (*PayloadDigestQuery, error)) PayloadDigestPaginateOption {
	return func(pager *payloaddigestPager) error {
		if filter == nil {
			return errors.New("PayloadDigestQuery filter cannot be nil")
		}
		pager.filter = filter
		return nil
	}
}

type payloaddigestPager struct {
	reverse bool
	order   *PayloadDigestOrder
	filter  func(*PayloadDigestQuery) (*PayloadDigestQuery, error)
}

func newPayloadDigestPager(opts []PayloadDigestPaginateOption, reverse bool) (*payloaddigestPager, error) {
	pager := &payloaddigestPager{reverse: reverse}
	for _, opt := range opts {
		if err := opt(pager); err != nil {
			return nil, err
		}
	}
	if pager.order == nil {
		pager.order = DefaultPayloadDigestOrder
	}
	return pager, nil
}

func (p *payloaddigestPager) applyFilter(query *PayloadDigestQuery) (*PayloadDigestQuery, error) {
	if p.filter != nil {
		return p.filter(query)
	}
	return query, nil
}

func (p *payloaddigestPager) toCursor(_m *PayloadDigest) Cursor {
	return p.order.Field.toCursor(_m)
}

func (p *payloaddigestPager) applyCursors(query *PayloadDigestQuery, after, before *Cursor) (*PayloadDigestQuery, error) {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	for _, predicate := range entgql.CursorsPredicate(after, before, DefaultPayloadDigestOrder.Field.column, p.order.Field.column, direction) {
		query = query.Where(predicate)
	}
	return query, nil
}

func (p *payloaddigestPager) applyOrder(query *PayloadDigestQuery) *PayloadDigestQuery {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	query = query.Order(p.order.Field.toTerm(direction.OrderTermOption()))
	if p.order.Field != DefaultPayloadDigestOrder.Field {
		query = query.Order(DefaultPayloadDigestOrder.Field.toTerm(direction.OrderTermOption()))
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return query
}

func (p *payloaddigestPager) orderExpr(query *PayloadDigestQuery) sql.Querier {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return sql.ExprFunc(func(b *sql.Builder) {
		b.Ident(p.order.Field.column).Pad().WriteString(string(direction))
		if p.order.Field != DefaultPayloadDigestOrder.Field {
			b.Comma().Ident(DefaultPayloadDigestOrder.Field.column).Pad().WriteString(string(direction))
		}
	})
}

// Paginate executes the query and returns a relay based cursor connection to PayloadDigest.
func (_m *PayloadDigestQuery) Paginate(
	ctx context.Context, after *Cursor, first *int,
	before *Cursor, last *int, opts ...PayloadDigestPaginateOption,
) (*PayloadDigestConnection, error) {
	if err := validateFirstLast(first, last); err != nil {
		return nil, err
	}
	pager, err := newPayloadDigestPager(opts, last != nil)
	if err != nil {
		return nil, err
	}
	if _m, err = pager.applyFilter(_m); err != nil {
		return nil, err
	}
	conn := &PayloadDigestConnection{Edges: []*PayloadDigestEdge{}}
	ignoredEdges := !hasCollectedField(ctx, edgesField)
	if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) {
		hasPagination := after != nil || first != nil || before != nil || last != nil
		if hasPagination || ignoredEdges {
			c := _m.Clone()
			c.ctx.Fields = nil
			if conn.TotalCount, err = c.Count(ctx); err != nil {
				return nil, err
			}
			conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0
			conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0
		}
	}
	if ignoredEdges || (first != nil && *first == 0) || (last != nil && *last == 0) {
		return conn, nil
	}
	if _m, err = pager.applyCursors(_m, after, before); err != nil {
		return nil, err
	}
	limit := paginateLimit(first, last)
	if limit != 0 {
		_m.Limit(limit)
	}
	if field := collectedField(ctx, edgesField, nodeField); field != nil {
		if err := _m.collectField(ctx, limit == 1, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil {
			return nil, err
		}
	}
	_m = pager.applyOrder(_m)
	nodes, err := _m.All(ctx)
	if err != nil {
		return nil, err
	}
	conn.build(nodes, pager, after, first, before, last)
	return conn, nil
}

// PayloadDigestOrderField defines the ordering field of PayloadDigest.
type PayloadDigestOrderField struct {
	// Value extracts the ordering value from the given PayloadDigest.
	Value    func(*PayloadDigest) (ent.Value, error)
	column   string // field or computed.
	toTerm   func(...sql.OrderTermOption) payloaddigest.OrderOption
	toCursor func(*PayloadDigest) Cursor
}

// PayloadDigestOrder defines the ordering of PayloadDigest.
type PayloadDigestOrder struct {
	Direction OrderDirection           `json:"direction"`
	Field     *PayloadDigestOrderField `json:"field"`
}

// DefaultPayloadDigestOrder is the default ordering of PayloadDigest.
var DefaultPayloadDigestOrder = &PayloadDigestOrder{
	Direction: entgql.OrderDirectionAsc,
	Field: &PayloadDigestOrderField{
		Value: func(_m *PayloadDigest) (ent.Value, error) {
			return _m.ID, nil
		},
		column: payloaddigest.FieldID,
		toTerm: payloaddigest.ByID,
		toCursor: func(_m *PayloadDigest) Cursor {
			return Cursor{ID: _m.ID}
		},
	},
}

// ToEdge converts PayloadDigest into PayloadDigestEdge.
func (_m *PayloadDigest) ToEdge(order *PayloadDigestOrder) *PayloadDigestEdge {
	if order == nil {
		order = DefaultPayloadDigestOrder
	}
	return &PayloadDigestEdge{
		Node:   _m,
		Cursor: order.Field.toCursor(_m),
	}
}

// SignatureEdge is the edge representation of Signature.
type SignatureEdge struct {
	Node   *Signature `json:"node"`
	Cursor Cursor     `json:"cursor"`
}

// SignatureConnection is the connection containing edges to Signature.
type SignatureConnection struct {
	Edges      []*SignatureEdge `json:"edges"`
	PageInfo   PageInfo         `json:"pageInfo"`
	TotalCount int              `json:"totalCount"`
}

func (c *SignatureConnection) build(nodes []*Signature, pager *signaturePager, 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) *Signature
	if last != nil {
		n := len(nodes) - 1
		nodeAt = func(i int) *Signature {
			return nodes[n-i]
		}
	} else {
		nodeAt = func(i int) *Signature {
			return nodes[i]
		}
	}
	c.Edges = make([]*SignatureEdge, len(nodes))
	for i := range nodes {
		node := nodeAt(i)
		c.Edges[i] = &SignatureEdge{
			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)
	}
}

// SignaturePaginateOption enables pagination customization.
type SignaturePaginateOption func(*signaturePager) error

// WithSignatureOrder configures pagination ordering.
func WithSignatureOrder(order *SignatureOrder) SignaturePaginateOption {
	if order == nil {
		order = DefaultSignatureOrder
	}
	o := *order
	return func(pager *signaturePager) error {
		if err := o.Direction.Validate(); err != nil {
			return err
		}
		if o.Field == nil {
			o.Field = DefaultSignatureOrder.Field
		}
		pager.order = &o
		return nil
	}
}

// WithSignatureFilter configures pagination filter.
func WithSignatureFilter(filter func(*SignatureQuery) (*SignatureQuery, error)) SignaturePaginateOption {
	return func(pager *signaturePager) error {
		if filter == nil {
			return errors.New("SignatureQuery filter cannot be nil")
		}
		pager.filter = filter
		return nil
	}
}

type signaturePager struct {
	reverse bool
	order   *SignatureOrder
	filter  func(*SignatureQuery) (*SignatureQuery, error)
}

func newSignaturePager(opts []SignaturePaginateOption, reverse bool) (*signaturePager, error) {
	pager := &signaturePager{reverse: reverse}
	for _, opt := range opts {
		if err := opt(pager); err != nil {
			return nil, err
		}
	}
	if pager.order == nil {
		pager.order = DefaultSignatureOrder
	}
	return pager, nil
}

func (p *signaturePager) applyFilter(query *SignatureQuery) (*SignatureQuery, error) {
	if p.filter != nil {
		return p.filter(query)
	}
	return query, nil
}

func (p *signaturePager) toCursor(_m *Signature) Cursor {
	return p.order.Field.toCursor(_m)
}

func (p *signaturePager) applyCursors(query *SignatureQuery, after, before *Cursor) (*SignatureQuery, error) {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	for _, predicate := range entgql.CursorsPredicate(after, before, DefaultSignatureOrder.Field.column, p.order.Field.column, direction) {
		query = query.Where(predicate)
	}
	return query, nil
}

func (p *signaturePager) applyOrder(query *SignatureQuery) *SignatureQuery {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	query = query.Order(p.order.Field.toTerm(direction.OrderTermOption()))
	if p.order.Field != DefaultSignatureOrder.Field {
		query = query.Order(DefaultSignatureOrder.Field.toTerm(direction.OrderTermOption()))
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return query
}

func (p *signaturePager) orderExpr(query *SignatureQuery) sql.Querier {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return sql.ExprFunc(func(b *sql.Builder) {
		b.Ident(p.order.Field.column).Pad().WriteString(string(direction))
		if p.order.Field != DefaultSignatureOrder.Field {
			b.Comma().Ident(DefaultSignatureOrder.Field.column).Pad().WriteString(string(direction))
		}
	})
}

// Paginate executes the query and returns a relay based cursor connection to Signature.
func (_m *SignatureQuery) Paginate(
	ctx context.Context, after *Cursor, first *int,
	before *Cursor, last *int, opts ...SignaturePaginateOption,
) (*SignatureConnection, error) {
	if err := validateFirstLast(first, last); err != nil {
		return nil, err
	}
	pager, err := newSignaturePager(opts, last != nil)
	if err != nil {
		return nil, err
	}
	if _m, err = pager.applyFilter(_m); err != nil {
		return nil, err
	}
	conn := &SignatureConnection{Edges: []*SignatureEdge{}}
	ignoredEdges := !hasCollectedField(ctx, edgesField)
	if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) {
		hasPagination := after != nil || first != nil || before != nil || last != nil
		if hasPagination || ignoredEdges {
			c := _m.Clone()
			c.ctx.Fields = nil
			if conn.TotalCount, err = c.Count(ctx); err != nil {
				return nil, err
			}
			conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0
			conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0
		}
	}
	if ignoredEdges || (first != nil && *first == 0) || (last != nil && *last == 0) {
		return conn, nil
	}
	if _m, err = pager.applyCursors(_m, after, before); err != nil {
		return nil, err
	}
	limit := paginateLimit(first, last)
	if limit != 0 {
		_m.Limit(limit)
	}
	if field := collectedField(ctx, edgesField, nodeField); field != nil {
		if err := _m.collectField(ctx, limit == 1, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil {
			return nil, err
		}
	}
	_m = pager.applyOrder(_m)
	nodes, err := _m.All(ctx)
	if err != nil {
		return nil, err
	}
	conn.build(nodes, pager, after, first, before, last)
	return conn, nil
}

// SignatureOrderField defines the ordering field of Signature.
type SignatureOrderField struct {
	// Value extracts the ordering value from the given Signature.
	Value    func(*Signature) (ent.Value, error)
	column   string // field or computed.
	toTerm   func(...sql.OrderTermOption) signature.OrderOption
	toCursor func(*Signature) Cursor
}

// SignatureOrder defines the ordering of Signature.
type SignatureOrder struct {
	Direction OrderDirection       `json:"direction"`
	Field     *SignatureOrderField `json:"field"`
}

// DefaultSignatureOrder is the default ordering of Signature.
var DefaultSignatureOrder = &SignatureOrder{
	Direction: entgql.OrderDirectionAsc,
	Field: &SignatureOrderField{
		Value: func(_m *Signature) (ent.Value, error) {
			return _m.ID, nil
		},
		column: signature.FieldID,
		toTerm: signature.ByID,
		toCursor: func(_m *Signature) Cursor {
			return Cursor{ID: _m.ID}
		},
	},
}

// ToEdge converts Signature into SignatureEdge.
func (_m *Signature) ToEdge(order *SignatureOrder) *SignatureEdge {
	if order == nil {
		order = DefaultSignatureOrder
	}
	return &SignatureEdge{
		Node:   _m,
		Cursor: order.Field.toCursor(_m),
	}
}

// StatementEdge is the edge representation of Statement.
type StatementEdge struct {
	Node   *Statement `json:"node"`
	Cursor Cursor     `json:"cursor"`
}

// StatementConnection is the connection containing edges to Statement.
type StatementConnection struct {
	Edges      []*StatementEdge `json:"edges"`
	PageInfo   PageInfo         `json:"pageInfo"`
	TotalCount int              `json:"totalCount"`
}

func (c *StatementConnection) build(nodes []*Statement, pager *statementPager, 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) *Statement
	if last != nil {
		n := len(nodes) - 1
		nodeAt = func(i int) *Statement {
			return nodes[n-i]
		}
	} else {
		nodeAt = func(i int) *Statement {
			return nodes[i]
		}
	}
	c.Edges = make([]*StatementEdge, len(nodes))
	for i := range nodes {
		node := nodeAt(i)
		c.Edges[i] = &StatementEdge{
			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)
	}
}

// StatementPaginateOption enables pagination customization.
type StatementPaginateOption func(*statementPager) error

// WithStatementOrder configures pagination ordering.
func WithStatementOrder(order *StatementOrder) StatementPaginateOption {
	if order == nil {
		order = DefaultStatementOrder
	}
	o := *order
	return func(pager *statementPager) error {
		if err := o.Direction.Validate(); err != nil {
			return err
		}
		if o.Field == nil {
			o.Field = DefaultStatementOrder.Field
		}
		pager.order = &o
		return nil
	}
}

// WithStatementFilter configures pagination filter.
func WithStatementFilter(filter func(*StatementQuery) (*StatementQuery, error)) StatementPaginateOption {
	return func(pager *statementPager) error {
		if filter == nil {
			return errors.New("StatementQuery filter cannot be nil")
		}
		pager.filter = filter
		return nil
	}
}

type statementPager struct {
	reverse bool
	order   *StatementOrder
	filter  func(*StatementQuery) (*StatementQuery, error)
}

func newStatementPager(opts []StatementPaginateOption, reverse bool) (*statementPager, error) {
	pager := &statementPager{reverse: reverse}
	for _, opt := range opts {
		if err := opt(pager); err != nil {
			return nil, err
		}
	}
	if pager.order == nil {
		pager.order = DefaultStatementOrder
	}
	return pager, nil
}

func (p *statementPager) applyFilter(query *StatementQuery) (*StatementQuery, error) {
	if p.filter != nil {
		return p.filter(query)
	}
	return query, nil
}

func (p *statementPager) toCursor(_m *Statement) Cursor {
	return p.order.Field.toCursor(_m)
}

func (p *statementPager) applyCursors(query *StatementQuery, after, before *Cursor) (*StatementQuery, error) {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	for _, predicate := range entgql.CursorsPredicate(after, before, DefaultStatementOrder.Field.column, p.order.Field.column, direction) {
		query = query.Where(predicate)
	}
	return query, nil
}

func (p *statementPager) applyOrder(query *StatementQuery) *StatementQuery {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	query = query.Order(p.order.Field.toTerm(direction.OrderTermOption()))
	if p.order.Field != DefaultStatementOrder.Field {
		query = query.Order(DefaultStatementOrder.Field.toTerm(direction.OrderTermOption()))
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return query
}

func (p *statementPager) orderExpr(query *StatementQuery) sql.Querier {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return sql.ExprFunc(func(b *sql.Builder) {
		b.Ident(p.order.Field.column).Pad().WriteString(string(direction))
		if p.order.Field != DefaultStatementOrder.Field {
			b.Comma().Ident(DefaultStatementOrder.Field.column).Pad().WriteString(string(direction))
		}
	})
}

// Paginate executes the query and returns a relay based cursor connection to Statement.
func (_m *StatementQuery) Paginate(
	ctx context.Context, after *Cursor, first *int,
	before *Cursor, last *int, opts ...StatementPaginateOption,
) (*StatementConnection, error) {
	if err := validateFirstLast(first, last); err != nil {
		return nil, err
	}
	pager, err := newStatementPager(opts, last != nil)
	if err != nil {
		return nil, err
	}
	if _m, err = pager.applyFilter(_m); err != nil {
		return nil, err
	}
	conn := &StatementConnection{Edges: []*StatementEdge{}}
	ignoredEdges := !hasCollectedField(ctx, edgesField)
	if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) {
		hasPagination := after != nil || first != nil || before != nil || last != nil
		if hasPagination || ignoredEdges {
			c := _m.Clone()
			c.ctx.Fields = nil
			if conn.TotalCount, err = c.Count(ctx); err != nil {
				return nil, err
			}
			conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0
			conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0
		}
	}
	if ignoredEdges || (first != nil && *first == 0) || (last != nil && *last == 0) {
		return conn, nil
	}
	if _m, err = pager.applyCursors(_m, after, before); err != nil {
		return nil, err
	}
	limit := paginateLimit(first, last)
	if limit != 0 {
		_m.Limit(limit)
	}
	if field := collectedField(ctx, edgesField, nodeField); field != nil {
		if err := _m.collectField(ctx, limit == 1, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil {
			return nil, err
		}
	}
	_m = pager.applyOrder(_m)
	nodes, err := _m.All(ctx)
	if err != nil {
		return nil, err
	}
	conn.build(nodes, pager, after, first, before, last)
	return conn, nil
}

// StatementOrderField defines the ordering field of Statement.
type StatementOrderField struct {
	// Value extracts the ordering value from the given Statement.
	Value    func(*Statement) (ent.Value, error)
	column   string // field or computed.
	toTerm   func(...sql.OrderTermOption) statement.OrderOption
	toCursor func(*Statement) Cursor
}

// StatementOrder defines the ordering of Statement.
type StatementOrder struct {
	Direction OrderDirection       `json:"direction"`
	Field     *StatementOrderField `json:"field"`
}

// DefaultStatementOrder is the default ordering of Statement.
var DefaultStatementOrder = &StatementOrder{
	Direction: entgql.OrderDirectionAsc,
	Field: &StatementOrderField{
		Value: func(_m *Statement) (ent.Value, error) {
			return _m.ID, nil
		},
		column: statement.FieldID,
		toTerm: statement.ByID,
		toCursor: func(_m *Statement) Cursor {
			return Cursor{ID: _m.ID}
		},
	},
}

// ToEdge converts Statement into StatementEdge.
func (_m *Statement) ToEdge(order *StatementOrder) *StatementEdge {
	if order == nil {
		order = DefaultStatementOrder
	}
	return &StatementEdge{
		Node:   _m,
		Cursor: order.Field.toCursor(_m),
	}
}

// SubjectEdge is the edge representation of Subject.
type SubjectEdge struct {
	Node   *Subject `json:"node"`
	Cursor Cursor   `json:"cursor"`
}

// SubjectConnection is the connection containing edges to Subject.
type SubjectConnection struct {
	Edges      []*SubjectEdge `json:"edges"`
	PageInfo   PageInfo       `json:"pageInfo"`
	TotalCount int            `json:"totalCount"`
}

func (c *SubjectConnection) build(nodes []*Subject, pager *subjectPager, 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) *Subject
	if last != nil {
		n := len(nodes) - 1
		nodeAt = func(i int) *Subject {
			return nodes[n-i]
		}
	} else {
		nodeAt = func(i int) *Subject {
			return nodes[i]
		}
	}
	c.Edges = make([]*SubjectEdge, len(nodes))
	for i := range nodes {
		node := nodeAt(i)
		c.Edges[i] = &SubjectEdge{
			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)
	}
}

// SubjectPaginateOption enables pagination customization.
type SubjectPaginateOption func(*subjectPager) error

// WithSubjectOrder configures pagination ordering.
func WithSubjectOrder(order *SubjectOrder) SubjectPaginateOption {
	if order == nil {
		order = DefaultSubjectOrder
	}
	o := *order
	return func(pager *subjectPager) error {
		if err := o.Direction.Validate(); err != nil {
			return err
		}
		if o.Field == nil {
			o.Field = DefaultSubjectOrder.Field
		}
		pager.order = &o
		return nil
	}
}

// WithSubjectFilter configures pagination filter.
func WithSubjectFilter(filter func(*SubjectQuery) (*SubjectQuery, error)) SubjectPaginateOption {
	return func(pager *subjectPager) error {
		if filter == nil {
			return errors.New("SubjectQuery filter cannot be nil")
		}
		pager.filter = filter
		return nil
	}
}

type subjectPager struct {
	reverse bool
	order   *SubjectOrder
	filter  func(*SubjectQuery) (*SubjectQuery, error)
}

func newSubjectPager(opts []SubjectPaginateOption, reverse bool) (*subjectPager, error) {
	pager := &subjectPager{reverse: reverse}
	for _, opt := range opts {
		if err := opt(pager); err != nil {
			return nil, err
		}
	}
	if pager.order == nil {
		pager.order = DefaultSubjectOrder
	}
	return pager, nil
}

func (p *subjectPager) applyFilter(query *SubjectQuery) (*SubjectQuery, error) {
	if p.filter != nil {
		return p.filter(query)
	}
	return query, nil
}

func (p *subjectPager) toCursor(_m *Subject) Cursor {
	return p.order.Field.toCursor(_m)
}

func (p *subjectPager) applyCursors(query *SubjectQuery, after, before *Cursor) (*SubjectQuery, error) {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	for _, predicate := range entgql.CursorsPredicate(after, before, DefaultSubjectOrder.Field.column, p.order.Field.column, direction) {
		query = query.Where(predicate)
	}
	return query, nil
}

func (p *subjectPager) applyOrder(query *SubjectQuery) *SubjectQuery {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	query = query.Order(p.order.Field.toTerm(direction.OrderTermOption()))
	if p.order.Field != DefaultSubjectOrder.Field {
		query = query.Order(DefaultSubjectOrder.Field.toTerm(direction.OrderTermOption()))
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return query
}

func (p *subjectPager) orderExpr(query *SubjectQuery) sql.Querier {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return sql.ExprFunc(func(b *sql.Builder) {
		b.Ident(p.order.Field.column).Pad().WriteString(string(direction))
		if p.order.Field != DefaultSubjectOrder.Field {
			b.Comma().Ident(DefaultSubjectOrder.Field.column).Pad().WriteString(string(direction))
		}
	})
}

// Paginate executes the query and returns a relay based cursor connection to Subject.
func (_m *SubjectQuery) Paginate(
	ctx context.Context, after *Cursor, first *int,
	before *Cursor, last *int, opts ...SubjectPaginateOption,
) (*SubjectConnection, error) {
	if err := validateFirstLast(first, last); err != nil {
		return nil, err
	}
	pager, err := newSubjectPager(opts, last != nil)
	if err != nil {
		return nil, err
	}
	if _m, err = pager.applyFilter(_m); err != nil {
		return nil, err
	}
	conn := &SubjectConnection{Edges: []*SubjectEdge{}}
	ignoredEdges := !hasCollectedField(ctx, edgesField)
	if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) {
		hasPagination := after != nil || first != nil || before != nil || last != nil
		if hasPagination || ignoredEdges {
			c := _m.Clone()
			c.ctx.Fields = nil
			if conn.TotalCount, err = c.Count(ctx); err != nil {
				return nil, err
			}
			conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0
			conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0
		}
	}
	if ignoredEdges || (first != nil && *first == 0) || (last != nil && *last == 0) {
		return conn, nil
	}
	if _m, err = pager.applyCursors(_m, after, before); err != nil {
		return nil, err
	}
	limit := paginateLimit(first, last)
	if limit != 0 {
		_m.Limit(limit)
	}
	if field := collectedField(ctx, edgesField, nodeField); field != nil {
		if err := _m.collectField(ctx, limit == 1, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil {
			return nil, err
		}
	}
	_m = pager.applyOrder(_m)
	nodes, err := _m.All(ctx)
	if err != nil {
		return nil, err
	}
	conn.build(nodes, pager, after, first, before, last)
	return conn, nil
}

var (
	// SubjectOrderFieldCreatedAt orders Subject by created_at.
	SubjectOrderFieldCreatedAt = &SubjectOrderField{
		Value: func(_m *Subject) (ent.Value, error) {
			return _m.CreatedAt, nil
		},
		column: subject.FieldCreatedAt,
		toTerm: subject.ByCreatedAt,
		toCursor: func(_m *Subject) Cursor {
			return Cursor{
				ID:    _m.ID,
				Value: _m.CreatedAt,
			}
		},
	}
)

// String implement fmt.Stringer interface.
func (f SubjectOrderField) String() string {
	var str string
	switch f.column {
	case SubjectOrderFieldCreatedAt.column:
		str = "CREATED_AT"
	}
	return str
}

// MarshalGQL implements graphql.Marshaler interface.
func (f SubjectOrderField) MarshalGQL(w io.Writer) {
	io.WriteString(w, strconv.Quote(f.String()))
}

// UnmarshalGQL implements graphql.Unmarshaler interface.
func (f *SubjectOrderField) UnmarshalGQL(v interface{}) error {
	str, ok := v.(string)
	if !ok {
		return fmt.Errorf("SubjectOrderField %T must be a string", v)
	}
	switch str {
	case "CREATED_AT":
		*f = *SubjectOrderFieldCreatedAt
	default:
		return fmt.Errorf("%s is not a valid SubjectOrderField", str)
	}
	return nil
}

// SubjectOrderField defines the ordering field of Subject.
type SubjectOrderField struct {
	// Value extracts the ordering value from the given Subject.
	Value    func(*Subject) (ent.Value, error)
	column   string // field or computed.
	toTerm   func(...sql.OrderTermOption) subject.OrderOption
	toCursor func(*Subject) Cursor
}

// SubjectOrder defines the ordering of Subject.
type SubjectOrder struct {
	Direction OrderDirection     `json:"direction"`
	Field     *SubjectOrderField `json:"field"`
}

// DefaultSubjectOrder is the default ordering of Subject.
var DefaultSubjectOrder = &SubjectOrder{
	Direction: entgql.OrderDirectionAsc,
	Field: &SubjectOrderField{
		Value: func(_m *Subject) (ent.Value, error) {
			return _m.ID, nil
		},
		column: subject.FieldID,
		toTerm: subject.ByID,
		toCursor: func(_m *Subject) Cursor {
			return Cursor{ID: _m.ID}
		},
	},
}

// ToEdge converts Subject into SubjectEdge.
func (_m *Subject) ToEdge(order *SubjectOrder) *SubjectEdge {
	if order == nil {
		order = DefaultSubjectOrder
	}
	return &SubjectEdge{
		Node:   _m,
		Cursor: order.Field.toCursor(_m),
	}
}

// SubjectDigestEdge is the edge representation of SubjectDigest.
type SubjectDigestEdge struct {
	Node   *SubjectDigest `json:"node"`
	Cursor Cursor         `json:"cursor"`
}

// SubjectDigestConnection is the connection containing edges to SubjectDigest.
type SubjectDigestConnection struct {
	Edges      []*SubjectDigestEdge `json:"edges"`
	PageInfo   PageInfo             `json:"pageInfo"`
	TotalCount int                  `json:"totalCount"`
}

func (c *SubjectDigestConnection) build(nodes []*SubjectDigest, pager *subjectdigestPager, 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) *SubjectDigest
	if last != nil {
		n := len(nodes) - 1
		nodeAt = func(i int) *SubjectDigest {
			return nodes[n-i]
		}
	} else {
		nodeAt = func(i int) *SubjectDigest {
			return nodes[i]
		}
	}
	c.Edges = make([]*SubjectDigestEdge, len(nodes))
	for i := range nodes {
		node := nodeAt(i)
		c.Edges[i] = &SubjectDigestEdge{
			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)
	}
}

// SubjectDigestPaginateOption enables pagination customization.
type SubjectDigestPaginateOption func(*subjectdigestPager) error

// WithSubjectDigestOrder configures pagination ordering.
func WithSubjectDigestOrder(order *SubjectDigestOrder) SubjectDigestPaginateOption {
	if order == nil {
		order = DefaultSubjectDigestOrder
	}
	o := *order
	return func(pager *subjectdigestPager) error {
		if err := o.Direction.Validate(); err != nil {
			return err
		}
		if o.Field == nil {
			o.Field = DefaultSubjectDigestOrder.Field
		}
		pager.order = &o
		return nil
	}
}

// WithSubjectDigestFilter configures pagination filter.
func WithSubjectDigestFilter(filter func(*SubjectDigestQuery) (*SubjectDigestQuery, error)) SubjectDigestPaginateOption {
	return func(pager *subjectdigestPager) error {
		if filter == nil {
			return errors.New("SubjectDigestQuery filter cannot be nil")
		}
		pager.filter = filter
		return nil
	}
}

type subjectdigestPager struct {
	reverse bool
	order   *SubjectDigestOrder
	filter  func(*SubjectDigestQuery) (*SubjectDigestQuery, error)
}

func newSubjectDigestPager(opts []SubjectDigestPaginateOption, reverse bool) (*subjectdigestPager, error) {
	pager := &subjectdigestPager{reverse: reverse}
	for _, opt := range opts {
		if err := opt(pager); err != nil {
			return nil, err
		}
	}
	if pager.order == nil {
		pager.order = DefaultSubjectDigestOrder
	}
	return pager, nil
}

func (p *subjectdigestPager) applyFilter(query *SubjectDigestQuery) (*SubjectDigestQuery, error) {
	if p.filter != nil {
		return p.filter(query)
	}
	return query, nil
}

func (p *subjectdigestPager) toCursor(_m *SubjectDigest) Cursor {
	return p.order.Field.toCursor(_m)
}

func (p *subjectdigestPager) applyCursors(query *SubjectDigestQuery, after, before *Cursor) (*SubjectDigestQuery, error) {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	for _, predicate := range entgql.CursorsPredicate(after, before, DefaultSubjectDigestOrder.Field.column, p.order.Field.column, direction) {
		query = query.Where(predicate)
	}
	return query, nil
}

func (p *subjectdigestPager) applyOrder(query *SubjectDigestQuery) *SubjectDigestQuery {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	query = query.Order(p.order.Field.toTerm(direction.OrderTermOption()))
	if p.order.Field != DefaultSubjectDigestOrder.Field {
		query = query.Order(DefaultSubjectDigestOrder.Field.toTerm(direction.OrderTermOption()))
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return query
}

func (p *subjectdigestPager) orderExpr(query *SubjectDigestQuery) sql.Querier {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return sql.ExprFunc(func(b *sql.Builder) {
		b.Ident(p.order.Field.column).Pad().WriteString(string(direction))
		if p.order.Field != DefaultSubjectDigestOrder.Field {
			b.Comma().Ident(DefaultSubjectDigestOrder.Field.column).Pad().WriteString(string(direction))
		}
	})
}

// Paginate executes the query and returns a relay based cursor connection to SubjectDigest.
func (_m *SubjectDigestQuery) Paginate(
	ctx context.Context, after *Cursor, first *int,
	before *Cursor, last *int, opts ...SubjectDigestPaginateOption,
) (*SubjectDigestConnection, error) {
	if err := validateFirstLast(first, last); err != nil {
		return nil, err
	}
	pager, err := newSubjectDigestPager(opts, last != nil)
	if err != nil {
		return nil, err
	}
	if _m, err = pager.applyFilter(_m); err != nil {
		return nil, err
	}
	conn := &SubjectDigestConnection{Edges: []*SubjectDigestEdge{}}
	ignoredEdges := !hasCollectedField(ctx, edgesField)
	if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) {
		hasPagination := after != nil || first != nil || before != nil || last != nil
		if hasPagination || ignoredEdges {
			c := _m.Clone()
			c.ctx.Fields = nil
			if conn.TotalCount, err = c.Count(ctx); err != nil {
				return nil, err
			}
			conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0
			conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0
		}
	}
	if ignoredEdges || (first != nil && *first == 0) || (last != nil && *last == 0) {
		return conn, nil
	}
	if _m, err = pager.applyCursors(_m, after, before); err != nil {
		return nil, err
	}
	limit := paginateLimit(first, last)
	if limit != 0 {
		_m.Limit(limit)
	}
	if field := collectedField(ctx, edgesField, nodeField); field != nil {
		if err := _m.collectField(ctx, limit == 1, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil {
			return nil, err
		}
	}
	_m = pager.applyOrder(_m)
	nodes, err := _m.All(ctx)
	if err != nil {
		return nil, err
	}
	conn.build(nodes, pager, after, first, before, last)
	return conn, nil
}

// SubjectDigestOrderField defines the ordering field of SubjectDigest.
type SubjectDigestOrderField struct {
	// Value extracts the ordering value from the given SubjectDigest.
	Value    func(*SubjectDigest) (ent.Value, error)
	column   string // field or computed.
	toTerm   func(...sql.OrderTermOption) subjectdigest.OrderOption
	toCursor func(*SubjectDigest) Cursor
}

// SubjectDigestOrder defines the ordering of SubjectDigest.
type SubjectDigestOrder struct {
	Direction OrderDirection           `json:"direction"`
	Field     *SubjectDigestOrderField `json:"field"`
}

// DefaultSubjectDigestOrder is the default ordering of SubjectDigest.
var DefaultSubjectDigestOrder = &SubjectDigestOrder{
	Direction: entgql.OrderDirectionAsc,
	Field: &SubjectDigestOrderField{
		Value: func(_m *SubjectDigest) (ent.Value, error) {
			return _m.ID, nil
		},
		column: subjectdigest.FieldID,
		toTerm: subjectdigest.ByID,
		toCursor: func(_m *SubjectDigest) Cursor {
			return Cursor{ID: _m.ID}
		},
	},
}

// ToEdge converts SubjectDigest into SubjectDigestEdge.
func (_m *SubjectDigest) ToEdge(order *SubjectDigestOrder) *SubjectDigestEdge {
	if order == nil {
		order = DefaultSubjectDigestOrder
	}
	return &SubjectDigestEdge{
		Node:   _m,
		Cursor: order.Field.toCursor(_m),
	}
}

// TimestampEdge is the edge representation of Timestamp.
type TimestampEdge struct {
	Node   *Timestamp `json:"node"`
	Cursor Cursor     `json:"cursor"`
}

// TimestampConnection is the connection containing edges to Timestamp.
type TimestampConnection struct {
	Edges      []*TimestampEdge `json:"edges"`
	PageInfo   PageInfo         `json:"pageInfo"`
	TotalCount int              `json:"totalCount"`
}

func (c *TimestampConnection) build(nodes []*Timestamp, pager *timestampPager, 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) *Timestamp
	if last != nil {
		n := len(nodes) - 1
		nodeAt = func(i int) *Timestamp {
			return nodes[n-i]
		}
	} else {
		nodeAt = func(i int) *Timestamp {
			return nodes[i]
		}
	}
	c.Edges = make([]*TimestampEdge, len(nodes))
	for i := range nodes {
		node := nodeAt(i)
		c.Edges[i] = &TimestampEdge{
			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)
	}
}

// TimestampPaginateOption enables pagination customization.
type TimestampPaginateOption func(*timestampPager) error

// WithTimestampOrder configures pagination ordering.
func WithTimestampOrder(order *TimestampOrder) TimestampPaginateOption {
	if order == nil {
		order = DefaultTimestampOrder
	}
	o := *order
	return func(pager *timestampPager) error {
		if err := o.Direction.Validate(); err != nil {
			return err
		}
		if o.Field == nil {
			o.Field = DefaultTimestampOrder.Field
		}
		pager.order = &o
		return nil
	}
}

// WithTimestampFilter configures pagination filter.
func WithTimestampFilter(filter func(*TimestampQuery) (*TimestampQuery, error)) TimestampPaginateOption {
	return func(pager *timestampPager) error {
		if filter == nil {
			return errors.New("TimestampQuery filter cannot be nil")
		}
		pager.filter = filter
		return nil
	}
}

type timestampPager struct {
	reverse bool
	order   *TimestampOrder
	filter  func(*TimestampQuery) (*TimestampQuery, error)
}

func newTimestampPager(opts []TimestampPaginateOption, reverse bool) (*timestampPager, error) {
	pager := &timestampPager{reverse: reverse}
	for _, opt := range opts {
		if err := opt(pager); err != nil {
			return nil, err
		}
	}
	if pager.order == nil {
		pager.order = DefaultTimestampOrder
	}
	return pager, nil
}

func (p *timestampPager) applyFilter(query *TimestampQuery) (*TimestampQuery, error) {
	if p.filter != nil {
		return p.filter(query)
	}
	return query, nil
}

func (p *timestampPager) toCursor(_m *Timestamp) Cursor {
	return p.order.Field.toCursor(_m)
}

func (p *timestampPager) applyCursors(query *TimestampQuery, after, before *Cursor) (*TimestampQuery, error) {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	for _, predicate := range entgql.CursorsPredicate(after, before, DefaultTimestampOrder.Field.column, p.order.Field.column, direction) {
		query = query.Where(predicate)
	}
	return query, nil
}

func (p *timestampPager) applyOrder(query *TimestampQuery) *TimestampQuery {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	query = query.Order(p.order.Field.toTerm(direction.OrderTermOption()))
	if p.order.Field != DefaultTimestampOrder.Field {
		query = query.Order(DefaultTimestampOrder.Field.toTerm(direction.OrderTermOption()))
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return query
}

func (p *timestampPager) orderExpr(query *TimestampQuery) sql.Querier {
	direction := p.order.Direction
	if p.reverse {
		direction = direction.Reverse()
	}
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(p.order.Field.column)
	}
	return sql.ExprFunc(func(b *sql.Builder) {
		b.Ident(p.order.Field.column).Pad().WriteString(string(direction))
		if p.order.Field != DefaultTimestampOrder.Field {
			b.Comma().Ident(DefaultTimestampOrder.Field.column).Pad().WriteString(string(direction))
		}
	})
}

// Paginate executes the query and returns a relay based cursor connection to Timestamp.
func (_m *TimestampQuery) Paginate(
	ctx context.Context, after *Cursor, first *int,
	before *Cursor, last *int, opts ...TimestampPaginateOption,
) (*TimestampConnection, error) {
	if err := validateFirstLast(first, last); err != nil {
		return nil, err
	}
	pager, err := newTimestampPager(opts, last != nil)
	if err != nil {
		return nil, err
	}
	if _m, err = pager.applyFilter(_m); err != nil {
		return nil, err
	}
	conn := &TimestampConnection{Edges: []*TimestampEdge{}}
	ignoredEdges := !hasCollectedField(ctx, edgesField)
	if hasCollectedField(ctx, totalCountField) || hasCollectedField(ctx, pageInfoField) {
		hasPagination := after != nil || first != nil || before != nil || last != nil
		if hasPagination || ignoredEdges {
			c := _m.Clone()
			c.ctx.Fields = nil
			if conn.TotalCount, err = c.Count(ctx); err != nil {
				return nil, err
			}
			conn.PageInfo.HasNextPage = first != nil && conn.TotalCount > 0
			conn.PageInfo.HasPreviousPage = last != nil && conn.TotalCount > 0
		}
	}
	if ignoredEdges || (first != nil && *first == 0) || (last != nil && *last == 0) {
		return conn, nil
	}
	if _m, err = pager.applyCursors(_m, after, before); err != nil {
		return nil, err
	}
	limit := paginateLimit(first, last)
	if limit != 0 {
		_m.Limit(limit)
	}
	if field := collectedField(ctx, edgesField, nodeField); field != nil {
		if err := _m.collectField(ctx, limit == 1, graphql.GetOperationContext(ctx), *field, []string{edgesField, nodeField}); err != nil {
			return nil, err
		}
	}
	_m = pager.applyOrder(_m)
	nodes, err := _m.All(ctx)
	if err != nil {
		return nil, err
	}
	conn.build(nodes, pager, after, first, before, last)
	return conn, nil
}

// TimestampOrderField defines the ordering field of Timestamp.
type TimestampOrderField struct {
	// Value extracts the ordering value from the given Timestamp.
	Value    func(*Timestamp) (ent.Value, error)
	column   string // field or computed.
	toTerm   func(...sql.OrderTermOption) timestamp.OrderOption
	toCursor func(*Timestamp) Cursor
}

// TimestampOrder defines the ordering of Timestamp.
type TimestampOrder struct {
	Direction OrderDirection       `json:"direction"`
	Field     *TimestampOrderField `json:"field"`
}

// DefaultTimestampOrder is the default ordering of Timestamp.
var DefaultTimestampOrder = &TimestampOrder{
	Direction: entgql.OrderDirectionAsc,
	Field: &TimestampOrderField{
		Value: func(_m *Timestamp) (ent.Value, error) {
			return _m.ID, nil
		},
		column: timestamp.FieldID,
		toTerm: timestamp.ByID,
		toCursor: func(_m *Timestamp) Cursor {
			return Cursor{ID: _m.ID}
		},
	},
}

// ToEdge converts Timestamp into TimestampEdge.
func (_m *Timestamp) ToEdge(order *TimestampOrder) *TimestampEdge {
	if order == nil {
		order = DefaultTimestampOrder
	}
	return &TimestampEdge{
		Node:   _m,
		Cursor: order.Field.toCursor(_m),
	}
}
