deno.land / x / pothos@release-1713397530 / packages / plugin-directives / mock-ast.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
// @ts-nocheck/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion *//* eslint-disable no-param-reassign */import './global-types.ts';import { ArgumentNode, astFromValue, ConstDirectiveNode, ConstValueNode, DirectiveNode, EnumValueDefinitionNode, FieldDefinitionNode, GraphQLArgument, GraphQLEnumType, GraphQLEnumValue, GraphQLField, GraphQLFieldMap, GraphQLInputField, GraphQLInputFieldMap, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLScalarType, GraphQLSchema, GraphQLType, GraphQLUnionType, InputValueDefinitionNode, Kind, ListTypeNode, NamedTypeNode, OperationTypeNode, parseValue, TypeNode, ValueNode, } from 'https://cdn.skypack.dev/graphql?dts';import type { DirectiveList } from './types.ts';export default function mockAst(schema: GraphQLSchema) { const types = schema.getTypeMap(); schema.extensionASTNodes = [ { kind: Kind.SCHEMA_EXTENSION, directives: directiveNodes(schema.extensions?.directives as DirectiveList), operationTypes: ([ { operation: "query" as OperationTypeNode, node: schema.getQueryType(), }, { operation: "mutation" as OperationTypeNode, node: schema.getMutationType(), }, { operation: "subscription" as OperationTypeNode, node: schema.getSubscriptionType(), }, ] as const) .filter(({ node, operation }) => node && node.name !== `${operation[0].toUpperCase()}${operation.slice(1)}`) .map(({ operation, node }) => ({ kind: Kind.OPERATION_TYPE_DEFINITION, operation, type: { kind: Kind.NAMED_TYPE, name: { kind: Kind.NAME, value: node!.name } }, })), }, ]; Object.keys(types).forEach((typeName) => { const type = types[typeName]; if (type instanceof GraphQLObjectType) { type.astNode = { kind: Kind.OBJECT_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, interfaces: type.getInterfaces().map((iface) => typeNode(iface) as NamedTypeNode), fields: fieldNodes(type.getFields()), directives: directiveNodes(type.extensions?.directives as DirectiveList), }; } else if (type instanceof GraphQLInterfaceType) { type.astNode = { kind: Kind.INTERFACE_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, interfaces: type.getInterfaces().map((iface) => typeNode(iface) as NamedTypeNode), fields: fieldNodes(type.getFields()), directives: directiveNodes(type.extensions?.directives as DirectiveList), }; } else if (type instanceof GraphQLUnionType) { type.astNode = { kind: Kind.UNION_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, types: type.getTypes().map((iface) => typeNode(iface) as NamedTypeNode), directives: directiveNodes(type.extensions?.directives as DirectiveList), }; } else if (type instanceof GraphQLEnumType) { type.astNode = { kind: Kind.ENUM_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, values: enumValueNodes(type.getValues()), directives: directiveNodes(type.extensions?.directives as DirectiveList), }; } else if (type instanceof GraphQLScalarType) { type.astNode = { kind: Kind.SCALAR_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, directives: directiveNodes(type.extensions?.directives as DirectiveList), }; } else if (type instanceof GraphQLInputObjectType) { type.astNode = { kind: Kind.INPUT_OBJECT_TYPE_DEFINITION, name: { kind: Kind.NAME, value: typeName }, description: type.description ? { kind: Kind.STRING, value: type.description } : undefined, fields: inputFieldNodes(type.getFields()), directives: directiveNodes(type.extensions?.directives as DirectiveList), }; } });}function typeNode(type: GraphQLType): TypeNode { if (type instanceof GraphQLList) { return { kind: Kind.LIST_TYPE, type: typeNode(type.ofType) }; } if (type instanceof GraphQLNonNull) { return { kind: Kind.NON_NULL_TYPE, type: typeNode(type.ofType as GraphQLType) as ListTypeNode | NamedTypeNode, }; } return { kind: Kind.NAMED_TYPE, name: { kind: Kind.NAME, value: type.name } };}function valueNode(value: unknown): ValueNode { if (value == null) { return { kind: Kind.NULL }; } if (Array.isArray(value)) { return { kind: Kind.LIST, values: value.map(valueNode) }; } switch (typeof value) { case "object": return { kind: Kind.OBJECT, fields: Object.keys(value!).map((key) => ({ kind: Kind.OBJECT_FIELD, name: { kind: Kind.NAME, value: key }, value: valueNode((value as Record<string, unknown>)[key]), })), }; default: return parseValue(JSON.stringify(value)); }}function directiveNodes(directives: DirectiveList | Record<string, {}> | undefined, deprecationReason?: string | null): readonly ConstDirectiveNode[] { if (!directives) { return []; } const directiveList = Array.isArray(directives) ? directives : Object.keys(directives).flatMap((name) => Array.isArray(directives[name]) ? (directives[name] as {}[]).map((args) => ({ name, args, })) : { name, args: directives[name], }); if (deprecationReason) { directiveList.unshift({ name: "deprecated", args: { reason: deprecationReason!, }, }); } return directiveList.map((directive): DirectiveNode => ({ kind: Kind.DIRECTIVE, name: { kind: Kind.NAME, value: directive.name }, arguments: directive.args && Object.keys(directive.args).map((argName): ArgumentNode => ({ kind: Kind.ARGUMENT, name: { kind: Kind.NAME, value: argName }, value: valueNode((directive.args as Record<string, unknown>)[argName]), })), })) as readonly ConstDirectiveNode[];}function fieldNodes(fields: GraphQLFieldMap<unknown, unknown>): FieldDefinitionNode[] { return Object.keys(fields).map((fieldName) => { const field: GraphQLField<unknown, unknown> = fields[fieldName]; field.astNode = { kind: Kind.FIELD_DEFINITION, description: field.description ? { kind: Kind.STRING, value: field.description } : undefined, name: { kind: Kind.NAME, value: fieldName }, arguments: argumentNodes(field.args), type: typeNode(field.type), directives: directiveNodes(field.extensions?.directives as DirectiveList, field.deprecationReason), }; return field.astNode!; });}function inputFieldNodes(fields: GraphQLInputFieldMap): InputValueDefinitionNode[] { return Object.keys(fields).map((fieldName) => { const field: GraphQLInputField = fields[fieldName]; const defaultValueNode = astFromValue(field.defaultValue, field.type) as ConstValueNode; field.astNode = { kind: Kind.INPUT_VALUE_DEFINITION, description: field.description ? { kind: Kind.STRING, value: field.description } : undefined, name: { kind: Kind.NAME, value: fieldName }, type: typeNode(field.type), defaultValue: field.defaultValue === undefined ? undefined : defaultValueNode, directives: directiveNodes(field.extensions?.directives as DirectiveList, field.deprecationReason), }; return field.astNode!; });}function argumentNodes(args: readonly GraphQLArgument[]): InputValueDefinitionNode[] { return args.map((arg): InputValueDefinitionNode => { const defaultValueNode = astFromValue(arg.defaultValue, arg.type) as ConstValueNode; arg.astNode = { kind: Kind.INPUT_VALUE_DEFINITION, description: arg.description ? { kind: Kind.STRING, value: arg.description } : undefined, name: { kind: Kind.NAME, value: arg.name }, type: typeNode(arg.type), defaultValue: arg.defaultValue === undefined ? undefined : defaultValueNode, directives: directiveNodes(arg.extensions?.directives as DirectiveList, arg.deprecationReason), }; return arg.astNode; });}function enumValueNodes(values: readonly GraphQLEnumValue[]): readonly EnumValueDefinitionNode[] { return values.map((value): EnumValueDefinitionNode => { value.astNode = { kind: Kind.ENUM_VALUE_DEFINITION, description: value.description ? { kind: Kind.STRING, value: value.description } : undefined, name: { kind: Kind.NAME, value: value.name }, directives: directiveNodes(value.extensions?.directives as DirectiveList, value.deprecationReason), }; return value.astNode; });}
pothos

Version Info

Tagged at
a year ago