The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import * as Effect from "effect/Effect" |
| 4 | import { |
| 5 | assertNoErrors, |
| 6 | declaration, |
| 7 | docOf, |
| 8 | generateIn, |
| 9 | generateTasks, |
| 10 | member, |
| 11 | taskConfig, |
| 12 | warnings |
| 13 | } from "./utils/generator.ts" |
| 14 | |
| 15 | describe("Generator mapping", () => { |
| 16 | it.effect("DateTime and URI have overridable defaults while other custom scalars stay Json", () => |
| 17 | Effect.gen(function*() { |
| 18 | const inputs = { |
| 19 | "schema.graphql": ` |
| 20 | scalar DateTime |
| 21 | scalar URI |
| 22 | scalar datetime |
| 23 | scalar uri |
| 24 | scalar GitObjectID |
| 25 | type Query { timestamp: DateTime! url: URI! lowerDate: datetime! lowerUri: uri! oid: GitObjectID! } |
| 26 | `, |
| 27 | "src/ops.graphql": "query Scalars { timestamp url lowerDate lowerUri oid }" |
| 28 | } |
| 29 | const config = { schema: "./schema.graphql", documents: ["src/*.graphql"] } |
| 30 | const defaults = yield* generateIn(inputs, config) |
| 31 | const overridden = yield* generateIn(inputs, { |
| 32 | ...config, |
| 33 | scalars: { DateTime: "./scalars.ts#Timestamp", URI: "./scalars.ts#Url" } |
| 34 | }) |
| 35 | for (const generated of [defaults, overridden]) { |
| 36 | assertNoErrors(generated) |
| 37 | const shared = generated.file("schema.graphql.ts") |
| 38 | for (const name of ["GitObjectID", "datetime", "uri"]) { |
| 39 | assert.strictEqual(declaration(shared, name), `export const ${name} = Schema.Json`) |
| 40 | } |
| 41 | } |
| 42 | const overrideShared = overridden.file("schema.graphql.ts") |
| 43 | assert.match(declaration(overrideShared, "DateTime"), /^export const DateTime = \w+\.Timestamp$/) |
| 44 | assert.match(declaration(overrideShared, "URI"), /^export const URI = \w+\.Url$/) |
| 45 | const shared = defaults.file("schema.graphql.ts") |
| 46 | assert.deepStrictEqual({ |
| 47 | DateTime: declaration(shared, "DateTime"), |
| 48 | URI: declaration(shared, "URI"), |
| 49 | warnings: [defaults, overridden].map((generated) => warnings(generated).map(({ message }) => message)) |
| 50 | }, { |
| 51 | DateTime: "export const DateTime = Schema.DateTimeUtcFromString", |
| 52 | URI: "export const URI = Schema.String", |
| 53 | warnings: [ |
| 54 | ["Custom scalars without a mapping decode as Schema.Json: GitObjectID, datetime, uri. Map each one to a codec with `scalars` in the config."], |
| 55 | ["Custom scalars without a mapping decode as Schema.Json: GitObjectID, datetime, uri. Map each one to a codec with `scalars` in the config."] |
| 56 | ] |
| 57 | }) |
| 58 | }).pipe(Effect.provide(NodeServices.layer))) |
| 59 | |
| 60 | it.effect("a non-null variable with a default is optional(T) and keeps the default as @default", () => |
| 61 | Effect.gen(function*() { |
| 62 | const generated = yield* generateTasks({ |
| 63 | "src/ops.graphql": "query Tasks($first: Int! = 20) { tasks(first: $first) { id } }" |
| 64 | }) |
| 65 | assertNoErrors(generated) |
| 66 | const ops = generated.file("src/ops.graphql.ts") |
| 67 | assert.strictEqual(member(ops, "first"), "Schema.optional(Shared.Int)") |
| 68 | assert.deepStrictEqual(docOf(ops, "first"), ["@default 20"]) |
| 69 | }).pipe(Effect.provide(NodeServices.layer))) |
| 70 | |
| 71 | it.effect("a recursive input object is a Codec, with an Encoded type when a field may transform", () => |
| 72 | Effect.gen(function*() { |
| 73 | const generated = yield* generateIn({ |
| 74 | "schema.graphql": ` |
| 75 | scalar Stamp |
| 76 | input Tree { child: Tree } |
| 77 | input Dated { at: Stamp next: Dated } |
| 78 | input Unused { self: Unused } |
| 79 | type Query { tree(t: Tree, d: Dated, u: Unused): Int } |
| 80 | `, |
| 81 | "src/ops.graphql": "query Q($t: Tree, $d: Dated) { tree(t: $t, d: $d) }" |
| 82 | }, { schema: "./schema.graphql", documents: ["src/*.graphql"], scalars: { Stamp: "./scalars.ts#Stamp" } }) |
| 83 | assertNoErrors(generated) |
| 84 | const shared = generated.file("schema.graphql.ts") |
| 85 | assert.deepStrictEqual({ |
| 86 | Tree: declaration(shared, "Tree"), |
| 87 | Dated: declaration(shared, "Dated"), |
| 88 | unused: shared.includes("Unused") |
| 89 | }, { |
| 90 | Tree: "export const Tree: Schema.Codec<Tree> = Schema.Struct({", |
| 91 | Dated: "export const Dated: Schema.Codec<Dated, DatedEncoded> = Schema.Struct({", |
| 92 | unused: false |
| 93 | }) |
| 94 | assert.include(shared, "export interface DatedEncoded {\n readonly at?: typeof Stamp.Encoded | null | undefined") |
| 95 | }).pipe(Effect.provide(NodeServices.layer))) |
| 96 | |
| 97 | it.effect("a scalars entry overrides a built-in", () => |
| 98 | Effect.gen(function*() { |
| 99 | const generated = yield* generateTasks( |
| 100 | { "src/ops.graphql": "query Tasks { tasks { id } }" }, |
| 101 | { ...taskConfig, scalars: { ID: "./scalars.ts#NodeId" } } |
| 102 | ) |
| 103 | assertNoErrors(generated) |
| 104 | assert.match(declaration(generated.file("schema/app.graphql.ts"), "ID"), /^export const ID = \w+\.NodeId$/) |
| 105 | }).pipe(Effect.provide(NodeServices.layer))) |
| 106 | |
| 107 | it.effect("the shared module defaults to the schema's name and directory", () => |
| 108 | Effect.gen(function*() { |
| 109 | const generated = yield* generateTasks({ "src/ops.graphql": "query Tasks { tasks { id } }" }) |
| 110 | assertNoErrors(generated) |
| 111 | assert.deepStrictEqual(generated.paths, ["schema/app.graphql.ts", "src/ops.graphql.ts"]) |
| 112 | assert.match(generated.file("src/ops.graphql.ts"), /^import \* as Shared from "\.\.\/schema\/app\.graphql\.ts"$/m) |
| 113 | }).pipe(Effect.provide(NodeServices.layer))) |
| 114 | |
| 115 | it.effect("a relative scalar module with an extension other than .ts or .js is imported as written", () => |
| 116 | Effect.gen(function*() { |
| 117 | const generated = yield* generateTasks( |
| 118 | { "src/ops.graphql": "query Tasks { tasks { due } }" }, |
| 119 | { ...taskConfig, scalars: { Timestamp: "./scalars.mjs#Timestamp" } } |
| 120 | ) |
| 121 | assertNoErrors(generated) |
| 122 | assert.match(generated.file("schema/app.graphql.ts"), /^import \* as \w+ from "\.\.\/scalars\.mjs"$/m) |
| 123 | }).pipe(Effect.provide(NodeServices.layer))) |
| 124 | |
| 125 | it.effect("importExtension applies to generated and relative scalar modules, not bare specifiers", () => |
| 126 | Effect.gen(function*() { |
| 127 | const generated = yield* generateTasks( |
| 128 | { "src/ops.graphql": "query Tasks { tasks { due bodyHTML } }" }, |
| 129 | { ...taskConfig, importExtension: ".js", scalars: { ...taskConfig.scalars, HTML: "@acme/scalars#HTML" } } |
| 130 | ) |
| 131 | assertNoErrors(generated) |
| 132 | const shared = generated.file("schema/app.graphql.ts") |
| 133 | assert.match(generated.file("src/ops.graphql.ts"), /^import \* as Shared from "\.\.\/schema\/app\.graphql\.js"$/m) |
| 134 | assert.match(shared, /^import \* as \w+ from "\.\.\/scalars\.js"$/m) |
| 135 | assert.match(shared, /^import \* as \w+ from "@acme\/scalars"$/m) |
| 136 | }).pipe(Effect.provide(NodeServices.layer))) |
| 137 | }) |