deno.land / x / pothos@release-1713397530 / packages / core / utils / 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
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
// @ts-nocheckimport type BuildCache from '../build-cache.ts';import { PothosSchemaError } from '../errors.ts';import { PothosInputFieldConfig, PothosInputFieldType, PothosTypeConfig, SchemaTypes, } from '../types/index.ts';import { unwrapInputFieldType } from './params.ts';export interface InputTypeFieldsMapping<Types extends SchemaTypes, T> { configs: Record<string, PothosInputFieldConfig<Types>>; map: InputFieldsMapping<Types, T> | null;}export type InputFieldMapping<Types extends SchemaTypes, T> = { kind: "Enum"; isList: boolean; config: PothosInputFieldConfig<Types>; value: T;} | { kind: "InputObject"; config: PothosInputFieldConfig<Types>; isList: boolean; value: T | null; fields: InputTypeFieldsMapping<Types, T>;} | { kind: "Scalar"; isList: boolean; config: PothosInputFieldConfig<Types>; value: T;};export type InputFieldsMapping<Types extends SchemaTypes, T> = Map<string, InputFieldMapping<Types, T>>;export function resolveInputTypeConfig<Types extends SchemaTypes>(type: PothosInputFieldType<Types>, buildCache: BuildCache<Types>): Extract<PothosTypeConfig, { kind: "Enum" | "InputObject" | "Scalar";}> { if (type.kind === "List") { return resolveInputTypeConfig(type.type, buildCache); } const config = buildCache.getTypeConfig(type.ref); if (config.kind === "Enum" || config.kind === "Scalar" || config.kind === "InputObject") { return config; } throw new PothosSchemaError(`Unexpected config type ${config.kind} for input ref ${String(type.ref)}`);}export function mapInputFields<Types extends SchemaTypes, T>(inputs: Record<string, PothosInputFieldConfig<Types>>, buildCache: BuildCache<Types>, mapper: (config: PothosInputFieldConfig<Types>) => T | null): InputFieldsMapping<Types, T> | null { const filterMappings = new Map<InputFieldsMapping<Types, T>, InputFieldsMapping<Types, T>>(); return filterMapped(internalMapInputFields(inputs, buildCache, mapper, new Map<string, InputTypeFieldsMapping<Types, T>>())); function filterMapped(map: InputFieldsMapping<Types, T>) { if (filterMappings.has(map)) { return filterMappings.get(map)!; } const filtered = new Map<string, InputFieldMapping<Types, T>>(); filterMappings.set(map, filtered); map.forEach((mapping, fieldName) => { if (mapping.kind === "Enum" || mapping.kind === "Scalar") { filtered.set(fieldName, mapping); return; } const hasNestedMappings = checkForMappings(mapping.fields.map!); if (mapping.value !== null || hasNestedMappings) { const filteredTypeFields = filterMapped(mapping.fields.map!); const mappingForType = { ...mapping, fields: { configs: mapping.fields.configs, map: filteredTypeFields, }, }; filtered.set(fieldName, mappingForType); } }); return filtered.size > 0 ? filtered : null; } function checkForMappings(map: InputFieldsMapping<Types, T>, hasMappings = new Map<InputFieldsMapping<Types, T>, boolean>()): boolean { if (hasMappings.has(map)) { return hasMappings.get(map)!; } hasMappings.set(map, false); let result = false; map.forEach((mapping) => { if (mapping.value !== null) { result = true; } else if (mapping.kind === "InputObject" && mapping.fields.map && checkForMappings(mapping.fields.map, hasMappings)) { result = true; } }); hasMappings.set(map, result); return result; }}function internalMapInputFields<Types extends SchemaTypes, T>(inputs: Record<string, PothosInputFieldConfig<Types>>, buildCache: BuildCache<Types>, mapper: (config: PothosInputFieldConfig<Types>) => T | null, seenTypes: Map<string, InputTypeFieldsMapping<Types, T>>) { const map = new Map<string, InputFieldMapping<Types, T>>(); Object.keys(inputs).forEach((fieldName) => { const inputField = inputs[fieldName]; const typeConfig = resolveInputTypeConfig(inputField.type, buildCache); const fieldMapping = mapper(inputField); if (typeConfig.kind === "Enum" || typeConfig.kind === "Scalar") { if (fieldMapping !== null) { map.set(fieldName, { kind: typeConfig.kind, isList: inputField.type.kind === "List", config: inputField, value: fieldMapping, }); } return; } const inputFieldConfigs = buildCache.getInputTypeFieldConfigs(unwrapInputFieldType(inputField.type)); if (!seenTypes.has(typeConfig.name)) { const typeEntry = { configs: inputFieldConfigs, map: new Map<string, InputFieldMapping<Types, T>>(), }; seenTypes.set(typeConfig.name, typeEntry); typeEntry.map = internalMapInputFields(inputFieldConfigs, buildCache, mapper, seenTypes); } const typeFields = seenTypes.get(typeConfig.name)!; map.set(fieldName, { kind: typeConfig.kind, isList: inputField.type.kind === "List", config: inputField, value: fieldMapping, fields: typeFields, }); }); return map;}export function createInputValueMapper<Types extends SchemaTypes, T, Args extends unknown[] = []>(argMap: InputFieldsMapping<Types, T>, mapValue: (val: unknown, mapping: InputFieldMapping<Types, T>, ...args: Args) => unknown) { return function mapObject(obj: object, map: InputFieldsMapping<Types, T> = argMap, ...args: Args) { const mapped: Record<string, unknown> = { ...obj }; map.forEach((field, fieldName) => { let fieldVal = (obj as Record<string, unknown>)[fieldName]; if (fieldVal === null || fieldVal === undefined) { return; } if (field.kind === "InputObject" && field.fields.map) { fieldVal = field.isList ? (fieldVal as (Record<string, unknown> | null)[]).map((val) => val && mapObject(val, field.fields.map!, ...args)) : mapObject(fieldVal as Record<string, unknown>, field.fields.map, ...args); mapped[fieldName] = fieldVal; } if (field.kind !== "InputObject" || field.value !== null) { mapped[fieldName] = field.isList ? (fieldVal as unknown[]).map((val) => val == null ? val : mapValue(val, field, ...args)) : mapValue(fieldVal, field, ...args); } }); return mapped; };}
pothos

Version Info

Tagged at
a year ago