import { Prisma, PrismaClient } from '@prisma/client'; import type { AppSyncIdentity, AppSyncIdentityCognito, AppSyncIdentityIAM, AppSyncIdentityLambda, AppSyncIdentityOIDC, AppSyncResolverEvent, AppSyncResolverHandler } from 'aws-lambda'; import type { Actions, ActionsAliases, Authorizations } from './consts'; export type logLevel = 'INFO' | 'WARN' | 'ERROR'; export type PrismaAppSyncOptionsType = { connectionString?: string; sanitize?: boolean; logLevel?: logLevel; defaultPagination?: number | false; maxDepth?: number; maxReqPerUserMinute?: number | false; }; export type Options = Required & { modelsMapping: any; fieldsMapping: any; }; export type InjectedConfig = { modelsMapping?: { [modelVariant: string]: { prismaRef: string; singular: string; plural: string; }; }; fieldsMapping?: { [fieldPath: string]: { type: string; isRelation: boolean; }; }; operations?: string; }; export type RuntimeConfig = { modelsMapping: { [modelVariant: string]: { prismaRef: string; singular: string; plural: string; }; }; fieldsMapping: { [fieldPath: string]: { type: string; isRelation: boolean; }; }; operations: string[]; }; export type Action = typeof Actions[keyof typeof Actions] | string; export type ActionsAlias = typeof ActionsAliases[keyof typeof ActionsAliases] | 'custom' | null; export type ActionsAliasStr = keyof typeof ActionsAliases; export type Context = { action: Action; alias: ActionsAlias; model: Model; }; export type Model = { prismaRef: string; singular: string; plural: string; } | null; export type { AppSyncResolverHandler, AppSyncResolverEvent, AppSyncIdentity }; /** * ### QueryParams * * @example * ``` * { * type: 'Query', * operation: 'getPost', * context: { action: 'get', alias: 'access', model: 'Post' }, * fields: ['title', 'status'], * paths: ['get/post/title', 'get/post/status'], * args: { where: { id: 5 } }, * prismaArgs: { * where: { id: 5 }, * select: { title: true, status: true }, * }, * authorization: 'API_KEY', * identity: { ... }, * } * ``` */ export type QueryParams = { type: GraphQLType; operation: string; context: Context; fields: string[]; paths: string[]; args: T; prismaArgs: PrismaArgs; authorization: Authorization; identity: Identity; headers: any; }; export type Authorization = typeof Authorizations[keyof typeof Authorizations] | null; export type PrismaGet = Pick, 'where'> & Pick; export type PrismaList = Pick; export type PrismaCount = Pick; export type PrismaCreate = Pick, 'data'> & Pick; export type PrismaCreateMany = Pick, 'data'> & Pick; export type PrismaUpdate = Pick, 'data' | 'where'> & Pick; export type PrismaUpdateMany = Pick, 'data' | 'where'>; export type PrismaUpsert = Pick, 'where'> & Pick & Pick & Pick; export type PrismaDelete = Pick, 'where'> & Pick; export type PrismaDeleteMany = Pick, 'where'>; export type QueryBuilder = { prismaGet: (...prismaArgs: PrismaArgs[]) => PrismaGet; prismaList: (...prismaArgs: PrismaArgs[]) => PrismaList; prismaCount: (...prismaArgs: PrismaArgs[]) => PrismaCount; prismaCreate: (...prismaArgs: PrismaArgs[]) => PrismaCreate; prismaCreateMany: (...prismaArgs: PrismaArgs[]) => PrismaCreateMany; prismaUpdate: (...prismaArgs: PrismaArgs[]) => PrismaUpdate; prismaUpdateMany: (...prismaArgs: PrismaArgs[]) => PrismaUpdateMany; prismaUpsert: (...prismaArgs: PrismaArgs[]) => PrismaUpsert; prismaDelete: (...prismaArgs: PrismaArgs[]) => PrismaDelete; prismaDeleteMany: (...prismaArgs: PrismaArgs[]) => PrismaDeleteMany; }; export type QueryParamsCustom = QueryParams & { prismaClient: PrismaClient; }; export type BeforeHookParams = QueryParams & { prismaClient: PrismaClient; }; /** * ### AfterHookParams * * @example * ``` * { * type: 'Query', * operation: 'getPost', * context: { action: 'get', alias: 'access', model: 'Post' }, * fields: ['title', 'status'], * paths: ['get/post/title', 'get/post/status'], * args: { where: { id: 5 } }, * prismaArgs: { * where: { id: 5 }, * select: { title: true, status: true }, * }, * authorization: 'API_KEY', * identity: { ... }, * result: { title: 'Hello World', status: 'PUBLISHED' } * } * ``` */ export type AfterHookParams = QueryParams & { prismaClient: PrismaClient; result: any | any[]; }; export type ShieldContext = { action: Action; model: string; }; export type Reason = string | ((context: ShieldContext) => string); export type ShieldRule = boolean | ((context: ShieldContext) => boolean | Promise) | any; export type Shield = { [matcher: string]: boolean | { rule: ShieldRule; reason?: Reason; }; }; export type HooksProps = { before: BeforeHookParams; after: AfterHookParams; }; export type HooksReturn = { before: Promise; after: Promise; }; export type HookPath = Operations | CustomResolvers; export type HooksParameter = `${HookType}:${HookPath}` | `${HookType}:**`; export type HooksParameters = { [matcher in HooksParameter]?: (props: HooksProps[HookType]) => HooksReturn[HookType]; }; export type Hooks = HooksParameters<'before', Operations, CustomResolvers> | HooksParameters<'after', Operations, CustomResolvers>; export type ShieldAuthorization = { canAccess: boolean; reason: Reason; prismaFilter: any; matcher: string; globPattern: string; }; export type ResolveParams = { event: AppSyncEvent; resolvers?: { [resolver in CustomResolvers]: ((props: QueryParamsCustom) => Promise) | boolean; }; shield?: (props: QueryParams) => Shield; hooks?: Hooks; }; export { PrismaClient, Prisma }; export type PrismaArgs = { where?: any; create?: any; update?: any; data?: any; select?: any; orderBy?: any; skip?: number | undefined; take?: number | undefined; skipDuplicates?: boolean | undefined; }; export type PrismaOperator = keyof Required; export type AppSyncEvent = AppSyncResolverEvent; export type GraphQLType = 'Query' | 'Mutation' | 'Subscription'; export type API_KEY = null | { [key: string]: any; }; export type AWS_LAMBDA = AppSyncIdentityLambda; export type AWS_IAM = AppSyncIdentityIAM; export type AMAZON_COGNITO_USER_POOLS = AppSyncIdentityCognito; export type OPENID_CONNECT = AppSyncIdentityOIDC; export type Identity = API_KEY | AWS_LAMBDA | AWS_IAM | AMAZON_COGNITO_USER_POOLS | OPENID_CONNECT;