import { GraphQLResolveInfo } from 'graphql' import { core } from 'nexus' import { CommonFieldConfig } from 'nexus/dist/core' import * as Helpers from './helpers' // todo remove framework types from here once // https://github.com/graphql-nexus/nexus/issues/1069 /** * Framework-only * Note: PrismaClient type is now generic to support both prisma-client-js and prisma-client generators */ declare global { interface NexusTestContextApp { db: { client: any // PrismaClient - type is dynamic based on generator used } } } /** * Framework & Library */ export type ModelNameInGraphQLTypes = ReturnType extends core.GetGen<'objectNames'> ? true : false export type CustomFieldResolver = ( root: core.SourceValue, args: core.ArgsValue, context: core.GetGen<'context'>, info: GraphQLResolveInfo, originalResolve: core.FieldResolver ) => | core.MaybePromise> | core.MaybePromiseDeep> type NexusPrismaScalarOpts< TypeName extends string, MethodName extends string, Alias extends string | undefined > = { /** * Alias the field name */ alias?: Alias /** * Provide a custom resolver on any resolver generated by the plugin via t.crud or t.model. * You can either entirely replace the generated resolver, or wrap it via the use of the `originalResolve` parameter. * * @example * * ```ts * t.crud.posts({ * async resolve(root, args, ctx, info, originalResolve) { * console.log('logic before the resolver') * const res = await originalResolve(root, args, ctx, info) * console.log('logic after the resolver') * return res * } * }) * ``` */ resolve?: CustomFieldResolver } & NexusGenPluginFieldConfig & CommonFieldConfig type RootObjectTypes = Pick, core.GetGen<'objectNames'>> /** * Determine if `B` is a subset (or equivalent to) of `A`. */ type IsSubset = keyof A extends never ? false : B extends A ? true : false type OmitByValue = Pick< T, { [Key in keyof T]: T[Key] extends ValueType ? never : Key }[keyof T] > type GetSubsetTypes = keyof OmitByValue< { [P in keyof RootObjectTypes]: ModelName extends keyof Helpers.GetGen<'models'> // if ? IsSubset> extends true // else if ? RootObjectTypes[P] : never // else : never }, never > type SubsetTypes = GetSubsetTypes extends never ? "ERROR: No subset types are available. Please make sure that one of your GraphQL type is a subset of your t.model('')" : GetSubsetTypes type DynamicRequiredType = ModelNameInGraphQLTypes extends true ? { type?: SubsetTypes } : { type: SubsetTypes } type GetNexusPrismaInput< ModelName extends string, MethodName extends string, InputName extends 'filtering' | 'ordering' > = ModelName extends keyof Helpers.GetGen<'inputs'> ? MethodName extends keyof Helpers.GetGen2<'inputs', ModelName> ? InputName extends keyof Helpers.GetGen3<'inputs', ModelName, MethodName> ? Helpers.GetGen3<'inputs', ModelName, MethodName>[InputName] & string : never : never : never /** * Represents arguments required by Prisma Client JS that will * be derived from a request's input (args, context, and info) * and omitted from the GraphQL API. The object itself maps the * names of these args to a function that takes an object representing * the request's input and returns the value to pass to the prisma * arg of the same name. */ export type LocalComputedInputs = Record< string, (params: LocalMutationResolverParams) => unknown > export type GlobalComputedInputs = Record unknown> type BaseMutationResolverParams = { info: GraphQLResolveInfo ctx: Context } export type GlobalMutationResolverParams = BaseMutationResolverParams & { args: Record & { data: unknown } } export type LocalMutationResolverParams = BaseMutationResolverParams & { args: MethodName extends keyof core.GetGen2<'argTypes', 'Mutation'> ? core.GetGen3<'argTypes', 'Mutation', MethodName> : any } export type Context = core.GetGen<'context'> export type BaseRelationOptions< TypeName extends string, MethodName extends string, Alias extends string | undefined, ReturnType extends string > = DynamicRequiredType & { /** * Alias the default field name */ alias?: Alias /** * Enable the usage of a custom resolver on any resolver generated by the plugin via t.crud or t.model. * You can either entirely replace the generated resolver, or wrap it via the use of the `originalResolve` parameter. * * @example * * ```ts * t.crud.posts({ * async resolve(root, args, ctx, info, originalResolve) { * console.log('logic before the resolver') * const res = await originalResolve(root, args, ctx, info) * console.log('logic after the resolver') * return res * } * }) * ``` */ resolve?: CustomFieldResolver computedInputs?: LocalComputedInputs } & NexusGenPluginFieldConfig & CommonFieldConfig // If GetNexusPrismaInput returns never, it means there are no filtering/ordering args for it. type NexusPrismaRelationOpts< ModelName extends string, MethodName extends string, Alias extends string | undefined, ReturnType extends string > = GetNexusPrismaInput extends never ? BaseRelationOptions // else if : GetNexusPrismaInput extends never ? BaseRelationOptions // else : BaseRelationOptions & { filtering?: boolean | Partial, boolean>> ordering?: boolean | Partial, boolean>> pagination?: boolean | Helpers.GetGen<'pagination'> } type IsScalar = TypeName extends core.GetGen<'scalarNames'> ? true : false type IsObject = Name extends core.GetGen<'objectNames'> ? true : false type IsEnum = Name extends core.GetGen<'enumNames'> ? true : false type IsInputObject = Name extends core.GetGen<'inputNames'> ? true : false /** * The kind that a GraphQL type may be. */ type Kind = 'Enum' | 'Object' | 'Scalar' | 'InputObject' /** * Helper to safely reference a Kind type. For example instead of the following * which would admit a typo: * * ```ts * type Foo = Bar extends 'scalar' ? ... * ``` * * You can do this which guarantees a correct reference: * * ```ts * type Foo = Bar extends AKind<'Scalar'> ? ... * ``` * */ type AKind = T type GetKind = IsEnum extends true ? 'Enum' // else if : IsScalar extends true ? 'Scalar' // else if : IsObject extends true ? 'Object' // else if : IsInputObject extends true ? 'InputObject' // else // FIXME should be \`never\`, but GQL objects named differently // than backing type fall into this branch : 'Object' export type NexusPrismaFields & string> = { [MethodName in keyof Helpers.GetGen2<'outputs', ModelName> & string]: NexusPrismaMethod< ModelName, MethodName, GetKind & string> // Is the return type a scalar? > } type NexusPrismaMethod< ModelName extends keyof Helpers.GetGen<'outputs'> & string, MethodName extends keyof Helpers.GetGen2<'outputs', ModelName> & string, ThisKind extends Kind, ReturnType extends string = Helpers.GetGen3<'outputs', ModelName, MethodName> & string > = ThisKind extends AKind<'Enum'> ? () => NexusPrismaFields // else if // if scalar return scalar opts : ThisKind extends AKind<'Scalar'> ? ( opts?: NexusPrismaScalarOpts ) => NexusPrismaFields // else if // if model name has a mapped graphql types then make opts optional : ModelNameInGraphQLTypes extends true ? ( opts?: NexusPrismaRelationOpts ) => NexusPrismaFields // else // force use input the related graphql type -> { type: '...' } : ( opts: NexusPrismaRelationOpts ) => NexusPrismaFields type GetNexusPrismaMethod = TypeName extends keyof Helpers.GetGen<'methods'> ? Helpers.GetGen2<'methods', TypeName> : >( typeName: CustomTypeName ) => Helpers.GetGen2<'methods', CustomTypeName> export type GetNexusPrisma< TypeName extends string, ModelOrCrud extends 'model' | 'crud' > = ModelOrCrud extends 'model' ? TypeName extends 'Mutation' ? never : TypeName extends 'Query' ? never : GetNexusPrismaMethod : ModelOrCrud extends 'crud' ? TypeName extends 'Mutation' ? GetNexusPrismaMethod : TypeName extends 'Query' ? GetNexusPrismaMethod : never : never