// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"database/sql/driver"
	"fmt"

	"entgo.io/contrib/entgql"
	"entgo.io/ent/dialect/sql"
	"github.com/99designs/gqlgen/graphql"
	"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"
)

// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (_q *AttestationQuery) CollectFields(ctx context.Context, satisfies ...string) (*AttestationQuery, error) {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return _q, nil
	}
	if err := _q.collectField(ctx, false, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
		return nil, err
	}
	return _q, nil
}

func (_q *AttestationQuery) collectField(ctx context.Context, oneNode bool, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
	path = append([]string(nil), path...)
	var (
		unknownSeen    bool
		fieldSeen      = make(map[string]struct{}, len(attestation.Columns))
		selectedFields = []string{attestation.FieldID}
	)
	for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
		switch field.Name {

		case "attestationCollection":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&AttestationCollectionClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, attestationcollectionImplementors)...); err != nil {
				return err
			}
			_q.withAttestationCollection = query
		case "type":
			if _, ok := fieldSeen[attestation.FieldType]; !ok {
				selectedFields = append(selectedFields, attestation.FieldType)
				fieldSeen[attestation.FieldType] = struct{}{}
			}
		case "id":
		case "__typename":
		default:
			unknownSeen = true
		}
	}
	if !unknownSeen {
		_q.Select(selectedFields...)
	}
	return nil
}

type attestationPaginateArgs struct {
	first, last   *int
	after, before *Cursor
	opts          []AttestationPaginateOption
}

func newAttestationPaginateArgs(rv map[string]any) *attestationPaginateArgs {
	args := &attestationPaginateArgs{}
	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].(*AttestationWhereInput); ok {
		args.opts = append(args.opts, WithAttestationFilter(v.Filter))
	}
	return args
}

// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (_q *AttestationCollectionQuery) CollectFields(ctx context.Context, satisfies ...string) (*AttestationCollectionQuery, error) {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return _q, nil
	}
	if err := _q.collectField(ctx, false, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
		return nil, err
	}
	return _q, nil
}

func (_q *AttestationCollectionQuery) collectField(ctx context.Context, oneNode bool, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
	path = append([]string(nil), path...)
	var (
		unknownSeen    bool
		fieldSeen      = make(map[string]struct{}, len(attestationcollection.Columns))
		selectedFields = []string{attestationcollection.FieldID}
	)
	for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
		switch field.Name {

		case "attestations":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&AttestationClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, false, opCtx, field, path, mayAddCondition(satisfies, attestationImplementors)...); err != nil {
				return err
			}
			_q.WithNamedAttestations(alias, func(wq *AttestationQuery) {
				*wq = *query
			})

		case "statement":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&StatementClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, statementImplementors)...); err != nil {
				return err
			}
			_q.withStatement = query
		case "name":
			if _, ok := fieldSeen[attestationcollection.FieldName]; !ok {
				selectedFields = append(selectedFields, attestationcollection.FieldName)
				fieldSeen[attestationcollection.FieldName] = struct{}{}
			}
		case "id":
		case "__typename":
		default:
			unknownSeen = true
		}
	}
	if !unknownSeen {
		_q.Select(selectedFields...)
	}
	return nil
}

type attestationcollectionPaginateArgs struct {
	first, last   *int
	after, before *Cursor
	opts          []AttestationCollectionPaginateOption
}

func newAttestationCollectionPaginateArgs(rv map[string]any) *attestationcollectionPaginateArgs {
	args := &attestationcollectionPaginateArgs{}
	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].(*AttestationCollectionWhereInput); ok {
		args.opts = append(args.opts, WithAttestationCollectionFilter(v.Filter))
	}
	return args
}

// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (_q *AttestationPolicyQuery) CollectFields(ctx context.Context, satisfies ...string) (*AttestationPolicyQuery, error) {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return _q, nil
	}
	if err := _q.collectField(ctx, false, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
		return nil, err
	}
	return _q, nil
}

func (_q *AttestationPolicyQuery) collectField(ctx context.Context, oneNode bool, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
	path = append([]string(nil), path...)
	var (
		unknownSeen    bool
		fieldSeen      = make(map[string]struct{}, len(attestationpolicy.Columns))
		selectedFields = []string{attestationpolicy.FieldID}
	)
	for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
		switch field.Name {

		case "statement":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&StatementClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, statementImplementors)...); err != nil {
				return err
			}
			_q.withStatement = query
		case "name":
			if _, ok := fieldSeen[attestationpolicy.FieldName]; !ok {
				selectedFields = append(selectedFields, attestationpolicy.FieldName)
				fieldSeen[attestationpolicy.FieldName] = struct{}{}
			}
		case "id":
		case "__typename":
		default:
			unknownSeen = true
		}
	}
	if !unknownSeen {
		_q.Select(selectedFields...)
	}
	return nil
}

type attestationpolicyPaginateArgs struct {
	first, last   *int
	after, before *Cursor
	opts          []AttestationPolicyPaginateOption
}

func newAttestationPolicyPaginateArgs(rv map[string]any) *attestationpolicyPaginateArgs {
	args := &attestationpolicyPaginateArgs{}
	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].(*AttestationPolicyWhereInput); ok {
		args.opts = append(args.opts, WithAttestationPolicyFilter(v.Filter))
	}
	return args
}

// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (_q *DsseQuery) CollectFields(ctx context.Context, satisfies ...string) (*DsseQuery, error) {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return _q, nil
	}
	if err := _q.collectField(ctx, false, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
		return nil, err
	}
	return _q, nil
}

func (_q *DsseQuery) collectField(ctx context.Context, oneNode bool, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
	path = append([]string(nil), path...)
	var (
		unknownSeen    bool
		fieldSeen      = make(map[string]struct{}, len(dsse.Columns))
		selectedFields = []string{dsse.FieldID}
	)
	for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
		switch field.Name {

		case "statement":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&StatementClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, statementImplementors)...); err != nil {
				return err
			}
			_q.withStatement = query

		case "signatures":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&SignatureClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, false, opCtx, field, path, mayAddCondition(satisfies, signatureImplementors)...); err != nil {
				return err
			}
			_q.WithNamedSignatures(alias, func(wq *SignatureQuery) {
				*wq = *query
			})

		case "payloadDigests":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&PayloadDigestClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, false, opCtx, field, path, mayAddCondition(satisfies, payloaddigestImplementors)...); err != nil {
				return err
			}
			_q.WithNamedPayloadDigests(alias, func(wq *PayloadDigestQuery) {
				*wq = *query
			})
		case "createdAt":
			if _, ok := fieldSeen[dsse.FieldCreatedAt]; !ok {
				selectedFields = append(selectedFields, dsse.FieldCreatedAt)
				fieldSeen[dsse.FieldCreatedAt] = struct{}{}
			}
		case "gitoidSha256":
			if _, ok := fieldSeen[dsse.FieldGitoidSha256]; !ok {
				selectedFields = append(selectedFields, dsse.FieldGitoidSha256)
				fieldSeen[dsse.FieldGitoidSha256] = struct{}{}
			}
		case "payloadType":
			if _, ok := fieldSeen[dsse.FieldPayloadType]; !ok {
				selectedFields = append(selectedFields, dsse.FieldPayloadType)
				fieldSeen[dsse.FieldPayloadType] = struct{}{}
			}
		case "id":
		case "__typename":
		default:
			unknownSeen = true
		}
	}
	if !unknownSeen {
		_q.Select(selectedFields...)
	}
	return nil
}

type dssePaginateArgs struct {
	first, last   *int
	after, before *Cursor
	opts          []DssePaginateOption
}

func newDssePaginateArgs(rv map[string]any) *dssePaginateArgs {
	args := &dssePaginateArgs{}
	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[orderByField]; ok {
		switch v := v.(type) {
		case map[string]any:
			var (
				err1, err2 error
				order      = &DsseOrder{Field: &DsseOrderField{}, Direction: entgql.OrderDirectionAsc}
			)
			if d, ok := v[directionField]; ok {
				err1 = order.Direction.UnmarshalGQL(d)
			}
			if f, ok := v[fieldField]; ok {
				err2 = order.Field.UnmarshalGQL(f)
			}
			if err1 == nil && err2 == nil {
				args.opts = append(args.opts, WithDsseOrder(order))
			}
		case *DsseOrder:
			if v != nil {
				args.opts = append(args.opts, WithDsseOrder(v))
			}
		}
	}
	if v, ok := rv[whereField].(*DsseWhereInput); ok {
		args.opts = append(args.opts, WithDsseFilter(v.Filter))
	}
	return args
}

// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (_q *PayloadDigestQuery) CollectFields(ctx context.Context, satisfies ...string) (*PayloadDigestQuery, error) {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return _q, nil
	}
	if err := _q.collectField(ctx, false, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
		return nil, err
	}
	return _q, nil
}

func (_q *PayloadDigestQuery) collectField(ctx context.Context, oneNode bool, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
	path = append([]string(nil), path...)
	var (
		unknownSeen    bool
		fieldSeen      = make(map[string]struct{}, len(payloaddigest.Columns))
		selectedFields = []string{payloaddigest.FieldID}
	)
	for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
		switch field.Name {

		case "dsse":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&DsseClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, dsseImplementors)...); err != nil {
				return err
			}
			_q.withDsse = query
		case "algorithm":
			if _, ok := fieldSeen[payloaddigest.FieldAlgorithm]; !ok {
				selectedFields = append(selectedFields, payloaddigest.FieldAlgorithm)
				fieldSeen[payloaddigest.FieldAlgorithm] = struct{}{}
			}
		case "value":
			if _, ok := fieldSeen[payloaddigest.FieldValue]; !ok {
				selectedFields = append(selectedFields, payloaddigest.FieldValue)
				fieldSeen[payloaddigest.FieldValue] = struct{}{}
			}
		case "id":
		case "__typename":
		default:
			unknownSeen = true
		}
	}
	if !unknownSeen {
		_q.Select(selectedFields...)
	}
	return nil
}

type payloaddigestPaginateArgs struct {
	first, last   *int
	after, before *Cursor
	opts          []PayloadDigestPaginateOption
}

func newPayloadDigestPaginateArgs(rv map[string]any) *payloaddigestPaginateArgs {
	args := &payloaddigestPaginateArgs{}
	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].(*PayloadDigestWhereInput); ok {
		args.opts = append(args.opts, WithPayloadDigestFilter(v.Filter))
	}
	return args
}

// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (_q *SignatureQuery) CollectFields(ctx context.Context, satisfies ...string) (*SignatureQuery, error) {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return _q, nil
	}
	if err := _q.collectField(ctx, false, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
		return nil, err
	}
	return _q, nil
}

func (_q *SignatureQuery) collectField(ctx context.Context, oneNode bool, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
	path = append([]string(nil), path...)
	var (
		unknownSeen    bool
		fieldSeen      = make(map[string]struct{}, len(signature.Columns))
		selectedFields = []string{signature.FieldID}
	)
	for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
		switch field.Name {

		case "dsse":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&DsseClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, dsseImplementors)...); err != nil {
				return err
			}
			_q.withDsse = query

		case "timestamps":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&TimestampClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, false, opCtx, field, path, mayAddCondition(satisfies, timestampImplementors)...); err != nil {
				return err
			}
			_q.WithNamedTimestamps(alias, func(wq *TimestampQuery) {
				*wq = *query
			})
		case "keyID":
			if _, ok := fieldSeen[signature.FieldKeyID]; !ok {
				selectedFields = append(selectedFields, signature.FieldKeyID)
				fieldSeen[signature.FieldKeyID] = struct{}{}
			}
		case "signature":
			if _, ok := fieldSeen[signature.FieldSignature]; !ok {
				selectedFields = append(selectedFields, signature.FieldSignature)
				fieldSeen[signature.FieldSignature] = struct{}{}
			}
		case "id":
		case "__typename":
		default:
			unknownSeen = true
		}
	}
	if !unknownSeen {
		_q.Select(selectedFields...)
	}
	return nil
}

type signaturePaginateArgs struct {
	first, last   *int
	after, before *Cursor
	opts          []SignaturePaginateOption
}

func newSignaturePaginateArgs(rv map[string]any) *signaturePaginateArgs {
	args := &signaturePaginateArgs{}
	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].(*SignatureWhereInput); ok {
		args.opts = append(args.opts, WithSignatureFilter(v.Filter))
	}
	return args
}

// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (_q *StatementQuery) CollectFields(ctx context.Context, satisfies ...string) (*StatementQuery, error) {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return _q, nil
	}
	if err := _q.collectField(ctx, false, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
		return nil, err
	}
	return _q, nil
}

func (_q *StatementQuery) collectField(ctx context.Context, oneNode bool, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
	path = append([]string(nil), path...)
	var (
		unknownSeen    bool
		fieldSeen      = make(map[string]struct{}, len(statement.Columns))
		selectedFields = []string{statement.FieldID}
	)
	for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
		switch field.Name {

		case "subjects":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&SubjectClient{config: _q.config}).Query()
			)
			args := newSubjectPaginateArgs(fieldArgs(ctx, new(SubjectWhereInput), path...))
			if err := validateFirstLast(args.first, args.last); err != nil {
				return fmt.Errorf("validate first and last in path %q: %w", path, err)
			}
			pager, err := newSubjectPager(args.opts, args.last != nil)
			if err != nil {
				return fmt.Errorf("create new pager in path %q: %w", path, err)
			}
			if query, err = pager.applyFilter(query); err != nil {
				return err
			}
			ignoredEdges := !hasCollectedField(ctx, append(path, edgesField)...)
			if hasCollectedField(ctx, append(path, totalCountField)...) || hasCollectedField(ctx, append(path, pageInfoField)...) {
				hasPagination := args.after != nil || args.first != nil || args.before != nil || args.last != nil
				if hasPagination || ignoredEdges {
					query := query.Clone()
					_q.loadTotal = append(_q.loadTotal, func(ctx context.Context, nodes []*Statement) error {
						ids := make([]driver.Value, len(nodes))
						for i := range nodes {
							ids[i] = nodes[i].ID
						}
						var v []struct {
							NodeID uuid.UUID `sql:"statement_subjects"`
							Count  int       `sql:"count"`
						}
						query.Where(func(s *sql.Selector) {
							s.Where(sql.InValues(s.C(statement.SubjectsColumn), ids...))
						})
						if err := query.GroupBy(statement.SubjectsColumn).Aggregate(Count()).Scan(ctx, &v); err != nil {
							return err
						}
						m := make(map[uuid.UUID]int, len(v))
						for i := range v {
							m[v[i].NodeID] = v[i].Count
						}
						for i := range nodes {
							n := m[nodes[i].ID]
							if nodes[i].Edges.totalCount[0] == nil {
								nodes[i].Edges.totalCount[0] = make(map[string]int)
							}
							nodes[i].Edges.totalCount[0][alias] = n
						}
						return nil
					})
				} else {
					_q.loadTotal = append(_q.loadTotal, func(_ context.Context, nodes []*Statement) error {
						for i := range nodes {
							n := len(nodes[i].Edges.Subjects)
							if nodes[i].Edges.totalCount[0] == nil {
								nodes[i].Edges.totalCount[0] = make(map[string]int)
							}
							nodes[i].Edges.totalCount[0][alias] = n
						}
						return nil
					})
				}
			}
			if ignoredEdges || (args.first != nil && *args.first == 0) || (args.last != nil && *args.last == 0) {
				continue
			}
			if query, err = pager.applyCursors(query, args.after, args.before); err != nil {
				return err
			}
			path = append(path, edgesField, nodeField)
			if field := collectedField(ctx, path...); field != nil {
				if err := query.collectField(ctx, false, opCtx, *field, path, mayAddCondition(satisfies, subjectImplementors)...); err != nil {
					return err
				}
			}
			if limit := paginateLimit(args.first, args.last); limit > 0 {
				if oneNode {
					pager.applyOrder(query.Limit(limit))
				} else {
					modify := entgql.LimitPerRow(statement.SubjectsColumn, limit, pager.orderExpr(query))
					query.modifiers = append(query.modifiers, modify)
				}
			} else {
				query = pager.applyOrder(query)
			}
			_q.WithNamedSubjects(alias, func(wq *SubjectQuery) {
				*wq = *query
			})

		case "policy":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&AttestationPolicyClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, attestationpolicyImplementors)...); err != nil {
				return err
			}
			_q.withPolicy = query

		case "attestationCollections":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&AttestationCollectionClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, attestationcollectionImplementors)...); err != nil {
				return err
			}
			_q.withAttestationCollections = query

		case "dsse":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&DsseClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, false, opCtx, field, path, mayAddCondition(satisfies, dsseImplementors)...); err != nil {
				return err
			}
			_q.WithNamedDsse(alias, func(wq *DsseQuery) {
				*wq = *query
			})
		case "predicate":
			if _, ok := fieldSeen[statement.FieldPredicate]; !ok {
				selectedFields = append(selectedFields, statement.FieldPredicate)
				fieldSeen[statement.FieldPredicate] = struct{}{}
			}
		case "id":
		case "__typename":
		default:
			unknownSeen = true
		}
	}
	if !unknownSeen {
		_q.Select(selectedFields...)
	}
	return nil
}

type statementPaginateArgs struct {
	first, last   *int
	after, before *Cursor
	opts          []StatementPaginateOption
}

func newStatementPaginateArgs(rv map[string]any) *statementPaginateArgs {
	args := &statementPaginateArgs{}
	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].(*StatementWhereInput); ok {
		args.opts = append(args.opts, WithStatementFilter(v.Filter))
	}
	return args
}

// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (_q *SubjectQuery) CollectFields(ctx context.Context, satisfies ...string) (*SubjectQuery, error) {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return _q, nil
	}
	if err := _q.collectField(ctx, false, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
		return nil, err
	}
	return _q, nil
}

func (_q *SubjectQuery) collectField(ctx context.Context, oneNode bool, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
	path = append([]string(nil), path...)
	var (
		unknownSeen    bool
		fieldSeen      = make(map[string]struct{}, len(subject.Columns))
		selectedFields = []string{subject.FieldID}
	)
	for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
		switch field.Name {

		case "subjectDigests":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&SubjectDigestClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, false, opCtx, field, path, mayAddCondition(satisfies, subjectdigestImplementors)...); err != nil {
				return err
			}
			_q.WithNamedSubjectDigests(alias, func(wq *SubjectDigestQuery) {
				*wq = *query
			})

		case "statement":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&StatementClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, statementImplementors)...); err != nil {
				return err
			}
			_q.withStatement = query
		case "createdAt":
			if _, ok := fieldSeen[subject.FieldCreatedAt]; !ok {
				selectedFields = append(selectedFields, subject.FieldCreatedAt)
				fieldSeen[subject.FieldCreatedAt] = struct{}{}
			}
		case "name":
			if _, ok := fieldSeen[subject.FieldName]; !ok {
				selectedFields = append(selectedFields, subject.FieldName)
				fieldSeen[subject.FieldName] = struct{}{}
			}
		case "id":
		case "__typename":
		default:
			unknownSeen = true
		}
	}
	if !unknownSeen {
		_q.Select(selectedFields...)
	}
	return nil
}

type subjectPaginateArgs struct {
	first, last   *int
	after, before *Cursor
	opts          []SubjectPaginateOption
}

func newSubjectPaginateArgs(rv map[string]any) *subjectPaginateArgs {
	args := &subjectPaginateArgs{}
	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[orderByField]; ok {
		switch v := v.(type) {
		case map[string]any:
			var (
				err1, err2 error
				order      = &SubjectOrder{Field: &SubjectOrderField{}, Direction: entgql.OrderDirectionAsc}
			)
			if d, ok := v[directionField]; ok {
				err1 = order.Direction.UnmarshalGQL(d)
			}
			if f, ok := v[fieldField]; ok {
				err2 = order.Field.UnmarshalGQL(f)
			}
			if err1 == nil && err2 == nil {
				args.opts = append(args.opts, WithSubjectOrder(order))
			}
		case *SubjectOrder:
			if v != nil {
				args.opts = append(args.opts, WithSubjectOrder(v))
			}
		}
	}
	if v, ok := rv[whereField].(*SubjectWhereInput); ok {
		args.opts = append(args.opts, WithSubjectFilter(v.Filter))
	}
	return args
}

// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (_q *SubjectDigestQuery) CollectFields(ctx context.Context, satisfies ...string) (*SubjectDigestQuery, error) {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return _q, nil
	}
	if err := _q.collectField(ctx, false, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
		return nil, err
	}
	return _q, nil
}

func (_q *SubjectDigestQuery) collectField(ctx context.Context, oneNode bool, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
	path = append([]string(nil), path...)
	var (
		unknownSeen    bool
		fieldSeen      = make(map[string]struct{}, len(subjectdigest.Columns))
		selectedFields = []string{subjectdigest.FieldID}
	)
	for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
		switch field.Name {

		case "subject":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&SubjectClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, subjectImplementors)...); err != nil {
				return err
			}
			_q.withSubject = query
		case "algorithm":
			if _, ok := fieldSeen[subjectdigest.FieldAlgorithm]; !ok {
				selectedFields = append(selectedFields, subjectdigest.FieldAlgorithm)
				fieldSeen[subjectdigest.FieldAlgorithm] = struct{}{}
			}
		case "value":
			if _, ok := fieldSeen[subjectdigest.FieldValue]; !ok {
				selectedFields = append(selectedFields, subjectdigest.FieldValue)
				fieldSeen[subjectdigest.FieldValue] = struct{}{}
			}
		case "id":
		case "__typename":
		default:
			unknownSeen = true
		}
	}
	if !unknownSeen {
		_q.Select(selectedFields...)
	}
	return nil
}

type subjectdigestPaginateArgs struct {
	first, last   *int
	after, before *Cursor
	opts          []SubjectDigestPaginateOption
}

func newSubjectDigestPaginateArgs(rv map[string]any) *subjectdigestPaginateArgs {
	args := &subjectdigestPaginateArgs{}
	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].(*SubjectDigestWhereInput); ok {
		args.opts = append(args.opts, WithSubjectDigestFilter(v.Filter))
	}
	return args
}

// CollectFields tells the query-builder to eagerly load connected nodes by resolver context.
func (_q *TimestampQuery) CollectFields(ctx context.Context, satisfies ...string) (*TimestampQuery, error) {
	fc := graphql.GetFieldContext(ctx)
	if fc == nil {
		return _q, nil
	}
	if err := _q.collectField(ctx, false, graphql.GetOperationContext(ctx), fc.Field, nil, satisfies...); err != nil {
		return nil, err
	}
	return _q, nil
}

func (_q *TimestampQuery) collectField(ctx context.Context, oneNode bool, opCtx *graphql.OperationContext, collected graphql.CollectedField, path []string, satisfies ...string) error {
	path = append([]string(nil), path...)
	var (
		unknownSeen    bool
		fieldSeen      = make(map[string]struct{}, len(timestamp.Columns))
		selectedFields = []string{timestamp.FieldID}
	)
	for _, field := range graphql.CollectFields(opCtx, collected.Selections, satisfies) {
		switch field.Name {

		case "signature":
			var (
				alias = field.Alias
				path  = append(path, alias)
				query = (&SignatureClient{config: _q.config}).Query()
			)
			if err := query.collectField(ctx, oneNode, opCtx, field, path, mayAddCondition(satisfies, signatureImplementors)...); err != nil {
				return err
			}
			_q.withSignature = query
		case "type":
			if _, ok := fieldSeen[timestamp.FieldType]; !ok {
				selectedFields = append(selectedFields, timestamp.FieldType)
				fieldSeen[timestamp.FieldType] = struct{}{}
			}
		case "timestamp":
			if _, ok := fieldSeen[timestamp.FieldTimestamp]; !ok {
				selectedFields = append(selectedFields, timestamp.FieldTimestamp)
				fieldSeen[timestamp.FieldTimestamp] = struct{}{}
			}
		case "id":
		case "__typename":
		default:
			unknownSeen = true
		}
	}
	if !unknownSeen {
		_q.Select(selectedFields...)
	}
	return nil
}

type timestampPaginateArgs struct {
	first, last   *int
	after, before *Cursor
	opts          []TimestampPaginateOption
}

func newTimestampPaginateArgs(rv map[string]any) *timestampPaginateArgs {
	args := &timestampPaginateArgs{}
	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].(*TimestampWhereInput); ok {
		args.opts = append(args.opts, WithTimestampFilter(v.Filter))
	}
	return args
}

const (
	afterField     = "after"
	firstField     = "first"
	beforeField    = "before"
	lastField      = "last"
	orderByField   = "orderBy"
	directionField = "direction"
	fieldField     = "field"
	whereField     = "where"
)

func fieldArgs(ctx context.Context, whereInput any, path ...string) map[string]any {
	field := collectedField(ctx, path...)
	if field == nil || field.Arguments == nil {
		return nil
	}
	oc := graphql.GetOperationContext(ctx)
	args := field.ArgumentMap(oc.Variables)
	return unmarshalArgs(ctx, whereInput, args)
}

// unmarshalArgs allows extracting the field arguments from their raw representation.
func unmarshalArgs(ctx context.Context, whereInput any, args map[string]any) map[string]any {
	for _, k := range []string{firstField, lastField} {
		v, ok := args[k]
		if !ok || v == nil {
			continue
		}
		i, err := graphql.UnmarshalInt(v)
		if err == nil {
			args[k] = &i
		}
	}
	for _, k := range []string{beforeField, afterField} {
		v, ok := args[k]
		if !ok {
			continue
		}
		c := &Cursor{}
		if c.UnmarshalGQL(v) == nil {
			args[k] = c
		}
	}
	if v, ok := args[whereField]; ok && whereInput != nil {
		if err := graphql.UnmarshalInputFromContext(ctx, v, whereInput); err == nil {
			args[whereField] = whereInput
		}
	}

	return args
}

// mayAddCondition appends another type condition to the satisfies list
// if it does not exist in the list.
func mayAddCondition(satisfies []string, typeCond []string) []string {
Cond:
	for _, c := range typeCond {
		for _, s := range satisfies {
			if c == s {
				continue Cond
			}
		}
		satisfies = append(satisfies, c)
	}
	return satisfies
}
