import type { DistributeOmit, FieldKind, FieldNullability, FieldOptionsFromKind, InferredFieldOptionKeys, InputFieldMap, InputShapeFromFields, InterfaceParam, InterfaceRef, InterfaceTypeOptions, MaybePromise, ObjectParam, ObjectRef, ObjectTypeOptions, OutputRef, OutputShape, OutputType, Resolver, SchemaTypes, ShapeFromTypeParam, TypeParam, } from '@pothos/core'; import type DataLoader from 'dataloader'; import type { GraphQLResolveInfo } from 'graphql'; export type DataloaderKey = bigint | number | string; export type LoadableFieldOptions< Types extends SchemaTypes, ParentShape, Type extends TypeParam, Nullable extends FieldNullability, Args extends InputFieldMap, ResolveReturnShape, Key, CacheKey, Kind extends FieldKind = FieldKind, ByPath extends boolean = boolean, > = Omit< FieldOptionsFromKind, InferredFieldOptionKeys > & { byPath?: ByPath; load: ( keys: Key[], context: Types['Context'], args: false extends ByPath ? never : InputShapeFromFields, info: false extends ByPath ? never : GraphQLResolveInfo, ) => Promise)[]>; loaderOptions?: DataLoader.Options, CacheKey>; sort?: (value: LoaderShapeFromType) => Key; resolve: Resolver< ParentShape, InputShapeFromFields, Types['Context'], (Type extends unknown[] ? [OutputRef] : OutputRef) extends infer KeyType ? KeyType extends OutputRef | [OutputRef] ? ShapeFromTypeParam< Types, KeyType, Nullable extends FieldNullability ? Nullable : never > : never : never, ResolveReturnShape >; }; export type LoadableListFieldOptions< Types extends SchemaTypes, ParentShape, Type extends OutputType, Nullable extends FieldNullability<[Type]>, Args extends InputFieldMap, ResolveReturnShape, Key, CacheKey, Kind extends FieldKind = FieldKind, ByPath extends boolean = boolean, > = Omit< FieldOptionsFromKind, InferredFieldOptionKeys | 'type' > & { type: Type; byPath?: ByPath; load: ( keys: Key[], context: Types['Context'], args: false extends ByPath ? never : InputShapeFromFields, info: false extends ByPath ? never : GraphQLResolveInfo, ) => Promise)[]>; loaderOptions?: DataLoader.Options, CacheKey>; sort?: (value: ShapeFromTypeParam) => Key; resolve: Resolver< ParentShape, InputShapeFromFields, Types['Context'], Key, ResolveReturnShape >; }; export type LoadableGroupFieldOptions< Types extends SchemaTypes, ParentShape, Type extends OutputType, Nullable extends FieldNullability<[Type]>, Args extends InputFieldMap, ResolveReturnShape, Key, CacheKey, Kind extends FieldKind = FieldKind, ByPath extends boolean = boolean, > = Omit< FieldOptionsFromKind, InferredFieldOptionKeys | 'type' > & { type: Type; byPath?: ByPath; load: ( keys: Key[], context: Types['Context'], args: false extends ByPath ? never : InputShapeFromFields, info: false extends ByPath ? never : GraphQLResolveInfo, ) => Promise[]>; loaderOptions?: DataLoader.Options[], CacheKey>; group: (value: ShapeFromTypeParam) => Key; resolve: Resolver< ParentShape, InputShapeFromFields, Types['Context'], Key, ResolveReturnShape >; }; export type DataLoaderOptions< Types extends SchemaTypes, LoadResult, Key extends bigint | number | string, CacheKey, Shape = ShapeFromLoadResult, > = { load: (keys: Key[], context: Types['Context']) => Promise; loaderOptions?: DataLoader.Options; } & ( | { toKey: (value: Shape) => Key; cacheResolved?: boolean; sort?: boolean; } | { toKey?: undefined; cacheResolved?: (value: Shape) => Key; sort?: (value: Shape) => Key; } ); export type DataloaderObjectTypeOptions< Types extends SchemaTypes, LoadResult, Key extends bigint | number | string, Interfaces extends InterfaceParam[], NameOrRef extends ObjectParam | string, CacheKey, Shape, > = DataLoaderOptions & ObjectTypeOptions< Types, NameOrRef extends ObjectParam ? NameOrRef : ObjectRef, Shape, Interfaces >; export type LoadableInterfaceOptions< Types extends SchemaTypes, LoadResult, Key extends bigint | number | string, Interfaces extends InterfaceParam[], NameOrRef extends InterfaceParam | string, CacheKey, Shape = ShapeFromLoadResult, > = DataLoaderOptions & InterfaceTypeOptions< Types, NameOrRef extends InterfaceParam ? NameOrRef : InterfaceRef, Shape, Interfaces >; export type LoadableUnionOptions< Types extends SchemaTypes, Key extends bigint | number | string, Member extends ObjectParam, CacheKey, Shape, > = DataLoaderOptions & PothosSchemaTypes.UnionTypeOptions; export type LoaderShapeFromType< Types extends SchemaTypes, Type extends TypeParam, Nullable extends FieldNullability, > = Type extends [TypeParam] ? ShapeFromTypeParam : ShapeFromTypeParam; export interface LoadableRef { getDataloader: (context: C) => DataLoader; } export interface LoadableNodeId { id: DistributeOmit< FieldOptionsFromKind< Types, Shape, 'ID', false, {}, 'Object', Shape, MaybePromise> >, 'args' | 'nullable' | 'type' > & { parse?: (id: string, ctx: Types['Context']) => IDShape; }; } export type LoadableNodeOptions< Types extends SchemaTypes, LoadResult, Interfaces extends InterfaceParam[], NameOrRef extends ObjectParam | string, IDShape extends bigint | number | string = string, Key extends bigint | number | string = IDShape, CacheKey = Key, Shape = ShapeFromLoadResult, > = DataloaderObjectTypeOptions & LoadableNodeId; export type ShapeFromLoadResult = Exclude;