The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Generator from "@effect/graphql-generator/Generator" |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices" |
| 3 | import { assert, describe, it } from "@effect/vitest" |
| 4 | import * as Effect from "effect/Effect" |
| 5 | import * as FileSystem from "effect/FileSystem" |
| 6 | import * as Path from "effect/Path" |
| 7 | import { |
| 8 | assertDeclaredInOrder, |
| 9 | assertNoErrors, |
| 10 | generateIn, |
| 11 | generateTasks, |
| 12 | importGenerated, |
| 13 | member |
| 14 | } from "./utils/generator.ts" |
| 15 | |
| 16 | // `TaskSchedule` is defined before the `TaskCore` it spreads, so source order |
| 17 | // and dependency order disagree. |
| 18 | const fragments = ` |
| 19 | fragment TaskSchedule on Task { |
| 20 | estimate |
| 21 | ...TaskCore |
| 22 | } |
| 23 | |
| 24 | fragment TaskCore on Task { |
| 25 | id |
| 26 | title |
| 27 | } |
| 28 | ` |
| 29 | |
| 30 | const tasks = ` |
| 31 | query Scheduled { |
| 32 | tasks { |
| 33 | ...TaskSchedule |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | query Titled { |
| 38 | tasks { |
| 39 | ...TaskCore |
| 40 | title |
| 41 | done |
| 42 | } |
| 43 | } |
| 44 | ` |
| 45 | |
| 46 | const generate = generateTasks({ "src/fragments.graphql": fragments, "src/tasks.graphql": tasks }).pipe( |
| 47 | Effect.map((generated) => { |
| 48 | assertNoErrors(generated) |
| 49 | return { |
| 50 | generated, |
| 51 | fragments: generated.file("src/fragments.graphql.ts"), |
| 52 | tasks: generated.file("src/tasks.graphql.ts") |
| 53 | } |
| 54 | }) |
| 55 | ) |
| 56 | |
| 57 | describe("Generator per-file output", () => { |
| 58 | it.effect("deletes lists only headered orphans, not handwritten files, live outputs or the shared module", () => |
| 59 | Effect.gen(function*() { |
| 60 | const orphan = |
| 61 | "// Generated by @effect/graphql-generator from orphan.graphql. Do not edit.\nexport const Orphan = 1\n" |
| 62 | const generated = yield* generateIn({ |
| 63 | "schema.graphql": "type Query { hello: String! }", |
| 64 | "src/live.graphql": "query Live { hello }", |
| 65 | "src/live.graphql.ts": "// Generated by @effect/graphql-generator from live.graphql. Do not edit.\n", |
| 66 | "src/orphan.graphql.ts": orphan, |
| 67 | "src/handwritten.graphql.ts": "export const Handwritten = true\n", |
| 68 | "src/shared.graphql.ts": "// Generated by @effect/graphql-generator from schema.graphql. Do not edit.\n" |
| 69 | }, { |
| 70 | schema: "./schema.graphql", |
| 71 | documents: ["src/**/*.graphql"], |
| 72 | shared: "./src/shared.graphql.ts" |
| 73 | }) |
| 74 | assertNoErrors(generated) |
| 75 | const path = yield* Path.Path |
| 76 | assert.deepStrictEqual(generated.result.deletes.map((file) => path.basename(file)), ["orphan.graphql.ts"]) |
| 77 | const fs = yield* FileSystem.FileSystem |
| 78 | assert.strictEqual(yield* fs.readFileString(generated.result.deletes[0]!), orphan) |
| 79 | }).pipe(Effect.provide(NodeServices.layer))) |
| 80 | |
| 81 | it.effect("a shared module, which names no source, is never listed as stale", () => |
| 82 | Effect.gen(function*() { |
| 83 | const generated = yield* generateIn({ |
| 84 | "schema.graphql": "type Query { hello: String! }", |
| 85 | "src/live.graphql": "query Live { hello }", |
| 86 | "src/orphan.graphql.ts": "// Generated by @effect/graphql-generator from orphan.graphql. Do not edit.\n", |
| 87 | "src/other-shared.graphql.ts": "// Generated by @effect/graphql-generator. Do not edit.\n" |
| 88 | }, { schema: "./schema.graphql", documents: ["src/**/*.graphql"] }) |
| 89 | assertNoErrors(generated) |
| 90 | const path = yield* Path.Path |
| 91 | assert.deepStrictEqual(generated.result.deletes.map((file) => path.basename(file)), ["orphan.graphql.ts"]) |
| 92 | }).pipe(Effect.provide(NodeServices.layer))) |
| 93 | |
| 94 | it.effect("an interface without implementations has the possible typenames never", () => |
| 95 | Effect.gen(function*() { |
| 96 | const generated = yield* generateIn({ |
| 97 | "schema.graphql": "interface Lonely { id: ID! } type Query { lonely: Lonely }", |
| 98 | "src/a.graphql": "query A { lonely { id } }" |
| 99 | }, { schema: "./schema.graphql", documents: ["src/*.graphql"] }) |
| 100 | assertNoErrors(generated) |
| 101 | assert.include(generated.file("schema.graphql.ts"), "export type Lonely = never") |
| 102 | }).pipe(Effect.provide(NodeServices.layer))) |
| 103 | |
| 104 | it.effect("schema names that TypeScript reserves for types never become type or class names", () => |
| 105 | Effect.gen(function*() { |
| 106 | const generated = yield* generateIn({ |
| 107 | "schema.graphql": [ |
| 108 | "enum symbol { A B }", |
| 109 | "input any { x: Int }", |
| 110 | "type object { id: ID! }", |
| 111 | "union string = object", |
| 112 | "type Query { things(where: any, s: symbol): [object!] u: string }" |
| 113 | ].join("\n"), |
| 114 | "src/a.graphql": "query Q($w: any, $s: symbol) { things(where: $w, s: $s) { id } u { ... on object { id } } }" |
| 115 | }, { schema: "./schema.graphql", documents: ["src/*.graphql"] }) |
| 116 | assertNoErrors(generated) |
| 117 | const reserved = /\b(?:type|class) (?:any|bigint|boolean|never|number|object|string|symbol|undefined|unknown)\b/ |
| 118 | for (const file of generated.paths) { |
| 119 | assert.deepStrictEqual(generated.file(file).split("\n").filter((line) => reserved.test(line)), [], file) |
| 120 | } |
| 121 | }).pipe(Effect.provide(NodeServices.layer))) |
| 122 | |
| 123 | it.effect("colliding abstract type names have distinct typename aliases and matching references", () => |
| 124 | Effect.gen(function*() { |
| 125 | const generated = yield* generateIn({ |
| 126 | "schema.graphql": [ |
| 127 | "type A { id: ID! }", |
| 128 | "type B { id: ID! }", |
| 129 | "union string = A", |
| 130 | "union string_ = B", |
| 131 | "type Query { a: string! b: string_! }" |
| 132 | ].join("\n"), |
| 133 | "src/a.graphql": "query Q { a { __typename } b { __typename } }" |
| 134 | }, { schema: "./schema.graphql", documents: ["src/*.graphql"] }) |
| 135 | assertNoErrors(generated) |
| 136 | const aliases = Array.from( |
| 137 | generated.file("schema.graphql.ts").matchAll(/export type (\w+) = "([AB])"/g), |
| 138 | (match) => [match[1], match[2]] |
| 139 | ) |
| 140 | assert.deepStrictEqual(aliases.map((alias) => alias[1]), ["A", "B"]) |
| 141 | const references = Array.from( |
| 142 | generated.file("src/a.graphql.ts").matchAll(/GraphQL.otherTypename<Shared\.Typename\.(\w+)>/g), |
| 143 | (match) => match[1] |
| 144 | ) |
| 145 | assert.deepStrictEqual(references, aliases.map((alias) => alias[0])) |
| 146 | assert.notStrictEqual(aliases[0]![0], aliases[1]![0]) |
| 147 | }).pipe(Effect.provide(NodeServices.layer))) |
| 148 | |
| 149 | it.effect("a shared path equal to the schema or a document is an error, and nothing is generated", () => |
| 150 | Effect.gen(function*() { |
| 151 | for (const shared of ["./schema.graphql", "./src/a.graphql"]) { |
| 152 | const generated = yield* generateIn({ |
| 153 | "schema.graphql": "type Query { hello: String! }", |
| 154 | "src/a.graphql": "query A { hello }" |
| 155 | }, { schema: "./schema.graphql", documents: ["src/*.graphql"], shared }) |
| 156 | assert.strictEqual(generated.result.diagnostics.filter((d) => d.severity === "error").length, 1, shared) |
| 157 | assert.deepStrictEqual(generated.result.files, [], shared) |
| 158 | } |
| 159 | }).pipe(Effect.provide(NodeServices.layer))) |
| 160 | |
| 161 | it.effect("a dangling symlink under a documents root is skipped", () => |
| 162 | Effect.gen(function*() { |
| 163 | const fs = yield* FileSystem.FileSystem |
| 164 | const path = yield* Path.Path |
| 165 | const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "graphql-generator-" }) |
| 166 | yield* fs.makeDirectory(path.join(cwd, "src")) |
| 167 | yield* fs.writeFileString(path.join(cwd, "schema.graphql"), "type Query { hello: String! }") |
| 168 | yield* fs.writeFileString(path.join(cwd, "src", "a.graphql"), "query A { hello }") |
| 169 | yield* fs.symlink(path.join(cwd, "missing"), path.join(cwd, "src", "dangling")) |
| 170 | const result = yield* Generator.generate({ schema: "./schema.graphql", documents: ["src/**/*.graphql"] }, { cwd }) |
| 171 | assert.deepStrictEqual(result.diagnostics, []) |
| 172 | assert.include(result.files.map((file) => path.basename(file.path)), "a.graphql.ts") |
| 173 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) |
| 174 | |
| 175 | it.effect("an absolute documents pattern is a ConfigError", () => |
| 176 | Effect.gen(function*() { |
| 177 | const error = yield* generateIn({ |
| 178 | "schema.graphql": "type Query { hello: String! }", |
| 179 | "src/a.graphql": "query A { hello }" |
| 180 | }, { schema: "./schema.graphql", documents: ["/abs/src/*.graphql"] }).pipe(Effect.flip) |
| 181 | assert.strictEqual(error._tag, "ConfigError") |
| 182 | }).pipe(Effect.provide(NodeServices.layer))) |
| 183 | |
| 184 | it.effect(".gql documents name their group without the extension and are cleaned up as stale", () => |
| 185 | Effect.gen(function*() { |
| 186 | const generated = yield* generateIn({ |
| 187 | "schema.graphql": "type Query { hello: String! }", |
| 188 | "src/other.gql": "query Other { hello }", |
| 189 | "src/gone.gql.ts": "// Generated by @effect/graphql-generator from gone.gql. Do not edit.\n" |
| 190 | }, { schema: "./schema.graphql", documents: ["src/*.gql"] }) |
| 191 | assertNoErrors(generated) |
| 192 | assert.match(generated.file("src/other.gql.ts"), /^export const OtherGraphQLGroup\b/m) |
| 193 | const path = yield* Path.Path |
| 194 | assert.deepStrictEqual(generated.result.deletes.map((file) => path.basename(file)), ["gone.gql.ts"]) |
| 195 | }).pipe(Effect.provide(NodeServices.layer))) |
| 196 | |
| 197 | it.effect("fragments are declared after the fragments they spread", () => |
| 198 | Effect.gen(function*() { |
| 199 | const { fragments } = yield* generate |
| 200 | assertDeclaredInOrder(fragments, ["TaskCore", "TaskSchedule"]) |
| 201 | }).pipe(Effect.provide(NodeServices.layer))) |
| 202 | |
| 203 | it.effect("a fragment whose keys overlap its siblings is written out inline", () => |
| 204 | Effect.gen(function*() { |
| 205 | const { tasks } = yield* generate |
| 206 | assert.notInclude(tasks, "...TaskCore.fields") |
| 207 | assert.strictEqual(member(tasks, "id"), "Shared.ID") |
| 208 | assert.strictEqual(member(tasks, "title"), "Schema.String") |
| 209 | }).pipe(Effect.provide(NodeServices.layer))) |
| 210 | |
| 211 | it.effect("documents append every fragment they use, including through other fragments, in order of first use", () => |
| 212 | Effect.gen(function*() { |
| 213 | const { generated } = yield* generate |
| 214 | const module = yield* importGenerated(generated, "src/tasks.graphql.ts") |
| 215 | assert.strictEqual( |
| 216 | module.Scheduled.document, |
| 217 | "query Scheduled{tasks{...TaskSchedule}}fragment TaskSchedule on Task{estimate...TaskCore}fragment TaskCore on Task{id title}" |
| 218 | ) |
| 219 | }).pipe(Effect.provide(NodeServices.layer))) |
| 220 | }) |