The best way to control Claude Code, Codex and more from any computer or phone

main
ts 114 lines 4.17 KB
Raw
1 /** Variable @skip/@include conditions produce optional fields; literal conditions are folded. */
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, generateTasks, importGenerated, member, members } from "./utils/generator.ts"
7
8 // In its own file, so the fields it declares don't repeat in `src/ops.graphql.ts`.
9 // Only written for operations that spread it, since an unused fragment is an error.
10 const fragments = `
11 fragment TaskCore on Task {
12 title
13 estimate
14 }
15 `
16
17 const generate = (operations: string) =>
18 generateTasks({
19 ...(operations.includes("...TaskCore") ? { "src/fragments.graphql": fragments } : {}),
20 "src/ops.graphql": operations
21 }).pipe(
22 Effect.map((generated) => {
23 assertNoErrors(generated)
24 return { ops: generated.file("src/ops.graphql.ts"), generated }
25 })
26 )
27
28 describe("Generator @skip and @include", () => {
29 it.effect("a variable condition on a field makes it an optional key", () =>
30 Effect.gen(function*() {
31 const { generated, ops } = yield* generate(`
32 query One($show: Boolean!, $taskId: ID!) {
33 task(id: $taskId) {
34 id
35 title @include(if: $show)
36 estimate @skip(if: $show)
37 }
38 }
39 `)
40 assert.strictEqual(member(ops, "id"), "Shared.ID")
41 assert.strictEqual(member(ops, "title"), "Schema.optionalKey(Schema.String)")
42 assert.strictEqual(member(ops, "estimate"), "Schema.optionalKey(Schema.NullOr(Shared.Float))")
43 const module = yield* importGenerated(generated, "src/ops.graphql.ts")
44 assert.strictEqual(
45 module.One.document,
46 "query One($show:Boolean!$taskId:ID!){task(id:$taskId){id title@include(if:$show)estimate@skip(if:$show)}}"
47 )
48 assert.deepStrictEqual(Schema.decodeUnknownSync(module.One.result)({ task: { id: "1" } }), { task: { id: "1" } })
49 }).pipe(Effect.provide(NodeServices.layer)))
50
51 it.effect("a variable condition on a spread makes every field it contributes an optional key", () =>
52 Effect.gen(function*() {
53 const { ops } = yield* generate(`
54 query One($hide: Boolean!, $taskId: ID!) {
55 task(id: $taskId) {
56 id
57 ...TaskCore @skip(if: $hide)
58 }
59 }
60 `)
61 assert.strictEqual(member(ops, "title"), "Schema.optionalKey(Schema.String)")
62 assert.strictEqual(member(ops, "estimate"), "Schema.optionalKey(Schema.NullOr(Shared.Float))")
63 assert.notInclude(ops, "...TaskCore.fields")
64 }).pipe(Effect.provide(NodeServices.layer)))
65
66 it.effect("literal conditions are folded: the field is dropped or the directive ignored", () =>
67 Effect.gen(function*() {
68 const { ops } = yield* generate(`
69 query One($taskId: ID!) {
70 task(id: $taskId) {
71 id
72 title @include(if: false)
73 done @skip(if: false)
74 estimate @include(if: true)
75 due @skip(if: true)
76 }
77 }
78 `)
79 assert.deepStrictEqual(members(ops, "title"), [])
80 assert.deepStrictEqual(members(ops, "due"), [])
81 assert.strictEqual(member(ops, "done"), "Schema.Boolean")
82 assert.strictEqual(member(ops, "estimate"), "Schema.NullOr(Shared.Float)")
83 }).pipe(Effect.provide(NodeServices.layer)))
84
85 it.effect("a conditional occurrence of a merged field keeps its condition on the children it adds", () =>
86 Effect.gen(function*() {
87 const { ops } = yield* generate(`
88 query One($show: Boolean!, $taskId: ID!) {
89 task(id: $taskId) {
90 id
91 }
92 task(id: $taskId) @include(if: $show) {
93 title
94 }
95 }
96 `)
97 assert.strictEqual(member(ops, "task"), "Schema.NullOr(Task)")
98 assert.strictEqual(member(ops, "id"), "Shared.ID")
99 assert.strictEqual(member(ops, "title"), "Schema.optionalKey(Schema.String)")
100 }).pipe(Effect.provide(NodeServices.layer)))
101
102 it.effect("the children of a field whose only occurrence is conditional stay required", () =>
103 Effect.gen(function*() {
104 const { ops } = yield* generate(`
105 query One($show: Boolean!, $taskId: ID!) {
106 task(id: $taskId) @include(if: $show) {
107 title
108 }
109 }
110 `)
111 assert.strictEqual(member(ops, "task"), "Schema.optionalKey(Schema.NullOr(Task))")
112 assert.strictEqual(member(ops, "title"), "Schema.String")
113 }).pipe(Effect.provide(NodeServices.layer)))
114 })