@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 204 lines 6.63 KB
Raw
1 /**
2 * Schema model shared by the SDL and introspection readers.
3 *
4 * - Names are strings; type references are chains ending in a name. No locations
5 * are retained. Absent optional values are `undefined`.
6 * - Lists preserve declaration order, including extensions. Interface
7 * `possibleTypes` are sorted by name.
8 * - The five built-in scalars are always present without descriptions.
9 * Introspection meta types and directive definitions are omitted.
10 * - Applied directives are discarded after extracting `deprecationReason` and
11 * `oneOf`. A missing deprecation reason becomes `"No longer supported"`.
12 * - Default values are parsed const values without locations.
13 *
14 * @internal
15 */
16 import type * as Ast from "./Ast.ts"
17
18 export interface Schema {
19 readonly queryType: string
20 readonly mutationType: string | undefined
21 readonly subscriptionType: string | undefined
22 readonly types: ReadonlyMap<string, NamedType>
23 }
24
25 export type NamedType = ScalarType | ObjectType | InterfaceType | UnionType | EnumType | InputObjectType
26
27 export interface ScalarType {
28 readonly _tag: "ScalarType"
29 readonly name: string
30 readonly description: string | undefined
31 }
32
33 export interface ObjectType {
34 readonly _tag: "ObjectType"
35 readonly name: string
36 readonly description: string | undefined
37 readonly interfaces: ReadonlyArray<string>
38 readonly fields: ReadonlyArray<Field>
39 }
40
41 export interface InterfaceType {
42 readonly _tag: "InterfaceType"
43 readonly name: string
44 readonly description: string | undefined
45 readonly interfaces: ReadonlyArray<string>
46 readonly fields: ReadonlyArray<Field>
47 /** The object types implementing this interface, sorted by name. */
48 readonly possibleTypes: ReadonlyArray<string>
49 }
50
51 export interface UnionType {
52 readonly _tag: "UnionType"
53 readonly name: string
54 readonly description: string | undefined
55 /** The member types in declaration order. */
56 readonly possibleTypes: ReadonlyArray<string>
57 }
58
59 export interface EnumType {
60 readonly _tag: "EnumType"
61 readonly name: string
62 readonly description: string | undefined
63 readonly values: ReadonlyArray<EnumValueDefinition>
64 }
65
66 export interface InputObjectType {
67 readonly _tag: "InputObjectType"
68 readonly name: string
69 readonly description: string | undefined
70 readonly oneOf: boolean
71 readonly fields: ReadonlyArray<InputValue>
72 }
73
74 export interface Field {
75 readonly name: string
76 readonly description: string | undefined
77 readonly arguments: ReadonlyArray<InputValue>
78 readonly type: TypeRef
79 readonly deprecationReason: string | undefined
80 }
81
82 /** An argument, an input object field or a directive argument. */
83 export interface InputValue {
84 readonly name: string
85 readonly description: string | undefined
86 readonly type: TypeRef
87 readonly defaultValue: ConstValue | undefined
88 readonly deprecationReason: string | undefined
89 }
90
91 export interface EnumValueDefinition {
92 readonly name: string
93 readonly description: string | undefined
94 readonly deprecationReason: string | undefined
95 }
96
97 export type TypeRef = NamedTypeRef | ListTypeRef | NonNullTypeRef
98
99 export interface NamedTypeRef {
100 readonly _tag: "NamedTypeRef"
101 readonly name: string
102 }
103
104 export interface ListTypeRef {
105 readonly _tag: "ListTypeRef"
106 readonly ofType: TypeRef
107 }
108
109 export interface NonNullTypeRef {
110 readonly _tag: "NonNullTypeRef"
111 readonly ofType: NamedTypeRef | ListTypeRef
112 }
113
114 /**
115 * A `Value[Const]` without locations. Tags and `value` encodings match the
116 * parser's AST: numbers keep their source text, strings are decoded.
117 */
118 export type ConstValue =
119 | { readonly _tag: "IntValue"; readonly value: string }
120 | { readonly _tag: "FloatValue"; readonly value: string }
121 | { readonly _tag: "StringValue"; readonly value: string }
122 | { readonly _tag: "BooleanValue"; readonly value: boolean }
123 | { readonly _tag: "NullValue" }
124 | { readonly _tag: "EnumValue"; readonly value: string }
125 | { readonly _tag: "ListValue"; readonly values: ReadonlyArray<ConstValue> }
126 | { readonly _tag: "ObjectValue"; readonly fields: ReadonlyArray<ConstObjectField> }
127
128 export interface ConstObjectField {
129 readonly name: string
130 readonly value: ConstValue
131 }
132
133 // -----------------------------------------------------------------------------
134 // Helpers shared by the readers and validation
135 // -----------------------------------------------------------------------------
136
137 /** The scalars every schema has, whether or not it declares them. */
138 export const builtInScalarNames: ReadonlyArray<string> = ["String", "Int", "Float", "Boolean", "ID"]
139
140 /** The reason `@deprecated` implies when it has none. */
141 export const defaultDeprecationReason = "No longer supported"
142
143 export const builtInScalar = (name: string): ScalarType => ({ _tag: "ScalarType", name, description: undefined })
144
145 /** Drops locations from a parsed const value. */
146 export const fromAstConstValue = (value: Ast.ConstValue): ConstValue => {
147 switch (value._tag) {
148 case "IntValue":
149 case "FloatValue":
150 case "StringValue":
151 case "EnumValue":
152 return { _tag: value._tag, value: value.value }
153 case "BooleanValue":
154 return { _tag: "BooleanValue", value: value.value }
155 case "NullValue":
156 return { _tag: "NullValue" }
157 case "ListValue":
158 return { _tag: "ListValue", values: value.values.map(fromAstConstValue) }
159 case "ObjectValue":
160 return {
161 _tag: "ObjectValue",
162 fields: value.fields.map((field) => ({ name: field.name.value, value: fromAstConstValue(field.value) }))
163 }
164 }
165 }
166
167 export const fromAstType = (type: Ast.Type): TypeRef => {
168 switch (type._tag) {
169 case "NamedType":
170 return { _tag: "NamedTypeRef", name: type.name.value }
171 case "ListType":
172 return { _tag: "ListTypeRef", ofType: fromAstType(type.type) }
173 case "NonNullType":
174 return { _tag: "NonNullTypeRef", ofType: fromAstType(type.type) as NamedTypeRef | ListTypeRef }
175 }
176 }
177
178 /** The root type an operation of `operation` kind selects from, if the schema configures one. */
179 export const rootTypeName = (schema: Schema, operation: Ast.OperationType): string | undefined => {
180 switch (operation) {
181 case "query":
182 return schema.queryType
183 case "mutation":
184 return schema.mutationType
185 case "subscription":
186 return schema.subscriptionType
187 }
188 }
189
190 /** The name at the bottom of a type reference. */
191 export const namedTypeOf = (type: TypeRef): string =>
192 type._tag === "NamedTypeRef" ? type.name : namedTypeOf(type.ofType)
193
194 /** Prints a type reference in SDL form, e.g. `[Pet!]!`. */
195 export const printTypeRef = (type: TypeRef): string => {
196 switch (type._tag) {
197 case "NamedTypeRef":
198 return type.name
199 case "ListTypeRef":
200 return `[${printTypeRef(type.ofType)}]`
201 case "NonNullTypeRef":
202 return `${printTypeRef(type.ofType)}!`
203 }
204 }