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
| 1 | /** |
| 2 | * Builds a {@link SchemaModel.Schema} directly from introspection JSON, |
| 3 | * without printing SDL and re-parsing it. |
| 4 | * |
| 5 | * @internal |
| 6 | */ |
| 7 | import * as Result from "effect/Result" |
| 8 | import { catchDiagnostic, type Diagnostic, make, type Source } from "./Diagnostic.ts" |
| 9 | import { parseConstValue } from "./Parser.ts" |
| 10 | import * as SchemaModel from "./SchemaModel.ts" |
| 11 | |
| 12 | /** |
| 13 | * Reads introspection JSON from `source.body`, accepting both `{ __schema }` |
| 14 | * and `{ data: { __schema } }`. `defaultValue` strings go through |
| 15 | * `Parser.parseConstValue`. The newer keys `isOneOf` and input value |
| 16 | * `isDeprecated` / `deprecationReason` are optional and read as `false` / |
| 17 | * absent when missing. `isDeprecated: true` with a `null` reason reads as |
| 18 | * `"No longer supported"`. |
| 19 | * |
| 20 | * Fails with a diagnostic for `source.path` when the body is not JSON, has |
| 21 | * neither shape, or a `defaultValue` does not parse. |
| 22 | */ |
| 23 | export const read = (source: Source): Result.Result<SchemaModel.Schema, Diagnostic> => |
| 24 | catchDiagnostic(() => new Reader(source).read()) |
| 25 | |
| 26 | type JsonObject = { readonly [key: string]: unknown } |
| 27 | |
| 28 | const isObject = (value: unknown): value is JsonObject => |
| 29 | typeof value === "object" && value !== null && !Array.isArray(value) |
| 30 | |
| 31 | class Reader { |
| 32 | readonly source: Source |
| 33 | |
| 34 | constructor(source: Source) { |
| 35 | this.source = source |
| 36 | } |
| 37 | |
| 38 | /** Shape errors carry no position: the JSON has been parsed and offsets are gone. */ |
| 39 | fail(message: string, offset = 0): never { |
| 40 | throw make(this.source, offset, message) |
| 41 | } |
| 42 | |
| 43 | expected(path: string, what: string): never { |
| 44 | return this.fail(`Invalid introspection result at ${path}: expected ${what}.`) |
| 45 | } |
| 46 | |
| 47 | read(): SchemaModel.Schema { |
| 48 | let json: unknown |
| 49 | try { |
| 50 | json = JSON.parse(this.source.body) |
| 51 | } catch (error) { |
| 52 | const message = error instanceof Error ? error.message : String(error) |
| 53 | const position = /at position (\d+)/.exec(message) |
| 54 | this.fail(`Invalid JSON: ${message}`, position === null ? 0 : Number(position[1])) |
| 55 | } |
| 56 | const schema = isObject(json) && isObject(json.__schema) |
| 57 | ? json.__schema |
| 58 | : isObject(json) && isObject(json.data) && isObject(json.data.__schema) |
| 59 | ? json.data.__schema |
| 60 | : this.fail("Expected an introspection result shaped as { __schema } or { data: { __schema } }.") |
| 61 | |
| 62 | const types = new Map<string, SchemaModel.NamedType>() |
| 63 | this.objects(schema, "types", "__schema", (type, path) => { |
| 64 | const name = this.string(type, "name", path) |
| 65 | if (name.startsWith("__")) return |
| 66 | types.set( |
| 67 | name, |
| 68 | SchemaModel.builtInScalarNames.includes(name) |
| 69 | ? SchemaModel.builtInScalar(name) |
| 70 | : this.namedType(type, name, path) |
| 71 | ) |
| 72 | }) |
| 73 | for (const name of SchemaModel.builtInScalarNames) { |
| 74 | if (!types.has(name)) types.set(name, SchemaModel.builtInScalar(name)) |
| 75 | } |
| 76 | |
| 77 | return { |
| 78 | queryType: this.rootName(schema, "queryType") ?? this.expected("__schema.queryType", "an object"), |
| 79 | mutationType: this.rootName(schema, "mutationType"), |
| 80 | subscriptionType: this.rootName(schema, "subscriptionType"), |
| 81 | types |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | namedType(type: JsonObject, name: string, path: string): SchemaModel.NamedType { |
| 86 | const description = this.optionalString(type, "description", path) |
| 87 | const kind = this.string(type, "kind", path) |
| 88 | switch (kind) { |
| 89 | case "SCALAR": |
| 90 | return { _tag: "ScalarType", name, description } |
| 91 | |
| 92 | case "OBJECT": |
| 93 | return { |
| 94 | _tag: "ObjectType", |
| 95 | name, |
| 96 | description, |
| 97 | interfaces: this.typeNames(type, "interfaces", path), |
| 98 | fields: this.fields(type, path) |
| 99 | } |
| 100 | case "INTERFACE": |
| 101 | return { |
| 102 | _tag: "InterfaceType", |
| 103 | name, |
| 104 | description, |
| 105 | interfaces: type.interfaces == null ? [] : this.typeNames(type, "interfaces", path), |
| 106 | fields: this.fields(type, path), |
| 107 | possibleTypes: this.typeNames(type, "possibleTypes", path).sort() |
| 108 | } |
| 109 | case "UNION": |
| 110 | return { _tag: "UnionType", name, description, possibleTypes: this.typeNames(type, "possibleTypes", path) } |
| 111 | case "ENUM": |
| 112 | return { |
| 113 | _tag: "EnumType", |
| 114 | name, |
| 115 | description, |
| 116 | values: this.objects(type, "enumValues", path, (value, valuePath) => ({ |
| 117 | name: this.string(value, "name", valuePath), |
| 118 | description: this.optionalString(value, "description", valuePath), |
| 119 | deprecationReason: this.deprecationReason(value, valuePath) |
| 120 | })) |
| 121 | } |
| 122 | case "INPUT_OBJECT": |
| 123 | return { |
| 124 | _tag: "InputObjectType", |
| 125 | name, |
| 126 | description, |
| 127 | oneOf: this.optionalBoolean(type, "isOneOf", path) ?? false, |
| 128 | fields: this.inputValues(type, "inputFields", path) |
| 129 | } |
| 130 | default: |
| 131 | return this.expected(`${path}.kind`, "a named type kind") |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | fields(type: JsonObject, path: string): ReadonlyArray<SchemaModel.Field> { |
| 136 | return this.objects(type, "fields", path, (field, fieldPath) => ({ |
| 137 | name: this.string(field, "name", fieldPath), |
| 138 | description: this.optionalString(field, "description", fieldPath), |
| 139 | arguments: this.inputValues(field, "args", fieldPath), |
| 140 | type: this.typeRef(field.type, `${fieldPath}.type`), |
| 141 | deprecationReason: this.deprecationReason(field, fieldPath) |
| 142 | })) |
| 143 | } |
| 144 | |
| 145 | inputValues(owner: JsonObject, key: string, path: string): ReadonlyArray<SchemaModel.InputValue> { |
| 146 | return this.objects(owner, key, path, (inputValue, valuePath) => { |
| 147 | const defaultValue = this.optionalString(inputValue, "defaultValue", valuePath) |
| 148 | return { |
| 149 | name: this.string(inputValue, "name", valuePath), |
| 150 | description: this.optionalString(inputValue, "description", valuePath), |
| 151 | type: this.typeRef(inputValue.type, `${valuePath}.type`), |
| 152 | defaultValue: defaultValue === undefined |
| 153 | ? undefined |
| 154 | : this.constValue(defaultValue, `${valuePath}.defaultValue`), |
| 155 | deprecationReason: this.deprecationReason(inputValue, valuePath) |
| 156 | } |
| 157 | }) |
| 158 | } |
| 159 | |
| 160 | constValue(body: string, path: string): SchemaModel.ConstValue { |
| 161 | const result = parseConstValue({ path: this.source.path, body }) |
| 162 | if (Result.isFailure(result)) { |
| 163 | return this.fail(`Invalid default value at ${path} (${JSON.stringify(body)}): ${result.failure.message}`) |
| 164 | } |
| 165 | return SchemaModel.fromAstConstValue(result.success) |
| 166 | } |
| 167 | |
| 168 | typeRef(value: unknown, path: string): SchemaModel.TypeRef { |
| 169 | const ref = this.object(value, path) |
| 170 | const kind = this.string(ref, "kind", path) |
| 171 | switch (kind) { |
| 172 | case "NON_NULL": { |
| 173 | const ofType = this.typeRef(ref.ofType, `${path}.ofType`) |
| 174 | if (ofType._tag === "NonNullTypeRef") this.expected(`${path}.ofType`, "a nullable type") |
| 175 | return { _tag: "NonNullTypeRef", ofType: ofType as SchemaModel.NamedTypeRef | SchemaModel.ListTypeRef } |
| 176 | } |
| 177 | case "LIST": |
| 178 | return { _tag: "ListTypeRef", ofType: this.typeRef(ref.ofType, `${path}.ofType`) } |
| 179 | default: |
| 180 | return { _tag: "NamedTypeRef", name: this.string(ref, "name", path) } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | typeNames(owner: JsonObject, key: string, path: string): Array<string> { |
| 185 | return this.objects(owner, key, path, (type, typePath) => this.string(type, "name", typePath)) |
| 186 | } |
| 187 | |
| 188 | rootName(schema: JsonObject, key: string): string | undefined { |
| 189 | const root = schema[key] |
| 190 | return root == null ? undefined : this.string(this.object(root, `__schema.${key}`), "name", `__schema.${key}`) |
| 191 | } |
| 192 | |
| 193 | deprecationReason(owner: JsonObject, path: string): string | undefined { |
| 194 | if (this.optionalBoolean(owner, "isDeprecated", path) !== true) return undefined |
| 195 | return this.optionalString(owner, "deprecationReason", path) ?? SchemaModel.defaultDeprecationReason |
| 196 | } |
| 197 | |
| 198 | // JSON accessors. `null` and a missing key both read as absent. |
| 199 | |
| 200 | object(value: unknown, path: string): JsonObject { |
| 201 | return isObject(value) ? value : this.expected(path, "an object") |
| 202 | } |
| 203 | |
| 204 | array(owner: JsonObject, key: string, path: string): ReadonlyArray<unknown> { |
| 205 | const value = owner[key] |
| 206 | return Array.isArray(value) ? value : this.expected(`${path}.${key}`, "an array") |
| 207 | } |
| 208 | |
| 209 | /** Maps the array at `owner[key]`, whose elements must be objects, passing each element's path. */ |
| 210 | objects<A>(owner: JsonObject, key: string, path: string, f: (value: JsonObject, path: string) => A): Array<A> { |
| 211 | return this.array(owner, key, path).map((value, i) => { |
| 212 | const itemPath = `${path}.${key}[${i}]` |
| 213 | return f(this.object(value, itemPath), itemPath) |
| 214 | }) |
| 215 | } |
| 216 | |
| 217 | string(owner: JsonObject, key: string, path: string): string { |
| 218 | const value = owner[key] |
| 219 | return typeof value === "string" ? value : this.expected(`${path}.${key}`, "a string") |
| 220 | } |
| 221 | |
| 222 | optionalString(owner: JsonObject, key: string, path: string): string | undefined { |
| 223 | const value = owner[key] |
| 224 | if (value == null) return undefined |
| 225 | return typeof value === "string" ? value : this.expected(`${path}.${key}`, "a string or null") |
| 226 | } |
| 227 | |
| 228 | optionalBoolean(owner: JsonObject, key: string, path: string): boolean | undefined { |
| 229 | const value = owner[key] |
| 230 | if (value == null) return undefined |
| 231 | return typeof value === "boolean" ? value : this.expected(`${path}.${key}`, "a boolean or null") |
| 232 | } |
| 233 | } |