UNPKG

graphql-gene

Version:

Generates automatically an executable schema out of your ORM models

74 lines (73 loc) 2.86 kB
import { GraphQLSchema, GraphQLResolveInfo } from 'graphql'; import { GeneContext } from 'graphql-gene/context'; import { GeneDefaultResolverArgs, GeneTypeConfig } from '../defineConfig'; import { AnyObject } from './typeUtils'; import { GraphQLVarType, ResolversOrScalars } from './graphql'; import { GraphqlToTypescript } from './graphqlToTypescript'; export type PluginSettings<T extends { isMatching: boolean; fieldName: TField extends string ? string : never; findOptionsState: object; }, TField extends string | number | symbol = string> = T; export type GraphQLTypeName = string; export type GraphQLFieldName = string; export type ArgsDef = Record<string, Set<string | string[] | null>>; export type FieldLines = Record<GraphQLFieldName, { directives: Set<string>; argsDef: ArgsDef; typeDef: string; }>; export type TypeDefLines = Record<GraphQLTypeName, { varType: GraphQLVarType; directives: Set<string>; lines: FieldLines; }>; export type DirectiveDefs = Record<GraphQLTypeName, { argsDef: ArgsDef; }>; export type GenerateSchemaOptions<SchemaTypes extends AnyObject = AnyObject, DataTypes extends AnyObject = AnyObject> = { schema?: GraphQLSchema; resolvers?: ResolversOrScalars; plugins?: GenePlugin[]; types: SchemaTypes; dataTypeMap?: DataTypes; }; export type GenePlugin<M = any> = { /** * Function receiving the model and returning true if the plugin should run. */ isMatching: (model: M) => boolean; /** * @deprecated In favor of "populateTypeDefs" */ getTypeDef(options: { model: M; typeName: string; isFieldIncluded: (fieldKey: string) => boolean; schemaOptions: GenerateSchemaOptions; }): TypeDefLines[0]; /** * Populate the typeDefLines object with one or multiple type definitions. It might need to * define multiple types if one field has arguments with inputs specific to this types * (i.e. associations using the default resolver). * * It returns { afterTypeDefHooks } which are hooks called after all model types are added * to the typeDefLines object. */ populateTypeDefs(options: { typeDefLines: TypeDefLines; model: M; typeName: string; isFieldIncluded: (fieldKey: string) => boolean; schemaOptions: GenerateSchemaOptions; }): { afterTypeDefHooks: (() => void)[]; }; defaultResolver?<M, ModelKey extends string, TSource = Record<string, unknown> | undefined, TContext = GeneContext, TArgDefs extends Record<string, string> = Record<string, string>>(options: { model: M; modelKey: ModelKey; config: GeneTypeConfig<TSource, TContext, TArgDefs>; args: GeneDefaultResolverArgs<M>; info: GraphQLResolveInfo; }): Promise<GraphqlToTypescript<ModelKey>>; };