The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Token-minimal printer for executable documents; SDL is not supported. |
| 3 | * |
| 4 | * Preserves definition order and number text. Strings use regular quoted |
| 5 | * syntax. Spaces separate adjacent non-punctuator tokens; there is no trailing |
| 6 | * newline. Anonymous queries without variables or directives use shorthand. |
| 7 | * |
| 8 | * Descriptions are omitted because they have no execution meaning and are |
| 9 | * rejected by servers using the 2021 grammar. |
| 10 | * |
| 11 | * {@link template} prints an operation as an `effect/graphql` |
| 12 | * `DocumentTemplate` instead: the same text, split around the names a batch |
| 13 | * renames. |
| 14 | * |
| 15 | * @internal |
| 16 | */ |
| 17 | import type { GraphQL } from "effect/graphql" |
| 18 | import type * as Ast from "./Ast.ts" |
| 19 | |
| 20 | /** |
| 21 | * Prints an executable document compactly. Throws on type-system definitions |
| 22 | * and extensions, which are outside the contract. |
| 23 | */ |
| 24 | export const print = (document: Ast.Document): string => concatAll(document.definitions.map(printDefinition)) |
| 25 | |
| 26 | /** |
| 27 | * Surrounds a name printed as a template placeholder. Names can't contain it |
| 28 | * and strings print it escaped, so splitting on it finds exactly the names. |
| 29 | */ |
| 30 | const marker = "\u0000" |
| 31 | |
| 32 | /** Which names print between markers. */ |
| 33 | interface Marks { |
| 34 | readonly variables: boolean |
| 35 | readonly fragments: ReadonlySet<string> |
| 36 | } |
| 37 | |
| 38 | const unmarked: Marks = { variables: false, fragments: new Set() } |
| 39 | |
| 40 | const markName = (name: string): string => `${marker}${name}${marker}` |
| 41 | |
| 42 | /** |
| 43 | * Joins two printed fragments, adding a space only where two non-punctuators |
| 44 | * would otherwise touch. A marker is ignored, so marked text joins exactly as |
| 45 | * it would unmarked. |
| 46 | */ |
| 47 | const concat = (left: string, right: string): string => |
| 48 | /[\w"-]$/.test(left.endsWith(marker) ? left.slice(0, -1) : left) && |
| 49 | /^[\w"-]/.test(right.startsWith(marker) ? right.slice(1) : right) |
| 50 | ? `${left} ${right}` |
| 51 | : left + right |
| 52 | |
| 53 | const concatAll = (parts: ReadonlyArray<string>): string => parts.reduce(concat, "") |
| 54 | |
| 55 | const printDefinition = (definition: Ast.Definition): string => { |
| 56 | switch (definition._tag) { |
| 57 | case "OperationDefinition": |
| 58 | return printOperation(definition) |
| 59 | case "FragmentDefinition": |
| 60 | return concatAll(["fragment", definition.name.value, printFragmentBody(definition, unmarked)]) |
| 61 | default: |
| 62 | throw new Error( |
| 63 | `@effect/graphql-generator: cannot print a ${definition._tag}; only executable documents are printed` |
| 64 | ) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** A fragment definition from its type condition on. */ |
| 69 | const printFragmentBody = (definition: Ast.FragmentDefinition, marks: Marks): string => |
| 70 | concatAll([ |
| 71 | "on", |
| 72 | definition.typeCondition.name.value, |
| 73 | printDirectives(definition.directives, marks), |
| 74 | printSelectionSet(definition.selectionSet, marks) |
| 75 | ]) |
| 76 | |
| 77 | const printOperation = (operation: Ast.OperationDefinition): string => { |
| 78 | const selectionSet = printSelectionSet(operation.selectionSet, unmarked) |
| 79 | if ( |
| 80 | operation.operation === "query" && |
| 81 | operation.name === undefined && |
| 82 | operation.variableDefinitions.length === 0 && |
| 83 | operation.directives.length === 0 |
| 84 | ) { |
| 85 | return selectionSet |
| 86 | } |
| 87 | return concatAll([ |
| 88 | operation.operation, |
| 89 | operation.name === undefined ? "" : operation.name.value, |
| 90 | operation.variableDefinitions.length === 0 |
| 91 | ? "" |
| 92 | : `(${ |
| 93 | concatAll( |
| 94 | operation.variableDefinitions.map((definition) => |
| 95 | `$${definition.variable.name.value}:${printVariableBody(definition)}` |
| 96 | ) |
| 97 | ) |
| 98 | })`, |
| 99 | printDirectives(operation.directives, unmarked), |
| 100 | selectionSet |
| 101 | ]) |
| 102 | } |
| 103 | |
| 104 | /** A variable definition after `$name:`. Default values and these directives are constant. */ |
| 105 | const printVariableBody = (definition: Ast.VariableDefinition): string => |
| 106 | concatAll([ |
| 107 | printType(definition.type), |
| 108 | definition.defaultValue === undefined ? "" : `=${printValue(definition.defaultValue, unmarked)}`, |
| 109 | printDirectives(definition.directives, unmarked) |
| 110 | ]) |
| 111 | |
| 112 | const printType = (type: Ast.Type): string => { |
| 113 | switch (type._tag) { |
| 114 | case "NamedType": |
| 115 | return type.name.value |
| 116 | case "ListType": |
| 117 | return `[${printType(type.type)}]` |
| 118 | case "NonNullType": |
| 119 | return `${printType(type.type)}!` |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | const printSelectionSet = (selectionSet: Ast.SelectionSet, marks: Marks): string => |
| 124 | `{${concatAll(selectionSet.selections.map((selection) => printSelection(selection, marks)))}}` |
| 125 | |
| 126 | const printSelection = (selection: Ast.Selection, marks: Marks): string => { |
| 127 | switch (selection._tag) { |
| 128 | case "Field": |
| 129 | return concatAll([ |
| 130 | selection.alias === undefined ? selection.name.value : `${selection.alias.value}:${selection.name.value}`, |
| 131 | printFieldBody(selection, marks) |
| 132 | ]) |
| 133 | case "FragmentSpread": |
| 134 | return concatAll([ |
| 135 | `...${marks.fragments.has(selection.name.value) ? markName(selection.name.value) : selection.name.value}`, |
| 136 | printDirectives(selection.directives, marks) |
| 137 | ]) |
| 138 | case "InlineFragment": |
| 139 | return concatAll([ |
| 140 | "...", |
| 141 | selection.typeCondition === undefined ? "" : `on ${selection.typeCondition.name.value}`, |
| 142 | printDirectives(selection.directives, marks), |
| 143 | printSelectionSet(selection.selectionSet, marks) |
| 144 | ]) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** A field after its alias and name. */ |
| 149 | const printFieldBody = (field: Ast.Field, marks: Marks): string => |
| 150 | concatAll([ |
| 151 | printArguments(field.arguments, marks), |
| 152 | printDirectives(field.directives, marks), |
| 153 | field.selectionSet === undefined ? "" : printSelectionSet(field.selectionSet, marks) |
| 154 | ]) |
| 155 | |
| 156 | const printArguments = (args: ReadonlyArray<Ast.Argument>, marks: Marks): string => |
| 157 | args.length === 0 |
| 158 | ? "" |
| 159 | : `(${concatAll(args.map((argument) => `${argument.name.value}:${printValue(argument.value, marks)}`))})` |
| 160 | |
| 161 | const printDirectives = (directives: ReadonlyArray<Ast.Directive>, marks: Marks): string => |
| 162 | concatAll(directives.map((directive) => `@${directive.name.value}${printArguments(directive.arguments, marks)}`)) |
| 163 | |
| 164 | const printValue = (value: Ast.Value, marks: Marks): string => { |
| 165 | switch (value._tag) { |
| 166 | case "Variable": |
| 167 | return `$${marks.variables ? markName(value.name.value) : value.name.value}` |
| 168 | case "IntValue": |
| 169 | case "FloatValue": |
| 170 | case "EnumValue": |
| 171 | return value.value |
| 172 | case "StringValue": |
| 173 | return printString(value.value) |
| 174 | case "BooleanValue": |
| 175 | return value.value ? "true" : "false" |
| 176 | case "NullValue": |
| 177 | return "null" |
| 178 | case "ListValue": |
| 179 | return `[${concatAll(value.values.map((item) => printValue(item, marks)))}]` |
| 180 | case "ObjectValue": |
| 181 | return `{${concatAll(value.fields.map((field) => `${field.name.value}:${printValue(field.value, marks)}`))}}` |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | const escapes: ReadonlyMap<string, string> = new Map([ |
| 186 | ["\"", "\\\""], |
| 187 | ["\\", "\\\\"], |
| 188 | ["\b", "\\b"], |
| 189 | ["\f", "\\f"], |
| 190 | ["\n", "\\n"], |
| 191 | ["\r", "\\r"], |
| 192 | ["\t", "\\t"] |
| 193 | ]) |
| 194 | |
| 195 | /** Prints a string value as a regular GraphQL string literal. */ |
| 196 | const printString = (value: string): string => { |
| 197 | let out = "\"" |
| 198 | for (let i = 0; i < value.length; i++) { |
| 199 | const char = value[i]! |
| 200 | const code = value.charCodeAt(i) |
| 201 | out += escapes.get(char) ?? |
| 202 | (code < 0x0020 || code === 0x007f ? `\\u${code.toString(16).toUpperCase().padStart(4, "0")}` : char) |
| 203 | } |
| 204 | return out + "\"" |
| 205 | } |
| 206 | |
| 207 | /** Splits marked text into template parts: literal text at even indices, names at odd ones. */ |
| 208 | const parts = (marked: string): ReadonlyArray<string> => marked.split(marker) |
| 209 | |
| 210 | /** |
| 211 | * Prints an operation and the fragments that follow it as a template. |
| 212 | * Joining the template gives the same text as {@link print}. |
| 213 | * |
| 214 | * Variable references are names to rename, and so are spreads of per-slice |
| 215 | * fragments: those that reference a variable, directly or through a fragment |
| 216 | * they spread. Spreads of shared fragments stay literal. |
| 217 | */ |
| 218 | export const template = ( |
| 219 | operation: Ast.OperationDefinition, |
| 220 | fragments: ReadonlyArray<Ast.FragmentDefinition> |
| 221 | ): GraphQL.DocumentTemplate => { |
| 222 | const perSlice = new Set<string>() |
| 223 | const marks: Marks = { variables: true, fragments: perSlice } |
| 224 | let changed = true |
| 225 | while (changed) { |
| 226 | changed = false |
| 227 | for (const fragment of fragments) { |
| 228 | if (!perSlice.has(fragment.name.value) && printFragmentBody(fragment, marks).includes(marker)) { |
| 229 | perSlice.add(fragment.name.value) |
| 230 | changed = true |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | const directives = printDirectives(operation.directives, marks) |
| 235 | return { |
| 236 | variables: operation.variableDefinitions.map((definition) => ({ |
| 237 | name: definition.variable.name.value, |
| 238 | body: printVariableBody(definition) |
| 239 | })), |
| 240 | ...(directives === "" ? {} : { directives: parts(directives) }), |
| 241 | selections: operation.selectionSet.selections.map((selection) => |
| 242 | selection._tag === "Field" |
| 243 | ? { |
| 244 | ...(selection.alias === undefined ? {} : { alias: selection.alias.value }), |
| 245 | name: selection.name.value, |
| 246 | body: parts(printFieldBody(selection, marks)) |
| 247 | } |
| 248 | : { body: parts(printSelection(selection, marks)) } |
| 249 | ), |
| 250 | fragments: fragments.map((fragment) => ({ |
| 251 | name: fragment.name.value, |
| 252 | body: parts(printFragmentBody(fragment, marks)) |
| 253 | })) |
| 254 | } |
| 255 | } |