deno.land / x / pothos@release-1713397530 / packages / plugin-add-graphql / schema-builder.ts

schema-builder.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// @ts-nocheckimport './global-types.ts';import { defaultFieldResolver, getNamedType, GraphQLEnumType, GraphQLInputObjectType, GraphQLInputType, GraphQLInterfaceType, GraphQLNamedInputType, GraphQLNamedOutputType, GraphQLObjectType, GraphQLOutputType, GraphQLUnionType, isListType, isNonNullType, } from 'https://cdn.skypack.dev/graphql?dts';import SchemaBuilder, { EnumRef, EnumValueConfigMap, InputFieldRef, InputType, InputTypeParam, ObjectParam, ObjectRef, OutputType, SchemaTypes, TypeParam, } from '../core/index.ts';import { AddGraphQLEnumTypeOptions, AddGraphQLInputTypeOptions, AddGraphQLInterfaceTypeOptions, AddGraphQLObjectTypeOptions, AddGraphQLUnionTypeOptions, EnumValuesWithShape, } from './types.ts';import { addReferencedType } from './utils.ts';const proto = SchemaBuilder.prototype as PothosSchemaTypes.SchemaBuilder<SchemaTypes>;function resolveOutputTypeRef(builder: PothosSchemaTypes.SchemaBuilder<SchemaTypes>, type: GraphQLNamedOutputType) { addReferencedType(builder, type); return type.name as OutputType<SchemaTypes>;}function resolveInputTypeRef(builder: PothosSchemaTypes.SchemaBuilder<SchemaTypes>, type: GraphQLNamedInputType) { addReferencedType(builder, type); return type.name as InputType<SchemaTypes>;}function resolveOutputType(builder: PothosSchemaTypes.SchemaBuilder<SchemaTypes>, type: GraphQLOutputType): { type: TypeParam<SchemaTypes>; nullable: boolean;} { const namedType = getNamedType(type); const isNullable = !isNonNullType(type); const nonNullable = isNonNullType(type) ? type.ofType : type; const isList = isListType(nonNullable); const typeRef = resolveOutputTypeRef(builder, namedType); if (!isList) { return { type: typeRef, nullable: isNullable, }; } return { type: [typeRef], nullable: { list: isNullable, items: !isNonNullType(nonNullable.ofType), } as unknown as boolean, };}function resolveInputType(builder: PothosSchemaTypes.SchemaBuilder<SchemaTypes>, type: GraphQLInputType): { type: InputTypeParam<SchemaTypes>; required: boolean;} { const namedType = getNamedType(type); const isNullable = !isNonNullType(type); const nonNullable = isNonNullType(type) ? type.ofType : type; const isList = isListType(nonNullable); const typeRef = resolveInputTypeRef(builder, namedType); if (!isList) { return { type: typeRef, required: !isNullable, }; } return { type: [typeRef], required: { list: !isNullable, items: isNonNullType(nonNullable.ofType), } as unknown as boolean, };}proto.addGraphQLObject = function addGraphQLObject<Shape>(type: GraphQLObjectType<Shape>, { fields, extensions, ...options }: AddGraphQLObjectTypeOptions<SchemaTypes, Shape> = {}) { const typeOptions = { ...options, description: type.description ?? undefined, isTypeOf: type.isTypeOf as never, extensions: { ...type.extensions, ...extensions }, interfaces: () => type.getInterfaces().map((i) => resolveOutputTypeRef(this, i)) as [ ], fields: (t: PothosSchemaTypes.ObjectFieldBuilder<SchemaTypes, Shape>) => { const existingFields = type.getFields(); const newFields = fields?.(t) ?? {}; const combinedFields: typeof newFields = { ...newFields, }; Object.entries(existingFields).forEach(([fieldName, field]) => { if (newFields[fieldName] !== undefined) { if (newFields[fieldName] === null) { delete combinedFields[fieldName]; } return; } const args: Record<string, InputFieldRef> = {}; for (const { name, ...arg } of field.args) { const input = resolveInputType(this, arg.type); args[name] = t.arg({ ...input, description: arg.description ?? undefined, deprecationReason: arg.deprecationReason ?? undefined, defaultValue: arg.defaultValue, extensions: arg.extensions, }); } combinedFields[fieldName] = t.field({ ...resolveOutputType(this, field.type), args, description: field.description ?? undefined, deprecationReason: field.deprecationReason ?? undefined, resolve: (field.resolve ?? defaultFieldResolver) as never, }); }); return combinedFields as {}; }, }; switch (type.name) { case "Query": this.queryType(typeOptions as never); return "Query" as never; case "Mutation": this.mutationType(typeOptions as never); return "Mutation" as never; case "Subscription": this.subscriptionType(typeOptions as never); return "Subscription" as never; default: return this.objectRef<Shape>(options?.name ?? type.name).implement(typeOptions as never); }};proto.addGraphQLInterface = function addGraphQLInterface<Shape = unknown>(type: GraphQLInterfaceType, { fields, extensions, ...options }: AddGraphQLInterfaceTypeOptions<SchemaTypes, Shape> = {}) { const ref = this.interfaceRef<Shape>(options?.name ?? type.name); ref.implement({ ...options, description: type.description ?? undefined, resolveType: type.resolveType as never, extensions: { ...type.extensions, ...extensions }, interfaces: () => type.getInterfaces().map((i) => resolveOutputTypeRef(this, i)) as [ ], fields: (t) => { const existingFields = type.getFields(); const newFields = fields?.(t) ?? {}; const combinedFields: typeof newFields = { ...newFields, }; Object.entries(existingFields).forEach(([fieldName, field]) => { if (newFields[fieldName] !== undefined) { if (newFields[fieldName] === null) { delete combinedFields[fieldName]; } return; } const args: Record<string, InputFieldRef> = {}; for (const { name, ...arg } of field.args) { args[name] = t.arg({ ...resolveInputType(this, arg.type), description: arg.description ?? undefined, deprecationReason: arg.deprecationReason ?? undefined, defaultValue: arg.defaultValue, extensions: arg.extensions, }); } combinedFields[fieldName] = t.field({ ...resolveOutputType(this, field.type), args, description: field.description ?? undefined, deprecationReason: field.deprecationReason ?? undefined, resolve: field.resolve as never, }); }); return combinedFields as {}; }, }); return ref;};proto.addGraphQLUnion = function addGraphQLUnion<Shape>(type: GraphQLUnionType, { types, extensions, ...options }: AddGraphQLUnionTypeOptions<SchemaTypes, ObjectRef<Shape>> = {}) { return this.unionType<ObjectParam<SchemaTypes>, Shape>(options?.name ?? type.name, { ...options, description: type.description ?? undefined, resolveType: type.resolveType as never, extensions: { ...type.extensions, ...extensions }, types: types ?? (type.getTypes().map((t) => resolveOutputTypeRef(this, t)) as [ ]), });};proto.addGraphQLEnum = function addGraphQLEnum<Shape extends number | string>(type: GraphQLEnumType, { values, extensions, ...options }: AddGraphQLEnumTypeOptions<SchemaTypes, EnumValuesWithShape<SchemaTypes, Shape>> = {}) { const newValues = values ?? type.getValues().reduce<EnumValueConfigMap<SchemaTypes>>((acc, value) => { acc[value.name] = { value: value.value as never, description: value.description ?? undefined, deprecationReason: value.deprecationReason ?? undefined, extensions: value.extensions, }; return acc; }, {}); const ref: EnumRef<Shape> = this.enumType<never, EnumValuesWithShape<SchemaTypes, Shape>>((options?.name ?? type.name) as never, { ...options, description: type.description ?? undefined, extensions: { ...type.extensions, ...extensions }, values: newValues, } as never); return ref;};proto.addGraphQLInput = function addGraphQLInput<Shape extends {}>(type: GraphQLInputObjectType, { name = type.name, fields, extensions, ...options }: AddGraphQLInputTypeOptions<SchemaTypes, Shape> = {}) { const ref = this.inputRef<Shape>(name); return ref.implement({ ...options, description: type.description ?? undefined, extensions: { ...type.extensions, ...extensions }, fields: (t) => { const existingFields = type.getFields(); const newFields: Record<string, InputFieldRef<unknown, "InputObject"> | null> = fields?.(t) ?? {}; const combinedFields: typeof newFields = { ...newFields, }; Object.entries(existingFields).forEach(([fieldName, field]) => { if (newFields[fieldName] !== undefined) { if (newFields[fieldName] === null) { delete combinedFields[fieldName]; } return; } combinedFields[fieldName] = t.field({ ...resolveInputType(this, field.type), description: field.description ?? undefined, defaultValue: field.defaultValue, extensions: field.extensions, }); }); return combinedFields as never; }, }) as never;};
pothos

Version Info

Tagged at
a year ago