deno.land / x / pothos@release-1713397530 / packages / core / fieldUtils / base.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// @ts-nocheckimport { defaultFieldResolver } from 'https://cdn.skypack.dev/graphql?dts';import { PothosSchemaError } from '../errors.ts';import FieldRef from '../refs/field.ts';import type { FieldKind, InputFieldMap, PothosInputFieldConfig, ShapeFromTypeParam, } from '../types/index.ts';import { FieldNullability, SchemaTypes, TypeParam } from '../types/index.ts';import { typeFromParam } from '../utils/index.ts';export default class BaseFieldUtil<Types extends SchemaTypes, ParentShape, Kind extends FieldKind> { typename: string; builder: PothosSchemaTypes.SchemaBuilder<Types>; kind: Kind; graphqlKind: PothosSchemaTypes.PothosKindToGraphQLType[Kind]; constructor(name: string, builder: PothosSchemaTypes.SchemaBuilder<Types>, kind: Kind, graphqlKind: PothosSchemaTypes.PothosKindToGraphQLType[Kind]) { this.typename = name; this.builder = builder; this.kind = kind; this.graphqlKind = graphqlKind; } protected createField<Args extends InputFieldMap, Type extends TypeParam<Types>, Nullable extends FieldNullability<Type>>( // eslint-disable-next-line @typescript-eslint/no-explicit-any options: PothosSchemaTypes.FieldOptions<Types, ParentShape, Type, Nullable, Args, any, {}>): FieldRef<ShapeFromTypeParam<Types, Type, Nullable>, Kind> { const ref: FieldRef<ShapeFromTypeParam<Types, Type, Nullable>, Kind> = new FieldRef(this.kind, this.typename); this.builder.configStore.addFieldRef(ref, options.type, options.args ?? {}, (name, parentField, typeConfig) => { const args: Record<string, PothosInputFieldConfig<Types>> = {}; if (options.args) { Object.keys(options.args).forEach((argName) => { const argRef = options.args![argName]; args[argName] = this.builder.configStore.createFieldConfig(argRef, argName, typeConfig, name, "Arg"); }); } let resolve = (options as { resolve?: (...argList: unknown[]) => unknown; }).resolve ?? (() => { throw new PothosSchemaError(`Not implemented: No resolver found for ${this.typename}.${name}`); }); if (options.extensions?.pothosExposedField === name) { resolve = defaultFieldResolver as typeof resolve; } const { subscribe } = options as { subscribe?: (...argList: unknown[]) => unknown; }; return { kind: this.kind as never, graphqlKind: this.graphqlKind, parentType: typeConfig.name, name, args, type: typeFromParam(options.type, this.builder.configStore, options.nullable ?? this.builder.defaultFieldNullability), pothosOptions: options as never, extensions: { pothosOriginalResolve: resolve, pothosOriginalSubscribe: subscribe, ...options.extensions, }, description: options.description, deprecationReason: options.deprecationReason, resolve, subscribe, }; }); return ref; } protected exposeField<Type extends TypeParam<Types>, Nullable extends FieldNullability<Type>, Name extends string & keyof ParentShape>(name: Name, { extensions, ...options }: Omit<PothosSchemaTypes.ObjectFieldOptions<Types, ParentShape, Type, Nullable, {}, {}>, "resolve">): FieldRef<ShapeFromTypeParam<Types, Type, Nullable>, Kind> { return this.createField({ ...options, extensions: { pothosExposedField: name, ...extensions, }, resolve: (parent) => (parent as Record<string, never>)[name as string], }); }}
pothos

Version Info

Tagged at
a year ago