graphql-gene
Version:
Generates automatically an executable schema out of your ORM models
1 lines • 14.6 kB
Source Map (JSON)
{"version":3,"file":"defineConfig.cjs","sources":["../src/defineConfig.ts"],"sourcesContent":["import type { GraphQLFieldResolver } from 'graphql'\nimport type { GeneContext } from 'graphql-gene/context'\nimport type {\n FindOptionsHandler,\n FindOptionsHandlerByType,\n FindOptionsStateByModel,\n GraphQLFieldName,\n GraphqlReturnTypes,\n GraphqlToTypescript,\n GraphqlTypeName,\n GraphQLVarType,\n InferFields,\n Narrow,\n OperatorInputs,\n Prop,\n PrototypeOrNot,\n SomeRequired,\n ValidGraphqlType,\n} from './types'\nimport type { GENE_RESOLVER_TEMPLATES, QUERY_ORDER_ENUM } from './constants'\n\ntype ArgsDefinition<V = string> = Record<string, V> | `${GENE_RESOLVER_TEMPLATES}` | undefined\nexport type StrictArgsDefinition = ArgsDefinition<GraphqlReturnTypes<ValidGraphqlType>>\n\nexport type GeneConfigTypes<\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgDefs extends ArgsDefinition = undefined,\n TReturnType extends string | unknown = unknown,\n> =\n | readonly string[]\n | (GeneObjectTypeConfig<TSource, TContext, TArgDefs, TReturnType> & { geneConfig?: GeneConfig })\n\nexport type GeneObjectTypeConfig<\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgDefs extends ArgsDefinition = undefined,\n TReturnType extends string | unknown = unknown,\n> = Record<\n GraphQLFieldName,\n | GraphqlReturnTypes<ValidGraphqlType | ''>\n | GeneTypeConfig<TSource, TContext, TArgDefs, TReturnType>\n>\n\ntype FallbackIfInvalid<T, TFallback> = T extends never ? TFallback : T\ntype AccurateTypeSource<\n TTypeName,\n TFallbackSource = Record<string, unknown> | undefined,\n> = TTypeName extends 'Query' | 'Mutation'\n ? undefined\n : TTypeName extends string\n ? FallbackIfInvalid<NonNullable<GraphqlToTypescript<TTypeName>>, TFallbackSource>\n : TFallbackSource\n\nexport type ExtendedTypes<\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgDefs extends ArgsDefinition = undefined,\n TReturnType extends string | unknown = unknown,\n> = {\n [typeName in GraphqlTypeName]?: Record<\n GraphQLFieldName,\n FieldConfig<\n AccurateTypeSource<typeName, TSource>,\n TContext,\n undefined extends TArgDefs ? ArgsDefinition : TArgDefs,\n TReturnType extends unknown ? GraphqlReturnTypes<ValidGraphqlType> : TReturnType\n >\n >\n}\n\nexport type NarrowExtendedTypes<TTypes extends Record<string, Record<string, object>>> = {\n [TypeName in keyof TTypes]?: {\n [FieldName in keyof TTypes[TypeName]]: ExtendedTypeField<TTypes[TypeName][FieldName], TypeName>\n }\n}\n\ntype ValidReturnTypeOption<T> =\n Narrow<T> extends GraphqlReturnTypes<ValidGraphqlType> ? Narrow<T> : never\n\nexport type ExtendedTypeField<T, TypeName> = {\n [K in keyof T]: K extends 'resolver'\n ? GeneResolverOption<\n AccurateTypeSource<TypeName>,\n GeneContext,\n Prop<T, 'args'> extends ArgsDefinition ? Prop<T, 'args'> : never,\n ValidReturnTypeOption<Prop<T, 'returnType'>>\n >\n : K extends 'returnType'\n ? ValidReturnTypeOption<T[K]>\n : K extends 'args'\n ? T[K] extends StrictArgsDefinition\n ? Narrow<T[K]>\n : never\n : K extends 'findOptions'\n ? FindOptionsHandlerByType<TypeName, T[K]>\n : Narrow<T[K]>\n}\n\nexport type StrictExtendedTypes<\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgDefs extends ArgsDefinition = undefined,\n> = ExtendedTypes<TSource, TContext, TArgDefs, GraphqlReturnTypes<ValidGraphqlType>>\n\nexport interface GeneConfig<\n M = unknown,\n TSource = M | Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgDefs extends ArgsDefinition = undefined,\n TReturnType extends string | unknown = unknown,\n TVarType extends GraphQLVarType = GraphQLVarType,\n> {\n /** Array of fields to include in the GraphQL type (default: include all). */\n include?: (InferFields<M> | RegExp)[]\n /** Array of fields to exclude in the GraphQL type (default: ['createdAt', updatedAt']). */\n exclude?: (InferFields<M> | RegExp)[]\n\n /** To include the timestamp attributes or not (default: false). */\n includeTimestamps?: boolean | ('createdAt' | 'updatedAt')[]\n\n /** The GraphQL variable type to use (default: \"type\"). */\n varType?: TVarType\n\n /** Directives to apply at the type level (also possible at the field level). */\n directives?: GeneDirectiveConfig<\n Record<string, string | number | boolean | string[] | number[] | boolean[] | null> | undefined,\n TSource extends M ? PrototypeOrNot<M> : TSource\n >[]\n\n /**\n * The values of \"aliases\" would be nested GeneConfig properties that overwrites the ones se\n * at a higher level. This is useful for instances with a specific scope include more fields\n * that the parent model (i.e. `AuthenticatedUser` being an alias of `User`). Note that the\n * alias needs to be exported from _graphqlTypes.ts_ as well.\n *\n * @example\n * include: ['id', 'username'],\n *\n * aliases: {\n * AuthenticatedUser: {\n * include: ['id', 'email', 'username', 'role', 'address', 'orders'],\n * },\n * },\n *\n * @example\n * export { User as AuthenticatedUser } from '../models/User/User.model'\n */\n aliases?: {\n [modelKey in GraphqlTypeName]?: GeneConfig<\n M,\n TSource,\n TContext,\n TArgDefs,\n TReturnType,\n TVarType\n >\n }\n\n /**\n * Extend the Query or Mutation types only.\n * @deprecated You should import and call `extendTypes` instead.\n */\n types?: ExtendedTypes<TSource, TContext, TArgDefs, TReturnType>\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n findOptions?: FindOptionsHandler<FindOptionsStateByModel<M, any>>\n}\n\nexport type GeneTypeConfig<\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgDefs extends ArgsDefinition = undefined,\n TReturnType extends string | unknown = unknown,\n> = {\n directives?: GeneDirectiveConfig<\n Record<string, string | number | boolean | string[] | number[] | boolean[] | null> | undefined,\n TSource\n >[]\n args?: TArgDefs extends undefined ? undefined : TArgDefs\n resolver?: GeneResolverOption<TSource, TContext, TArgDefs, TReturnType>\n returnType: TReturnType extends unknown ? GraphqlReturnTypes<ValidGraphqlType> : TReturnType\n findOptions?: FindOptionsHandler<object>\n}\n\nexport type FieldConfig<\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgDefs extends ArgsDefinition = undefined,\n TReturnType extends string | unknown = unknown,\n> =\n | GraphqlReturnTypes<ValidGraphqlType>\n | SomeRequired<GeneTypeConfig<TSource, TContext, TArgDefs, TReturnType>, 'resolver'>\n\ntype GeneResolverOption<\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgDefs extends ArgsDefinition = undefined,\n TReturnType extends string | unknown = unknown,\n> =\n | GeneResolver<\n TSource,\n TContext,\n FieldConfigArgsOption<TArgDefs, TReturnType>,\n TReturnType extends string ? GraphqlToTypescript<TReturnType> : unknown\n >\n | `${GENE_RESOLVER_TEMPLATES}`\n\nexport type GeneResolver<\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgs = Record<string, unknown> | undefined,\n TResult = unknown,\n> = (options: {\n source: Parameters<GraphQLFieldResolver<TSource, TContext, TArgs>>[0]\n args: Parameters<GraphQLFieldResolver<TSource, TContext, TArgs>>[1]\n context: Parameters<GraphQLFieldResolver<TSource, TContext, TArgs>>[2]\n info: Parameters<GraphQLFieldResolver<TSource, TContext, TArgs>>[3]\n}) => TResult | Promise<TResult>\n\nexport type FieldConfigArgsOption<\n TArgDefs extends ArgsDefinition,\n TReturnType extends string | unknown,\n> = TArgDefs extends `${GENE_RESOLVER_TEMPLATES.default}`\n ? GeneDefaultResolverArgs<\n TReturnType extends string ? NonNullable<GraphqlToTypescript<TReturnType>> : unknown\n >\n : TArgDefs extends undefined\n ? Record<string, unknown> | undefined\n : {\n [k in keyof Narrow<TArgDefs>]: Narrow<TArgDefs>[k] extends string\n ? GraphqlToTypescript<Narrow<TArgDefs>[k]>\n : unknown\n }\n\nexport type ArgsTypeToGraphQL<ArgsType> = {\n [k in keyof Required<ArgsType>]: Required<ArgsType>[k] extends number\n ? 'Int'\n : k extends 'where' | 'order'\n ? string\n : Required<ArgsType>[k] extends string\n ? 'String'\n : Required<ArgsType>[k] extends boolean\n ? 'Boolean'\n : never\n}\n\nexport type GeneDefaultResolverArgs<M> = {\n page: number\n perPage: number\n locale?: string\n id?: string\n where: {\n [k in keyof M]: Exclude<M[k], null | undefined> extends string | number | bigint | boolean\n ? OperatorInputs<Exclude<M[k], null | undefined>>\n : never\n }\n order?: keyof M extends string ? `${keyof M}_${QUERY_ORDER_ENUM}`[] : never\n}\n\nexport type GeneDirective<\n TDirectiveArgs,\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgs = Record<string, unknown> | undefined,\n> = TDirectiveArgs extends undefined\n ? (args?: TDirectiveArgs) => GeneDirectiveConfig<TDirectiveArgs, TSource, TContext, TArgs>\n : (args: TDirectiveArgs) => GeneDirectiveConfig<TDirectiveArgs, TSource, TContext, TArgs>\n\nexport type GeneDirectiveConfig<\n TDirectiveArgs =\n | Record<string, string | number | boolean | string[] | number[] | boolean[] | null>\n | undefined,\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgs = Record<string, unknown> | undefined,\n> = TDirectiveArgs extends undefined\n ? {\n name: string\n args?: TDirectiveArgs\n handler: GeneDirectiveHandler<TSource, TContext, TArgs>\n }\n : {\n name: string\n args: TDirectiveArgs\n handler: GeneDirectiveHandler<TSource, TContext, TArgs>\n }\n\nexport type GeneDirectiveHandler<\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgs = Record<string, unknown> | undefined,\n TResult = unknown,\n> = (options: {\n source: Parameters<GraphQLFieldResolver<TSource, TContext, TArgs, TResult>>[0]\n args: Parameters<GraphQLFieldResolver<TSource, TContext, TArgs, TResult>>[1]\n context: Parameters<GraphQLFieldResolver<TSource, TContext, TArgs, TResult>>[2]\n info: Parameters<GraphQLFieldResolver<TSource, TContext, TArgs, TResult>>[3]\n field: string\n filter: <TValue>(callback: (value: TValue) => unknown) => void\n resolve: () => Promise<TResult> | TResult\n}) => Promise<void> | void\n\nexport class GeneModel {\n declare static geneConfig?: GeneConfig\n}\n\n/**\n * Provide typing and default values to GeneConfig. You need to pass the model class as the first\n * argument to ensure accurate types for \"include\" and \"exclude\" options (they only accept regular\n * expressions or one of the model field name as string).\n *\n * @default geneConfig.exclude - ['createdAt', 'updatedAt']\n */\nexport function defineGraphqlGeneConfig<\n M = unknown,\n TSource = M | Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgDefs extends ArgsDefinition = undefined,\n TReturnType extends string | unknown = unknown,\n TVarType extends GraphQLVarType = 'type',\n>(_model: M, options: GeneConfig<M, TSource, TContext, TArgDefs, TReturnType, TVarType>) {\n return options\n}\n\nexport function defineDirective<\n TDirectiveArgs = undefined,\n TSource = Record<string, unknown> | undefined,\n TContext = GeneContext,\n TArgs = Record<string, unknown> | undefined,\n>(directive: GeneDirective<TDirectiveArgs, TSource, TContext, TArgs>) {\n return directive as GeneDirective<\n TDirectiveArgs,\n Record<string, unknown> | undefined,\n TContext,\n TArgs\n >\n}\n\n/**\n * @deprecated Field config passed to `extendTypes` don't need to be wrapped with `defineField`\n * anymore in order to be accurate (they are now even better typed without it).\n */\nexport function defineField<\n TSource extends Record<string, unknown> | undefined,\n TArgDefs extends StrictArgsDefinition,\n TReturnType extends GraphqlReturnTypes<ValidGraphqlType>,\n TContext = GeneContext,\n>(config: Narrow<FieldConfig<TSource, TContext, TArgDefs, TReturnType>, 'args' | 'returnType'>) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return config as FieldConfig<any, TContext, any, TReturnType>\n}\n\nexport function defineType<\n T extends GeneObjectTypeConfig<TSource, TContext, TArgDefs>,\n TSource,\n TContext,\n TArgDefs extends ArgsDefinition,\n TVarType extends GraphQLVarType,\n>(config: T, geneConfig?: GeneConfig<T, TSource, TContext, TArgDefs, string, TVarType>) {\n return {\n ...config,\n ...(typeof geneConfig !== 'undefined' && {\n geneConfig: defineGraphqlGeneConfig({} as T, geneConfig),\n }),\n } as T\n}\n\nexport function defineInput<\n T extends GeneObjectTypeConfig<TSource, TContext, TArgDefs>,\n TSource,\n TContext,\n TArgDefs extends ArgsDefinition,\n>(config: T) {\n return {\n ...config,\n geneConfig: defineGraphqlGeneConfig({}, { varType: 'input' }),\n }\n}\n\nexport function defineEnum<TValue extends string>(values: TValue[]) {\n return values\n}\n\nexport function defineUnion<TUnion extends string>(unions: TUnion[]) {\n type UnionDef = Record<TUnion, ''>\n const unionDef: Partial<UnionDef> = {}\n\n unions.forEach(type => (unionDef[type] = ''))\n\n const completeUnionDef = unionDef as Required<typeof unionDef>\n\n return {\n ...completeUnionDef,\n geneConfig: defineGraphqlGeneConfig({}, { varType: 'union' }),\n }\n}\n"],"names":["GeneModel","defineGraphqlGeneConfig","_model","options","defineDirective","directive","defineField","config","defineType","geneConfig","defineInput","defineEnum","values","defineUnion","unions","unionDef","type"],"mappings":"gFA+SO,MAAMA,CAAU,CAEvB,CASO,SAASC,EAOdC,EAAWC,EAA4E,CACvF,OAAOA,CACT,CAEO,SAASC,EAKdC,EAAoE,CACpE,OAAOA,CAMT,CAMO,SAASC,EAKdC,EAA8F,CAE9F,OAAOA,CACT,CAEO,SAASC,EAMdD,EAAWE,EAA2E,CACtF,MAAO,CACL,GAAGF,EACH,GAAI,OAAOE,EAAe,KAAe,CACvC,WAAYR,EAAwB,CAAA,EAASQ,CAAU,CAAA,CACzD,CAEJ,CAEO,SAASC,EAKdH,EAAW,CACX,MAAO,CACL,GAAGA,EACH,WAAYN,EAAwB,CAAA,EAAI,CAAE,QAAS,QAAS,CAAA,CAEhE,CAEO,SAASU,EAAkCC,EAAkB,CAClE,OAAOA,CACT,CAEO,SAASC,EAAmCC,EAAkB,CAEnE,MAAMC,EAA8B,CAAA,EAEpC,OAAAD,EAAO,QAAQE,GAASD,EAASC,CAAI,EAAI,EAAG,EAIrC,CACL,GAHuBD,EAIvB,WAAYd,EAAwB,CAAA,EAAI,CAAE,QAAS,QAAS,CAAA,CAEhE"}