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 | /** Generated interface and union selections decode through their result schemas. */ |
| 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 Schema from "effect/Schema" |
| 6 | import { assertNoErrors, generateIn, importGenerated } from "./utils/generator.ts" |
| 7 | |
| 8 | const decoderFor = Effect.fnUntraced(function*(schema: string, document: string) { |
| 9 | const generated = yield* generateIn( |
| 10 | { "schema.graphql": schema, "src/ops.graphql": document }, |
| 11 | { schema: "./schema.graphql", documents: ["src/*.graphql"] } |
| 12 | ) |
| 13 | assertNoErrors(generated) |
| 14 | const ops = yield* importGenerated(generated, "src/ops.graphql.ts") |
| 15 | return Schema.decodeUnknownSync(ops.Q.result) |
| 16 | }) |
| 17 | |
| 18 | describe("Generator abstract members", () => { |
| 19 | it.effect("a fragment on the only possible type of an interface keeps its own member", () => |
| 20 | Effect.gen(function*() { |
| 21 | const decode = yield* decoderFor( |
| 22 | "interface Only { id: ID! }\ntype User implements Only { id: ID! name: String! }\ntype Query { only: Only }", |
| 23 | "query Q { only { ... on User { name } } }" |
| 24 | ) |
| 25 | const user = { only: { __typename: "User", name: "Ann" } } |
| 26 | assert.deepStrictEqual(decode(user), user) |
| 27 | assert.throws(() => decode({ only: { __typename: "User" } })) |
| 28 | }).pipe(Effect.provide(NodeServices.layer))) |
| 29 | |
| 30 | it.effect("an interface fragment covering every member of a union generates and decodes", () => |
| 31 | Effect.gen(function*() { |
| 32 | const decode = yield* decoderFor( |
| 33 | `interface Named { name: String! } |
| 34 | union Result = User | Bot |
| 35 | type User implements Named { id: ID! name: String! } |
| 36 | type Bot implements Named { id: ID! name: String! } |
| 37 | type Query { results: [Result!]! }`, |
| 38 | "query Q { results { ... on Named { name } } }" |
| 39 | ) |
| 40 | const results = { results: [{ __typename: "User", name: "Ann" }, { __typename: "Bot", name: "ci" }] } |
| 41 | assert.deepStrictEqual(decode(results), results) |
| 42 | assert.throws(() => decode({ results: [{ __typename: "Bot" }] })) |
| 43 | }).pipe(Effect.provide(NodeServices.layer))) |
| 44 | }) |