deno.land / x / pothos@release-1713397530 / packages / core / fieldUtils / input.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// @ts-nocheckimport InputFieldRef from '../refs/input-field.ts';import InputListRef from '../refs/input-list.ts';import type { ArgBuilder, FieldRequiredness, InputShapeFromTypeParam, InputTypeParam, NormalizeArgs, } from '../types/index.ts';import { InputType, SchemaTypes } from '../types/index.ts';import { inputTypeFromParam } from '../utils/index.ts';export default class InputFieldBuilder<Types extends SchemaTypes, Kind extends keyof PothosSchemaTypes.InputFieldOptionsByKind> { builder: PothosSchemaTypes.SchemaBuilder<Types>; kind: Kind; typename: string; /** * Create a Boolean input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ boolean = this.helper("Boolean"); /** * Create a Float input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ float = this.helper("Float"); /** * Create a ID input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ id = this.helper("ID"); /** * Create a Int input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ int = this.helper("Int"); /** * Create a String input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ string = this.helper("String"); /** * Create a Boolean list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ booleanList = this.helper(["Boolean"]); /** * Create a Float list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ floatList = this.helper(["Float"]); /** * Create a ID list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ idList = this.helper(["ID"]); /** * Create a Int list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ intList = this.helper(["Int"]); /** * Create a String list input field * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ stringList = this.helper(["String"]); constructor(builder: PothosSchemaTypes.SchemaBuilder<Types>, kind: Kind, typename: string) { this.builder = builder; this.kind = kind; this.typename = typename; } listRef = <T extends InputTypeParam<Types>, Required extends boolean = true>(type: T, options?: { required?: Required; }): InputListRef<Types, InputShapeFromTypeParam<Types, T, Required>[]> => new InputListRef<Types, InputShapeFromTypeParam<Types, T, Required>[]>(type, options?.required ?? true); argBuilder(): ArgBuilder<Types> { const builder = this.field.bind(this as never) as InputFieldBuilder<Types, "Arg">["field"]; const protoKeys = Object.keys(Object.getPrototypeOf(this) as object).filter((key) => typeof (this as Record<string, unknown>)[key] === "function" && (Function.prototype as unknown as Record<string, unknown>)[key] === undefined); ([...Object.keys(this), ...protoKeys] as (keyof InputFieldBuilder<Types, "Arg">)[]).forEach((key) => { (builder as unknown as Record<string, unknown>)[key] = typeof this[key] === "function" ? (this[key] as Function).bind(this) : this[key]; }); return builder as ArgBuilder<Types>; } /** * Create in input field or argument for the current type * @param {PothosSchemaTypes.InputFieldOptions} [options={}] - Options for this field */ field<Type extends InputType<Types> | [ InputType<Types> ], Req extends FieldRequiredness<Type>>(options: PothosSchemaTypes.InputFieldOptionsByKind<Types, Type, Req>[Kind]): InputFieldRef<InputShapeFromTypeParam<Types, Type, Req>, Kind> { const ref: InputFieldRef<InputShapeFromTypeParam<Types, Type, Req>, Kind> = new InputFieldRef(this.kind, this.typename); this.builder.configStore.addFieldRef(ref, options.type, {}, (name, parentField, typeConfig) => ({ name, parentField, kind: this.kind, graphqlKind: this.kind, parentType: typeConfig.name, type: inputTypeFromParam<Types>(options.type, this.builder.configStore, options.required ?? this.builder.defaultInputFieldRequiredness), pothosOptions: options as unknown as PothosSchemaTypes.InputFieldOptionsByKind<Types>[Kind], description: options.description, deprecationReason: options.deprecationReason, defaultValue: options.defaultValue, extensions: options.extensions, })); return ref; } private helper<Type extends InputType<Types> | [ InputType<Types> ]>(type: Type) { return <Req extends FieldRequiredness<Type>>(...args: NormalizeArgs<[ options: Omit<PothosSchemaTypes.InputFieldOptionsByKind<Types, Type, Req>[Kind], "type"> ]>) => { const [options = {} as never] = args; return this.field({ ...options, type, } as PothosSchemaTypes.InputFieldOptionsByKind<Types, Type, Req>[Kind]); }; }}
pothos

Version Info

Tagged at
a year ago