@distilled.cloud/core 1.0.0-rc.12 → 1.0.0-rc.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (276) hide show
  1. package/lib/api.d.ts +4 -4
  2. package/lib/api.d.ts.map +1 -1
  3. package/lib/api.js +3 -5
  4. package/lib/api.js.map +1 -1
  5. package/lib/category.d.ts.map +1 -1
  6. package/lib/category.js +3 -5
  7. package/lib/category.js.map +1 -1
  8. package/lib/codegen/boolean-string-enums.d.ts +8 -8
  9. package/lib/codegen/boolean-string-enums.d.ts.map +1 -1
  10. package/lib/codegen/boolean-string-enums.js +83 -30
  11. package/lib/codegen/boolean-string-enums.js.map +1 -1
  12. package/lib/codegen/boolean-string-enums.test.js +130 -2
  13. package/lib/codegen/boolean-string-enums.test.js.map +1 -1
  14. package/lib/codegen/cli.d.ts +5 -4
  15. package/lib/codegen/cli.d.ts.map +1 -1
  16. package/lib/codegen/cli.js +12 -9
  17. package/lib/codegen/cli.js.map +1 -1
  18. package/lib/codegen/defer-naming.test.d.ts +2 -0
  19. package/lib/codegen/defer-naming.test.d.ts.map +1 -0
  20. package/lib/codegen/defer-naming.test.js +105 -0
  21. package/lib/codegen/defer-naming.test.js.map +1 -0
  22. package/lib/codegen/emit.d.ts.map +1 -1
  23. package/lib/codegen/emit.js +1 -3
  24. package/lib/codegen/emit.js.map +1 -1
  25. package/lib/codegen/format.d.ts +0 -12
  26. package/lib/codegen/format.d.ts.map +1 -1
  27. package/lib/codegen/format.js +10 -6
  28. package/lib/codegen/format.js.map +1 -1
  29. package/lib/codegen/generated-at.d.ts +29 -0
  30. package/lib/codegen/generated-at.d.ts.map +1 -0
  31. package/lib/codegen/generated-at.js +78 -0
  32. package/lib/codegen/generated-at.js.map +1 -0
  33. package/lib/codegen/generated-at.test.d.ts +2 -0
  34. package/lib/codegen/generated-at.test.d.ts.map +1 -0
  35. package/lib/codegen/generated-at.test.js +81 -0
  36. package/lib/codegen/generated-at.test.js.map +1 -0
  37. package/lib/codegen/generator.d.ts +24 -5
  38. package/lib/codegen/generator.d.ts.map +1 -1
  39. package/lib/codegen/generator.js +69 -80
  40. package/lib/codegen/generator.js.map +1 -1
  41. package/lib/codegen/generator.test.d.ts +2 -0
  42. package/lib/codegen/generator.test.d.ts.map +1 -0
  43. package/lib/codegen/generator.test.js +121 -0
  44. package/lib/codegen/generator.test.js.map +1 -0
  45. package/lib/codegen/graphql-client.d.ts +7 -1
  46. package/lib/codegen/graphql-client.d.ts.map +1 -1
  47. package/lib/codegen/graphql-client.js +264 -148
  48. package/lib/codegen/graphql-client.js.map +1 -1
  49. package/lib/codegen/graphql-client.test.js +98 -238
  50. package/lib/codegen/graphql-client.test.js.map +1 -1
  51. package/lib/codegen/graphql.d.ts.map +1 -1
  52. package/lib/codegen/graphql.js +7 -20
  53. package/lib/codegen/graphql.js.map +1 -1
  54. package/lib/codegen/model-diff.d.ts +10 -0
  55. package/lib/codegen/model-diff.d.ts.map +1 -0
  56. package/lib/codegen/model-diff.js +67 -0
  57. package/lib/codegen/model-diff.js.map +1 -0
  58. package/lib/codegen/naming.d.ts.map +1 -1
  59. package/lib/codegen/naming.js.map +1 -1
  60. package/lib/codegen/openapi-binary.test.d.ts +2 -0
  61. package/lib/codegen/openapi-binary.test.d.ts.map +1 -0
  62. package/lib/codegen/openapi-binary.test.js +67 -0
  63. package/lib/codegen/openapi-binary.test.js.map +1 -0
  64. package/lib/codegen/openapi-cli.d.ts +11 -11
  65. package/lib/codegen/openapi-cli.d.ts.map +1 -1
  66. package/lib/codegen/openapi-cli.js +26 -51
  67. package/lib/codegen/openapi-cli.js.map +1 -1
  68. package/lib/codegen/openapi.d.ts +16 -2
  69. package/lib/codegen/openapi.d.ts.map +1 -1
  70. package/lib/codegen/openapi.js +56 -39
  71. package/lib/codegen/openapi.js.map +1 -1
  72. package/lib/codegen/operations.d.ts.map +1 -1
  73. package/lib/codegen/operations.js.map +1 -1
  74. package/lib/codegen/pagination.d.ts.map +1 -1
  75. package/lib/codegen/pagination.js +2 -4
  76. package/lib/codegen/pagination.js.map +1 -1
  77. package/lib/codegen/patch-audit.d.ts +84 -0
  78. package/lib/codegen/patch-audit.d.ts.map +1 -0
  79. package/lib/codegen/patch-audit.js +366 -0
  80. package/lib/codegen/patch-audit.js.map +1 -0
  81. package/lib/codegen/patch-audit.test.d.ts +2 -0
  82. package/lib/codegen/patch-audit.test.d.ts.map +1 -0
  83. package/lib/codegen/patch-audit.test.js +141 -0
  84. package/lib/codegen/patch-audit.test.js.map +1 -0
  85. package/lib/codegen/patches.d.ts +75 -11
  86. package/lib/codegen/patches.d.ts.map +1 -1
  87. package/lib/codegen/patches.js +301 -65
  88. package/lib/codegen/patches.js.map +1 -1
  89. package/lib/codegen/patches.test.js +55 -5
  90. package/lib/codegen/patches.test.js.map +1 -1
  91. package/lib/codegen/prelude.js +1 -1
  92. package/lib/codegen/prelude.js.map +1 -1
  93. package/lib/codegen/proto.d.ts.map +1 -1
  94. package/lib/codegen/proto.js +2 -4
  95. package/lib/codegen/proto.js.map +1 -1
  96. package/lib/codegen/protocol-imports.test.d.ts +2 -0
  97. package/lib/codegen/protocol-imports.test.d.ts.map +1 -0
  98. package/lib/codegen/protocol-imports.test.js +36 -0
  99. package/lib/codegen/protocol-imports.test.js.map +1 -0
  100. package/lib/codegen/rewrite-operation-ids.d.ts +23 -0
  101. package/lib/codegen/rewrite-operation-ids.d.ts.map +1 -1
  102. package/lib/codegen/rewrite-operation-ids.js +97 -59
  103. package/lib/codegen/rewrite-operation-ids.js.map +1 -1
  104. package/lib/codegen/rewrite-operation-ids.test.js +19 -101
  105. package/lib/codegen/rewrite-operation-ids.test.js.map +1 -1
  106. package/lib/codegen/spec-path.d.ts +1 -1
  107. package/lib/codegen/spec-path.d.ts.map +1 -1
  108. package/lib/codegen/spec-path.js +1 -1
  109. package/lib/codegen/spec-path.js.map +1 -1
  110. package/lib/codegen/untagged-unions.test.d.ts +2 -0
  111. package/lib/codegen/untagged-unions.test.d.ts.map +1 -0
  112. package/lib/codegen/untagged-unions.test.js +80 -0
  113. package/lib/codegen/untagged-unions.test.js.map +1 -0
  114. package/lib/error-category.d.ts.map +1 -1
  115. package/lib/error-category.js.map +1 -1
  116. package/lib/errors.d.ts +2 -1
  117. package/lib/errors.d.ts.map +1 -1
  118. package/lib/errors.js +9 -4
  119. package/lib/errors.js.map +1 -1
  120. package/lib/fixtures/xml/upstream-validator.json +397 -0
  121. package/lib/graphql.d.ts +292 -205
  122. package/lib/graphql.d.ts.map +1 -1
  123. package/lib/graphql.js +945 -636
  124. package/lib/graphql.js.map +1 -1
  125. package/lib/graphql.test.js +135 -764
  126. package/lib/graphql.test.js.map +1 -1
  127. package/lib/graphql.types.d.ts +10 -0
  128. package/lib/graphql.types.d.ts.map +1 -1
  129. package/lib/graphql.types.js +4 -44
  130. package/lib/graphql.types.js.map +1 -1
  131. package/lib/index.d.ts +27 -0
  132. package/lib/index.d.ts.map +1 -0
  133. package/lib/index.js +27 -0
  134. package/lib/index.js.map +1 -0
  135. package/lib/json-patch.d.ts +13 -6
  136. package/lib/json-patch.d.ts.map +1 -1
  137. package/lib/json-patch.js +34 -19
  138. package/lib/json-patch.js.map +1 -1
  139. package/lib/json-patch.test.d.ts +2 -0
  140. package/lib/json-patch.test.d.ts.map +1 -0
  141. package/lib/json-patch.test.js +65 -0
  142. package/lib/json-patch.test.js.map +1 -0
  143. package/lib/pagination.d.ts +2 -1
  144. package/lib/pagination.d.ts.map +1 -1
  145. package/lib/pagination.js +28 -30
  146. package/lib/pagination.js.map +1 -1
  147. package/lib/pagination.test.d.ts +2 -0
  148. package/lib/pagination.test.d.ts.map +1 -0
  149. package/lib/pagination.test.js +131 -0
  150. package/lib/pagination.test.js.map +1 -0
  151. package/lib/protocol-http.d.ts +30 -16
  152. package/lib/protocol-http.d.ts.map +1 -1
  153. package/lib/protocol-http.js +133 -84
  154. package/lib/protocol-http.js.map +1 -1
  155. package/lib/protocol-http.test.js +141 -1
  156. package/lib/protocol-http.test.js.map +1 -1
  157. package/lib/protocol-rest.d.ts +18 -1
  158. package/lib/protocol-rest.d.ts.map +1 -1
  159. package/lib/protocol-rest.js +32 -21
  160. package/lib/protocol-rest.js.map +1 -1
  161. package/lib/protocol-rest.test.d.ts +2 -0
  162. package/lib/protocol-rest.test.d.ts.map +1 -0
  163. package/lib/protocol-rest.test.js +498 -0
  164. package/lib/protocol-rest.test.js.map +1 -0
  165. package/lib/query.d.ts +29 -0
  166. package/lib/query.d.ts.map +1 -0
  167. package/lib/query.js +27 -0
  168. package/lib/query.js.map +1 -0
  169. package/lib/response-validation.d.ts +67 -0
  170. package/lib/response-validation.d.ts.map +1 -0
  171. package/lib/response-validation.js +76 -0
  172. package/lib/response-validation.js.map +1 -0
  173. package/lib/response-validation.test.d.ts +2 -0
  174. package/lib/response-validation.test.d.ts.map +1 -0
  175. package/lib/response-validation.test.js +72 -0
  176. package/lib/response-validation.test.js.map +1 -0
  177. package/lib/retry-after.d.ts.map +1 -1
  178. package/lib/retry-after.js +1 -1
  179. package/lib/retry-after.js.map +1 -1
  180. package/lib/retry-after.test.d.ts +2 -0
  181. package/lib/retry-after.test.d.ts.map +1 -0
  182. package/lib/retry-after.test.js +204 -0
  183. package/lib/retry-after.test.js.map +1 -0
  184. package/lib/retry.d.ts.map +1 -1
  185. package/lib/retry.js +1 -2
  186. package/lib/retry.js.map +1 -1
  187. package/lib/retry.test.d.ts +2 -0
  188. package/lib/retry.test.d.ts.map +1 -0
  189. package/lib/retry.test.js +407 -0
  190. package/lib/retry.test.js.map +1 -0
  191. package/lib/sdks.test.d.ts +2 -0
  192. package/lib/sdks.test.d.ts.map +1 -0
  193. package/lib/sdks.test.js +76 -0
  194. package/lib/sdks.test.js.map +1 -0
  195. package/lib/testing.d.ts +39 -0
  196. package/lib/testing.d.ts.map +1 -0
  197. package/lib/testing.js +41 -0
  198. package/lib/testing.js.map +1 -0
  199. package/lib/trait.d.ts +29 -9
  200. package/lib/trait.d.ts.map +1 -1
  201. package/lib/trait.js +8 -0
  202. package/lib/trait.js.map +1 -1
  203. package/lib/xml.d.ts +44 -0
  204. package/lib/xml.d.ts.map +1 -0
  205. package/lib/xml.js +311 -0
  206. package/lib/xml.js.map +1 -0
  207. package/lib/xml.test.d.ts +2 -0
  208. package/lib/xml.test.d.ts.map +1 -0
  209. package/lib/xml.test.js +210 -0
  210. package/lib/xml.test.js.map +1 -0
  211. package/package.json +8 -11
  212. package/src/api.ts +38 -55
  213. package/src/category.ts +21 -50
  214. package/src/codegen/boolean-string-enums.test.ts +148 -17
  215. package/src/codegen/boolean-string-enums.ts +81 -32
  216. package/src/codegen/cli.ts +29 -47
  217. package/src/codegen/defer-naming.test.ts +113 -0
  218. package/src/codegen/emit.ts +2 -5
  219. package/src/codegen/format.ts +12 -10
  220. package/src/codegen/generated-at.test.ts +89 -0
  221. package/src/codegen/generated-at.ts +88 -0
  222. package/src/codegen/generator.test.ts +137 -0
  223. package/src/codegen/generator.ts +132 -193
  224. package/src/codegen/graphql-client.test.ts +100 -297
  225. package/src/codegen/graphql-client.ts +302 -210
  226. package/src/codegen/graphql.ts +22 -94
  227. package/src/codegen/model-diff.ts +67 -0
  228. package/src/codegen/naming.ts +1 -2
  229. package/src/codegen/openapi-binary.test.ts +91 -0
  230. package/src/codegen/openapi-cli.ts +38 -79
  231. package/src/codegen/openapi.ts +98 -133
  232. package/src/codegen/operations.ts +2 -5
  233. package/src/codegen/pagination.ts +4 -11
  234. package/src/codegen/patch-audit.test.ts +160 -0
  235. package/src/codegen/patch-audit.ts +480 -0
  236. package/src/codegen/patches.test.ts +73 -15
  237. package/src/codegen/patches.ts +389 -99
  238. package/src/codegen/prelude.ts +1 -1
  239. package/src/codegen/proto.ts +18 -54
  240. package/src/codegen/protocol-imports.test.ts +39 -0
  241. package/src/codegen/rewrite-operation-ids.test.ts +23 -114
  242. package/src/codegen/rewrite-operation-ids.ts +111 -98
  243. package/src/codegen/spec-path.ts +3 -6
  244. package/src/codegen/untagged-unions.test.ts +85 -0
  245. package/src/error-category.ts +5 -17
  246. package/src/errors.ts +18 -30
  247. package/src/fixtures/xml/upstream-validator.json +397 -0
  248. package/src/graphql.test.ts +173 -949
  249. package/src/graphql.ts +1468 -1186
  250. package/src/graphql.types.ts +78 -164
  251. package/src/index.ts +26 -0
  252. package/src/json-patch.test.ts +77 -0
  253. package/src/json-patch.ts +54 -60
  254. package/src/pagination.test.ts +169 -0
  255. package/src/pagination.ts +46 -103
  256. package/src/protocol-http.test.ts +178 -9
  257. package/src/protocol-http.ts +174 -163
  258. package/src/protocol-rest.test.ts +703 -0
  259. package/src/protocol-rest.ts +76 -65
  260. package/src/query.ts +39 -0
  261. package/src/response-validation.test.ts +89 -0
  262. package/src/response-validation.ts +107 -0
  263. package/src/retry-after.test.ts +267 -0
  264. package/src/retry-after.ts +4 -10
  265. package/src/retry.test.ts +566 -0
  266. package/src/retry.ts +6 -21
  267. package/src/sdks.test.ts +87 -0
  268. package/src/testing.ts +72 -0
  269. package/src/trait.ts +48 -63
  270. package/src/xml.test.ts +251 -0
  271. package/src/xml.ts +323 -0
  272. package/lib/graphql.fixture.d.ts +0 -249
  273. package/lib/graphql.fixture.d.ts.map +0 -1
  274. package/lib/graphql.fixture.js +0 -240
  275. package/lib/graphql.fixture.js.map +0 -1
  276. package/src/graphql.fixture.ts +0 -371
@@ -1,974 +1,198 @@
1
- import { describe, expect, test } from "bun:test";
2
1
  import * as Effect from "effect/Effect";
3
- import * as Result from "effect/Result";
4
- import * as Stream from "effect/Stream";
5
- import { Kind, parse } from "graphql";
6
- import * as G from "./graphql.ts";
2
+ import * as Layer from "effect/Layer";
3
+ import { describe, expect, test } from "vitest";
7
4
  import {
8
- adversarialArgument,
9
- errorClasses,
10
- fixtureModel,
11
- multipleErrorEnvelope,
12
- partialProjectEnvelope,
13
- type FixtureSchema,
14
- } from "./graphql.fixture.ts";
15
-
16
- const harness = (body: unknown, status = 200) => {
17
- const requests: Array<{ query: string; variables: Record<string, unknown> }> =
18
- [];
19
- const client = G.makeClient<FixtureSchema, never>(
20
- fixtureModel,
21
- (request) =>
22
- Effect.sync(() => {
23
- requests.push(request);
24
- return { body, status, headers: {} };
25
- }),
26
- errorClasses,
27
- );
28
- return { client, requests };
29
- };
30
-
31
- const failure = async <A, E>(effect: Effect.Effect<A, E>) => {
32
- const result = await Effect.runPromise(Effect.result(effect));
33
- if (Result.isSuccess(result)) throw new Error("Expected a typed failure");
34
- return result.failure;
5
+ GqlTransport,
6
+ connectionField,
7
+ listField,
8
+ objectField,
9
+ root,
10
+ rootConnection,
11
+ scalarField,
12
+ type TypeMeta,
13
+ } from "./graphql.ts";
14
+ import { Query } from "./query.ts";
15
+
16
+ type UserRow = {
17
+ email: string;
18
+ name: string;
19
+ projects: ReadonlyArray<{ name: string; stars: number }>;
35
20
  };
36
21
 
37
- const projectIdentity = {
38
- project: { where: { id: "p1" }, select: { id: true } },
39
- } as const;
40
- const servicesSelection = {
41
- project: {
42
- where: { id: "p1" },
43
- select: {
44
- id: true,
45
- services: { select: { edges: { node: { id: true } } } },
46
- },
47
- },
48
- } as const;
49
-
50
- describe("GraphQL selection compilation", () => {
51
- test("only requests chosen fields and serializes arguments as variables", () => {
52
- const compiled = G.compile(fixtureModel, "query", {
53
- project: { where: { id: adversarialArgument }, select: { id: true } },
54
- });
55
- expect(compiled.query).toMatch(/project\s*\(\s*id\s*:\s*\$/);
56
- expect(compiled.query).toMatch(/\{\s*id\s*\}/);
57
- expect(compiled.query).not.toContain("services");
58
- expect(compiled.query).not.toContain(adversarialArgument);
59
- expect(Object.values(compiled.variables)).toEqual([adversarialArgument]);
60
- expect(JSON.parse(JSON.stringify(compiled.variables))).toEqual(
61
- compiled.variables,
62
- );
63
- });
64
-
65
- test("nested arguments keep null, omit undefined, and let server defaults apply", () => {
66
- const compiled = G.compile(fixtureModel, "query", {
67
- project: {
68
- where: { id: "p1" },
69
- select: {
70
- services: {
71
- where: { first: undefined, after: null },
72
- select: { edges: { node: { id: true } } },
73
- },
74
- },
75
- },
76
- });
77
- expect(compiled.query).not.toContain("first:");
78
- expect(compiled.query).toMatch(/services\s*\(\s*after\s*:\s*\$/);
79
- expect(Object.values(compiled.variables)).toEqual(["p1", null]);
80
- });
81
-
82
- test("input objects preserve explicit null and omit optional members", () => {
83
- const compiled = G.compile(fixtureModel, "query", {
84
- projects: {
85
- where: {
86
- filter: {
87
- name: null,
88
- statuses: undefined,
89
- metadata: { text: adversarialArgument, absent: undefined },
90
- },
91
- },
92
- select: { id: true },
93
- },
94
- });
95
- expect(
96
- JSON.parse(JSON.stringify(Object.values(compiled.variables)[0])),
97
- ).toEqual({ name: null, metadata: { text: adversarialArgument } });
98
- expect(compiled.query).toContain("ProjectFilter");
99
- });
100
-
101
- test("aliases give repeated roots independent variables", () => {
102
- const compiled = G.compile(fixtureModel, "query", {
103
- __alias: {
104
- production: { project: { where: { id: "p1" }, select: { id: true } } },
105
- staging: { project: { where: { id: "p2" }, select: { name: true } } },
106
- },
107
- });
108
- expect(compiled.query).toMatch(/production\s*:\s*project/);
109
- expect(compiled.query).toMatch(/staging\s*:\s*project/);
110
- expect(Object.values(compiled.variables)).toEqual(["p1", "p2"]);
111
- });
112
-
113
- test("supports finite selections through recursive schema relationships", () => {
114
- const compiled = G.compile(fixtureModel, "query", {
115
- project: {
116
- where: { id: "p1" },
117
- select: { parent: { parent: { parent: { name: true } } } },
118
- },
119
- });
120
- expect(compiled.query.match(/parent/g)).toHaveLength(3);
121
- expect(compiled.query).not.toContain("services");
122
- });
123
-
124
- test("rejects invalid selections before transport execution", () => {
125
- expect(() =>
126
- G.compile(fixtureModel, "query", {
127
- project: { where: { id: "p1" }, select: { unknownField: true } },
128
- }),
129
- ).toThrow();
130
- expect(() =>
131
- G.compile(fixtureModel, "query", {
132
- project: { where: {}, select: { id: true } },
133
- }),
134
- ).toThrow();
135
- expect(() =>
136
- G.compile(fixtureModel, "query", {
137
- project: { where: { id: "p1" }, select: {} },
138
- }),
139
- ).toThrow();
140
- expect(() =>
141
- G.compile(fixtureModel, "query", { "bad alias": true }),
142
- ).toThrow();
143
- });
22
+ const User: TypeMeta = { name: "User", fields: {} };
23
+ const Project: TypeMeta = { name: "Project", fields: {} };
24
+ Object.assign(User.fields, {
25
+ email: scalarField("email"),
26
+ name: scalarField("name"),
27
+ projects: listField("projects", Project, { first: "Int" }),
28
+ });
29
+ Object.assign(Project.fields, {
30
+ name: scalarField("name"),
31
+ stars: scalarField("stars"),
144
32
  });
145
33
 
146
- describe("GraphQL query execution", () => {
147
- test("structurally wider selections fail in the typed channel before transport", async () => {
148
- const { client, requests } = harness({ data: { ping: true } });
149
- const invalidRoot = { ping: true, invented: true } as const;
150
- const invalidProjection = { id: true, invented: true } as const;
151
- const invalidNested = {
152
- project: { where: { id: "p1" }, select: invalidProjection },
153
- } as const;
154
- const invalidMutation = {
155
- editProject: {
156
- where: { id: "p1", name: "Updated" },
157
- select: invalidProjection,
158
- },
159
- } as const;
160
-
161
- expect((await failure(client.query(invalidRoot)))._tag).toBe(
162
- "GraphQLRequestError",
163
- );
164
- expect((await failure(client.query(invalidNested)))._tag).toBe(
165
- "GraphQLRequestError",
166
- );
167
- expect(
168
- (
169
- await failure(
170
- client.operation("query", "project")({ id: "p1" }, invalidProjection),
171
- )
172
- )._tag,
173
- ).toBe("GraphQLRequestError");
174
- expect((await failure(client.mutation(invalidMutation)))._tag).toBe(
175
- "GraphQLRequestError",
176
- );
177
- expect(requests).toHaveLength(0);
178
- });
179
-
180
- test("sends multiple roots in one HTTP request", async () => {
181
- const { client, requests } = harness({
182
- data: { project: { id: "p1" }, ping: true },
183
- });
184
- const value = await Effect.runPromise(
185
- client.query({ ...projectIdentity, ping: true }),
186
- );
187
- expect(value).toEqual({ project: { id: "p1" }, ping: true });
188
- expect(requests).toHaveLength(1);
189
- });
190
-
191
- test("__proto__ aliases are own properties without changing result prototypes", async () => {
192
- const { client } = harness({ data: { ["__proto__"]: { id: "p1" } } });
193
- const value = await Effect.runPromise(
194
- client.query({
195
- __alias: {
196
- ["__proto__"]: {
197
- project: { where: { id: "p1" }, select: { id: true } },
198
- },
199
- },
200
- }),
201
- );
202
- expect(Object.hasOwn(value, "__proto__")).toBe(true);
203
- expect(Object.getPrototypeOf(value)).toBe(Object.prototype);
204
- expect(value["__proto__"]).toEqual({ id: "p1" });
205
- });
206
-
207
- test("decodes null field and nullable list items without inventing missing fields", async () => {
208
- const { client } = harness({
209
- data: { projects: [null, { id: "p1", description: null }] },
210
- });
211
- const value = await Effect.runPromise(
212
- client.query({ projects: { select: { id: true, description: true } } }),
213
- );
214
- expect(value).toEqual({
215
- projects: [null, { id: "p1", description: null }],
216
- });
217
- });
218
-
219
- test("decodes union fragments into the selected concrete branch", async () => {
220
- const { client, requests } = harness({
221
- data: {
222
- search: [
223
- { __typename: "Project", name: "Production" },
224
- { __typename: "Service", status: "ACTIVE" },
225
- ],
226
- },
227
- });
228
- const value = await Effect.runPromise(
229
- client.query({
230
- search: {
231
- where: { text: "prod" },
232
- select: {
233
- __typename: true,
234
- __on: { Project: { name: true }, Service: { status: true } },
235
- },
236
- },
237
- }),
238
- );
239
- expect(value.search).toEqual([
240
- { __typename: "Project", name: "Production" },
241
- { __typename: "Service", status: "ACTIVE" },
242
- ]);
243
- expect(requests[0]!.query).toMatch(/\.\.\.\s*on\s+Project/);
244
- expect(requests[0]!.query).toMatch(/\.\.\.\s*on\s+Service/);
245
- });
246
-
247
- test("interface selections include shared fields and concrete fragments", async () => {
248
- const { client } = harness({
249
- data: { node: { __typename: "Project", id: "p1", name: "Production" } },
250
- });
251
- const value = await Effect.runPromise(
252
- client.query({
253
- node: {
254
- where: { id: "p1" },
255
- select: {
256
- __typename: true,
257
- id: true,
258
- __on: { Project: { name: true } },
259
- },
260
- },
261
- }),
262
- );
263
- expect(value.node).toEqual({
264
- __typename: "Project",
265
- id: "p1",
266
- name: "Production",
267
- });
268
- });
269
-
270
- test("interface and concrete selections merge nested fields at the same response path", async () => {
271
- const { client } = harness({
272
- data: {
273
- node: {
274
- __typename: "Project",
275
- parent: { id: "parent-1", name: "Parent" },
276
- },
277
- },
278
- });
279
- const value = await Effect.runPromise(
280
- client.query({
281
- node: {
282
- where: { id: "p1" },
283
- select: {
284
- __typename: true,
285
- parent: { id: true },
286
- __on: { Project: { parent: { name: true } } },
34
+ const Mock = Layer.succeed(GqlTransport, {
35
+ execute: (request) =>
36
+ Effect.sync(() => {
37
+ expect(request.document).toContain("email");
38
+ expect(request.document).toContain("stars");
39
+ expect(request.document).toContain("name");
40
+ return {
41
+ data: {
42
+ me: {
43
+ email: "ada@railway.app",
44
+ projects: [
45
+ { name: "engine", stars: 12 },
46
+ { name: "bombe", stars: 3 },
47
+ ],
287
48
  },
288
49
  },
289
- }),
290
- );
291
- expect(value.node).toEqual({
292
- __typename: "Project",
293
- parent: { id: "parent-1", name: "Parent" },
294
- });
295
- });
296
-
297
- test("include and skip directives allow omitted fields without false decode failures", async () => {
298
- const { client, requests } = harness({ data: { project: { id: "p1" } } });
299
- const include: boolean = false;
300
- const skip: boolean = true;
301
- const value = await Effect.runPromise(
302
- client.query({
303
- project: {
304
- where: { id: "p1" },
305
- select: { id: true, name: { $skip: skip, select: true } },
306
- },
307
- ping: { $include: include, select: true },
308
- }),
309
- );
310
- expect(value).toEqual({ project: { id: "p1" } });
311
- expect(requests[0]!.query).toContain("@include");
312
- expect(requests[0]!.query).toContain("@skip");
313
- expect(Object.values(requests[0]!.variables)).toContain(false);
314
- expect(Object.values(requests[0]!.variables)).toContain(true);
315
- });
316
-
317
- test("bad selected scalar types produce a decode error", async () => {
318
- const { client } = harness({
319
- data: { project: { id: { invalid: true } } },
320
- });
321
- expect((await failure(client.query(projectIdentity)))._tag).toBe(
322
- "GraphQLDecodeError",
323
- );
324
- });
325
-
326
- test("missing non-null selected fields produce a decode error", async () => {
327
- const { client } = harness({ data: { project: {} } });
328
- expect((await failure(client.query(projectIdentity)))._tag).toBe(
329
- "GraphQLDecodeError",
330
- );
331
- });
332
-
333
- test("malformed errors envelopes never masquerade as success", async () => {
334
- const { client } = harness({
335
- data: { ping: true },
336
- errors: [{ path: ["ping"] }],
337
- });
338
- expect((await failure(client.query({ ping: true })))._tag).toBe(
339
- "GraphQLDecodeError",
340
- );
341
- });
50
+ };
51
+ }),
342
52
  });
343
53
 
344
- describe("GraphQL typed failures", () => {
345
- const descriptionSelection = {
346
- search: {
347
- where: { text: "all" },
348
- select: {
349
- __typename: true,
350
- __on: {
351
- Project: { __alias: { label: { description: true } } },
352
- Service: { __alias: { label: { description: true } } },
353
- },
354
- },
355
- },
356
- } as const;
357
-
358
- test("list indexes and concrete typenames disambiguate identical branch error paths", async () => {
359
- const { client } = harness({
360
- data: {
361
- search: [
362
- { __typename: "Service", label: null },
363
- { __typename: "Project", label: null },
364
- ],
365
- },
366
- errors: [
367
- {
368
- message: "Description unavailable",
369
- path: ["search", 0, "label"],
370
- extensions: { code: "DESCRIPTION_UNAVAILABLE" },
371
- },
372
- {
373
- message: "Description unavailable",
374
- path: ["search", 1, "label"],
375
- extensions: { code: "DESCRIPTION_UNAVAILABLE" },
376
- },
377
- ],
378
- });
379
- const report = await Effect.runPromise(
380
- client.report.query(descriptionSelection),
381
- );
382
- expect(report.errors.map((error) => error._tag)).toEqual([
383
- "ServiceDescriptionUnavailable",
384
- "ProjectDescriptionUnavailable",
385
- ]);
386
- });
387
-
388
- test("erased concrete-type data leaves conflicting equal-rank error matches unknown", async () => {
389
- const { client } = harness({
390
- data: null,
391
- errors: [
392
- {
393
- message: "Description unavailable",
394
- path: ["search", 0, "label"],
395
- extensions: { code: "DESCRIPTION_UNAVAILABLE" },
396
- },
397
- ],
398
- });
399
- const report = await Effect.runPromise(
400
- client.report.query(descriptionSelection),
401
- );
402
- expect(report.errors[0]!._tag).toBe("UnknownGraphQLError");
403
- expect(report.errors[0]!.path).toEqual(["search", 0, "label"]);
404
- });
405
- test("strict execution aggregates a single field issue and preserves diagnostics", async () => {
406
- const { client } = harness(partialProjectEnvelope);
407
- const error = await failure(
408
- client.query({ ...servicesSelection, ping: true }),
409
- );
410
- expect(error._tag).toBe("GraphQLFailure");
411
- if (error._tag !== "GraphQLFailure")
412
- throw new Error("Expected GraphQLFailure");
413
- expect(error.errors).toHaveLength(1);
414
- expect(error.errors[0]!._tag).toBe("ServicesUnavailable");
415
- expect(error.errors[0]!.path).toEqual(["project", "services"]);
416
- expect(error.errors[0]!.extensions).toEqual({
417
- code: "UNAVAILABLE",
418
- incident: "incident-1",
54
+ describe("Query.fn", () => {
55
+ test("builds one document from a returned plan and maps/filters after fetch", async () => {
56
+ const program = Query.fn(() => {
57
+ const me = root<UserRow>("query", "me", User);
58
+ return {
59
+ email: me.email,
60
+ projects: me.projects({ first: 5 }).pipe(
61
+ Query.filter((project: Query<{ stars: number; name: string }>) =>
62
+ project.stars.pipe(Query.map((stars: number) => stars > 5)),
63
+ ),
64
+ Query.map((project: Query<{ stars: number; name: string }>) => ({
65
+ name: project.name,
66
+ stars: project.stars,
67
+ })),
68
+ ),
69
+ };
70
+ })();
71
+ const result = await Effect.runPromise(program.pipe(Effect.provide(Mock)));
72
+ expect(result).toEqual({
73
+ email: "ada@railway.app",
74
+ projects: [{ name: "engine", stars: 12 }],
419
75
  });
420
- expect(error.data).toEqual(partialProjectEnvelope.data);
421
76
  });
422
77
 
423
- test("report mode preserves partial data and typed issues as a success", async () => {
424
- const { client } = harness(partialProjectEnvelope);
425
- const report = await Effect.runPromise(
426
- client.report.query({ ...servicesSelection, ping: true }),
427
- );
428
- expect(report.data).toEqual(partialProjectEnvelope.data);
429
- expect(report.errors.map((error) => error._tag)).toEqual([
430
- "ServicesUnavailable",
431
- ]);
432
- });
433
-
434
- test("multiple errors retain aliases, tags, and all unaffected data", async () => {
435
- const { client } = harness(multipleErrorEnvelope);
436
- const error = await failure(
437
- client.query({
438
- __alias: {
439
- production: {
440
- project: { where: { id: "p1" }, select: { id: true } },
441
- },
442
- staging: {
443
- project: {
444
- where: { id: "p2" },
445
- select: servicesSelection.project.select,
78
+ test("Relay connections map as a list of nodes, one POST", async () => {
79
+ const Workspace: TypeMeta = { name: "Workspace", fields: {} };
80
+ Object.assign(Workspace.fields, { name: scalarField("name") });
81
+ const layer = Layer.succeed(GqlTransport, {
82
+ execute: (request) =>
83
+ Effect.sync(() => {
84
+ expect(request.document).toContain("edges");
85
+ expect(request.document).toContain("node");
86
+ expect(request.document).toContain("name");
87
+ expect(request.document).not.toContain("pageInfo");
88
+ return {
89
+ data: {
90
+ projects: {
91
+ edges: [{ node: { name: "engine" } }, { node: { name: "bombe" } }],
92
+ },
446
93
  },
447
- },
448
- },
449
- }),
450
- );
451
- expect(error._tag).toBe("GraphQLFailure");
452
- if (error._tag !== "GraphQLFailure")
453
- throw new Error("Expected GraphQLFailure");
454
- expect(error.errors.map((issue) => issue._tag)).toEqual([
455
- "ProjectNotFound",
456
- "ServicesUnavailable",
457
- ]);
458
- expect(error.errors.map((issue) => issue.path)).toEqual([
459
- ["production"],
460
- ["staging", "services"],
461
- ]);
462
- expect(error.data).toEqual(multipleErrorEnvelope.data);
463
- });
464
-
465
- test("null bubbling and list indexes do not prevent schema-coordinate matching", async () => {
466
- const { client } = harness({
467
- data: { projects: [null] },
468
- errors: [
469
- {
470
- message: "Services unavailable",
471
- path: ["projects", 0, "services"],
472
- extensions: { code: "UNAVAILABLE" },
473
- },
474
- ],
475
- });
476
- const report = await Effect.runPromise(
477
- client.report.query({
478
- projects: { select: servicesSelection.project.select },
479
- }),
480
- );
481
- expect(report.errors[0]!._tag).toBe("ServicesUnavailable");
482
- expect(report.errors[0]!.path).toEqual(["projects", 0, "services"]);
483
- expect(report.data).toEqual({ projects: [null] });
484
- });
485
-
486
- test("overlapping interface and concrete selections retain nested error contracts", async () => {
487
- const { client } = harness({
488
- data: {
489
- node: {
490
- __typename: "Project",
491
- parent: { id: "parent-1", services: null },
492
- },
493
- },
494
- errors: [
495
- {
496
- message: "Services unavailable",
497
- path: ["node", "parent", "services"],
498
- extensions: { code: "UNAVAILABLE" },
499
- },
500
- ],
94
+ };
95
+ }),
501
96
  });
502
- const report = await Effect.runPromise(
503
- client.report.query({
504
- node: {
505
- where: { id: "p1" },
506
- select: {
507
- __typename: true,
508
- parent: { id: true },
509
- __on: {
510
- Project: {
511
- parent: {
512
- services: { select: { edges: { node: { id: true } } } },
97
+ const program = Query.fn(() => {
98
+ const page = rootConnection<{ name: string }>(
99
+ "query",
100
+ "projects",
101
+ Workspace,
102
+ { first: 20 },
103
+ { first: "Int" },
104
+ );
105
+ return page.pipe(Query.map((project) => project.name));
106
+ })();
107
+ const names = await Effect.runPromise(program.pipe(Effect.provide(layer)));
108
+ expect(names).toEqual(["engine", "bombe"]);
109
+ });
110
+
111
+ test("nested connection fields unwrap edges.node", async () => {
112
+ const Service: TypeMeta = { name: "Service", fields: {} };
113
+ Object.assign(Service.fields, { name: scalarField("name") });
114
+ const Project: TypeMeta = { name: "Project", fields: {} };
115
+ Object.assign(Project.fields, {
116
+ name: scalarField("name"),
117
+ services: connectionField("services", Service, { first: "Int" }),
118
+ });
119
+ const layer = Layer.succeed(GqlTransport, {
120
+ execute: (request) =>
121
+ Effect.sync(() => {
122
+ expect(request.document).toMatch(/services[\s\S]*edges[\s\S]*node/);
123
+ return {
124
+ data: {
125
+ project: {
126
+ name: "engine",
127
+ services: {
128
+ edges: [{ node: { name: "web" } }, { node: { name: "db" } }],
513
129
  },
514
130
  },
515
131
  },
516
- },
517
- },
518
- }),
519
- );
520
- expect(report.errors[0]!._tag).toBe("ServicesUnavailable");
521
- });
522
-
523
- test("known codes at undeclared fields fall back to unknown", async () => {
524
- const { client } = harness({
525
- data: { project: null },
526
- errors: [
527
- {
528
- message: "Unavailable",
529
- path: ["project"],
530
- extensions: { code: "UNAVAILABLE" },
531
- },
532
- ],
533
- });
534
- const report = await Effect.runPromise(
535
- client.report.query(projectIdentity),
536
- );
537
- expect(report.errors[0]!._tag).toBe("UnknownGraphQLError");
538
- });
539
-
540
- test("unrecognized and unselected error paths fall back to unknown", async () => {
541
- const { client } = harness({
542
- data: { project: null },
543
- errors: [
544
- {
545
- message: "New provider error",
546
- path: ["project"],
547
- extensions: { code: "NEW_CODE" },
548
- },
549
- {
550
- message: "Unknown field path",
551
- path: ["neverSelected"],
552
- extensions: { code: "NOT_FOUND" },
553
- },
554
- ],
555
- });
556
- const report = await Effect.runPromise(
557
- client.report.query(projectIdentity),
558
- );
559
- expect(report.errors.map((error) => error._tag)).toEqual([
560
- "UnknownGraphQLError",
561
- "UnknownGraphQLError",
562
- ]);
563
- });
564
-
565
- test("global request errors without paths are decoded even on HTTP 400", async () => {
566
- const { client } = harness(
567
- {
568
- errors: [
569
- {
570
- message: "Authentication required",
571
- extensions: { code: "UNAUTHENTICATED" },
572
- },
573
- ],
574
- },
575
- 400,
576
- );
577
- const report = await Effect.runPromise(
578
- client.report.query(projectIdentity),
579
- );
580
- expect(report.errors[0]!._tag).toBe("Unauthorized");
581
- expect(report.status).toBe(400);
582
- });
583
-
584
- test("code and message matchers require both constraints", async () => {
585
- const { client } = harness({
586
- errors: [
587
- {
588
- message: "Not Authorized",
589
- extensions: { code: "INTERNAL_SERVER_ERROR" },
590
- },
591
- {
592
- message: "Resolver crashed",
593
- extensions: { code: "INTERNAL_SERVER_ERROR" },
594
- },
595
- ],
596
- });
597
- const report = await Effect.runPromise(
598
- client.report.query(projectIdentity),
599
- );
600
- expect(report.errors.map((error) => error._tag)).toEqual([
601
- "Unauthorized",
602
- "UnknownGraphQLError",
603
- ]);
604
- });
605
-
606
- test("a mutation with response errors is never automatically retried", async () => {
607
- const { client, requests } = harness({
608
- data: null,
609
- errors: [
610
- {
611
- message: "Not found",
612
- path: ["editProject"],
613
- extensions: { code: "NOT_FOUND" },
614
- },
615
- ],
616
- });
617
- const error = await failure(
618
- client.mutation({
619
- editProject: {
620
- where: { id: "p1", name: "Changed" },
621
- select: { id: true },
622
- },
623
- }),
624
- );
625
- expect(error._tag).toBe("GraphQLFailure");
626
- expect(requests).toHaveLength(1);
627
- expect(requests[0]!.query).toMatch(/^mutation/);
628
- });
629
-
630
- test("catchTags recovers only when every issue has an allowed tag", async () => {
631
- const { client } = harness({
632
- data: { project: null },
633
- errors: [
634
- {
635
- message: "Gone",
636
- path: ["project"],
637
- extensions: { code: "NOT_FOUND" },
638
- },
639
- ],
640
- });
641
- const value = await Effect.runPromise(
642
- client
643
- .query(projectIdentity)
644
- .pipe(G.catchTags("ProjectNotFound", () => Effect.succeed("absent"))),
645
- );
646
- expect(value).toBe("absent");
647
- });
648
-
649
- test("catchTags never hides another issue in a mixed aggregate", async () => {
650
- const { client } = harness({
651
- data: { project: null },
652
- errors: [
653
- {
654
- message: "Gone",
655
- path: ["project"],
656
- extensions: { code: "NOT_FOUND" },
657
- },
658
- {
659
- message: "Authentication required",
660
- extensions: { code: "UNAUTHENTICATED" },
661
- },
662
- ],
132
+ };
133
+ }),
663
134
  });
664
- let recovered = false;
665
- const error = await failure(
666
- client.query(projectIdentity).pipe(
667
- G.catchTags("ProjectNotFound", () =>
135
+ const program = Query.fn(() => {
136
+ const project = root<{
137
+ name: string;
138
+ services: ReadonlyArray<{ name: string }>;
139
+ }>("query", "project", Project, { id: "p1" }, { id: "String!" });
140
+ return {
141
+ name: project.name,
142
+ services: project
143
+ .services({ first: 20 })
144
+ .pipe(Query.map((service: Query<{ name: string }>) => service.name)),
145
+ };
146
+ })();
147
+ const result = await Effect.runPromise(program.pipe(Effect.provide(layer)));
148
+ expect(result).toEqual({ name: "engine", services: ["web", "db"] });
149
+ });
150
+ test("Query.map on an object selects its fields and keeps null", async () => {
151
+ const Deployment: TypeMeta = { name: "Deployment", fields: {} };
152
+ Object.assign(Deployment.fields, {
153
+ id: scalarField("id"),
154
+ status: scalarField("status"),
155
+ });
156
+ const Service: TypeMeta = { name: "Service", fields: {} };
157
+ Object.assign(Service.fields, {
158
+ name: scalarField("name"),
159
+ latestDeployment: objectField("latestDeployment", Deployment),
160
+ });
161
+ const documents: string[] = [];
162
+ const respond = (latestDeployment: unknown) =>
163
+ Layer.succeed(GqlTransport, {
164
+ execute: (request) =>
668
165
  Effect.sync(() => {
669
- recovered = true;
670
- return "absent";
166
+ documents.push(request.document);
167
+ return {
168
+ data: { service: { name: "web", latestDeployment } },
169
+ };
671
170
  }),
171
+ });
172
+ const program = Query.fn(() => {
173
+ const service = root<{
174
+ name: string;
175
+ latestDeployment: { id: string; status: string } | null;
176
+ }>("query", "service", Service);
177
+ return {
178
+ name: service.name,
179
+ deployment: service.latestDeployment.pipe(
180
+ Query.map((deployment) => ({
181
+ id: deployment.id,
182
+ status: deployment.status,
183
+ })),
672
184
  ),
185
+ };
186
+ })();
187
+ expect(
188
+ await Effect.runPromise(
189
+ program.pipe(Effect.provide(respond({ id: "d1", status: "SUCCESS" }))),
673
190
  ),
674
- );
675
- expect(recovered).toBe(false);
676
- expect(error._tag).toBe("GraphQLFailure");
677
- if (error._tag !== "GraphQLFailure")
678
- throw new Error("Expected GraphQLFailure");
679
- expect(error.errors.map((issue) => issue._tag)).toEqual([
680
- "ProjectNotFound",
681
- "Unauthorized",
682
- ]);
683
- });
684
- });
685
-
686
- describe("GraphQL connection pagination", () => {
687
- const paginated = (
688
- responses: Array<{
689
- edges: Array<{ node: { id: string } }>;
690
- pageInfo: { endCursor: string | null; hasNextPage: boolean };
691
- }>,
692
- ) => {
693
- const requests: Array<G.GraphQLRequest> = [];
694
- const client = G.makeClient<FixtureSchema, never>(
695
- fixtureModel,
696
- (request) =>
697
- Effect.sync(() => {
698
- const page = responses[requests.length];
699
- requests.push(request);
700
- if (!page)
701
- throw new Error("Paginator made an unexpected extra request");
702
- // Respect response aliases, including cursor fields added by the client.
703
- const document = parse(request.query);
704
- const operation = document.definitions.find(
705
- (node) => node.kind === Kind.OPERATION_DEFINITION,
706
- )!;
707
- if (operation.kind !== Kind.OPERATION_DEFINITION)
708
- throw new Error("Expected an operation");
709
- const root = operation.selectionSet.selections[0]!;
710
- if (root.kind !== Kind.FIELD)
711
- throw new Error("Expected a connection field");
712
- const selected: Record<string, unknown> = {};
713
- for (const child of root.selectionSet!.selections) {
714
- if (child.kind !== Kind.FIELD) continue;
715
- selected[child.alias?.value ?? child.name.value] =
716
- page[child.name.value as keyof typeof page];
717
- }
718
- return {
719
- body: { data: { services: selected } },
720
- status: 200,
721
- headers: {},
722
- };
723
- }),
724
- errorClasses,
725
- );
726
- return { requests, services: client.operation("query", "services") };
727
- };
728
-
729
- test("items advances cursors and stops on hasNextPage=false", async () => {
730
- const { services, requests } = paginated([
731
- {
732
- edges: [{ node: { id: "s1" } }],
733
- pageInfo: { endCursor: "c1", hasNextPage: true },
734
- },
735
- {
736
- edges: [{ node: { id: "s2" } }],
737
- pageInfo: { endCursor: "c2", hasNextPage: false },
738
- },
739
- ]);
740
- const values = await Effect.runPromise(
741
- Stream.runCollect(services.items({ first: 1 }, { id: true })),
742
- );
743
- expect(Array.from(values)).toEqual([{ id: "s1" }, { id: "s2" }]);
744
- expect(requests).toHaveLength(2);
745
- expect(Object.values(requests[0]!.variables)).toEqual([1]);
746
- expect(Object.values(requests[1]!.variables)).toEqual([1, "c1"]);
747
- expect(requests[0]!.query).not.toContain("name");
748
- });
749
-
750
- test("empty terminal pages do not cause another request", async () => {
751
- const { services, requests } = paginated([
752
- { edges: [], pageInfo: { endCursor: null, hasNextPage: false } },
753
- ]);
754
- const values = await Effect.runPromise(
755
- Stream.runCollect(services.items({}, { id: true })),
756
- );
757
- expect(Array.from(values)).toEqual([]);
758
- expect(requests).toHaveLength(1);
759
- });
760
-
761
- test("pages omits internally selected pageInfo from the requested projection", async () => {
762
- const { services, requests } = paginated([
763
- {
764
- edges: [{ node: { id: "s1" } }],
765
- pageInfo: { endCursor: null, hasNextPage: false },
766
- },
767
- ]);
768
- const pages = await Effect.runPromise(
769
- Stream.runCollect(services.pages({}, { edges: { node: { id: true } } })),
770
- );
771
- expect(Array.from(pages)).toEqual([{ edges: [{ node: { id: "s1" } }] }]);
772
- expect(requests[0]!.query).toContain("pageInfo");
773
- });
774
-
775
- test("a user alias named pageInfo cannot collide with hidden pagination fields", async () => {
776
- const { services, requests } = paginated([
777
- {
778
- edges: [{ node: { id: "s1" } }],
779
- pageInfo: { endCursor: null, hasNextPage: false },
780
- },
781
- ]);
782
- const pages = await Effect.runPromise(
783
- Stream.runCollect(
784
- services.pages(
785
- {},
786
- {
787
- __alias: { pageInfo: { edges: { node: { id: true } } } },
788
- },
789
- ),
790
- ),
791
- );
792
- expect(Array.from(pages)).toEqual([{ pageInfo: [{ node: { id: "s1" } }] }]);
793
- expect(requests).toHaveLength(1);
794
- });
795
-
796
- test("wrapped pageInfo selections still fetch the internal pagination cursor", async () => {
797
- const { services, requests } = paginated([
798
- { edges: [], pageInfo: { endCursor: "c1", hasNextPage: true } },
799
- { edges: [], pageInfo: { endCursor: null, hasNextPage: false } },
800
- ]);
801
- const pages = await Effect.runPromise(
802
- Stream.runCollect(
803
- services.pages({}, { pageInfo: { select: { hasNextPage: true } } }),
804
- ),
805
- );
806
- expect(Array.from(pages)).toEqual([
807
- { pageInfo: { hasNextPage: true } },
808
- { pageInfo: { hasNextPage: false } },
809
- ]);
810
- expect(requests).toHaveLength(2);
811
- });
812
-
813
- test("conditional pageInfo projection cannot skip internal cursor fetching", async () => {
814
- const { services, requests } = paginated([
815
- {
816
- edges: [{ node: { id: "s1" } }],
817
- pageInfo: { endCursor: "c1", hasNextPage: true },
818
- },
819
- {
820
- edges: [{ node: { id: "s2" } }],
821
- pageInfo: { endCursor: null, hasNextPage: false },
822
- },
823
- ]);
824
- const pages = await Effect.runPromise(
825
- Stream.runCollect(
826
- services.pages(
827
- {},
828
- {
829
- edges: { node: { id: true } },
830
- pageInfo: { $skip: true, select: { hasNextPage: true } },
831
- },
832
- ),
833
- ),
834
- );
835
- expect(Array.from(pages)).toEqual([
836
- { edges: [{ node: { id: "s1" } }] },
837
- { edges: [{ node: { id: "s2" } }] },
838
- ]);
839
- expect(requests).toHaveLength(2);
840
- });
841
-
842
- test("non-advancing cursors fail without hanging", async () => {
843
- const { services, requests } = paginated([
844
- {
845
- edges: [{ node: { id: "s1" } }],
846
- pageInfo: { endCursor: "c1", hasNextPage: true },
847
- },
848
- {
849
- edges: [{ node: { id: "s1" } }],
850
- pageInfo: { endCursor: "c1", hasNextPage: true },
851
- },
852
- ]);
853
- const error = await failure(
854
- Stream.runCollect(services.items({}, { id: true })),
855
- );
856
- expect(error._tag).toBe("GraphQLDecodeError");
857
- expect(requests).toHaveLength(2);
858
- });
859
-
860
- test("invalid pagination input fails in the typed channel without making a request", async () => {
861
- const { services, requests } = paginated([]);
862
- const stream = services.items(
863
- { first: "bad" as unknown as number },
864
- { id: true },
865
- );
866
- const error = await failure(Stream.runCollect(stream));
867
- expect(error._tag).toBe("GraphQLRequestError");
868
- expect(requests).toHaveLength(0);
869
- });
870
- });
871
-
872
- describe("GraphQL safe query retries", () => {
873
- const retryableModel = {
874
- ...fixtureModel,
875
- errors: {
876
- ...fixtureModel.errors,
877
- ServicesUnavailable: {
878
- ...fixtureModel.errors.ServicesUnavailable!,
879
- retryable: true,
880
- },
881
- },
882
- };
883
- const unavailable = (root = "services") => ({
884
- data: null,
885
- errors: [
886
- {
887
- message: "Services unavailable",
888
- path: [root],
889
- extensions: { code: "UNAVAILABLE" },
890
- },
891
- ],
892
- });
893
- const retried = (response: (attempt: number) => unknown) => {
894
- const requests: G.GraphQLRequest[] = [];
895
- const client = G.makeClient<FixtureSchema, never>(
896
- retryableModel,
897
- (request) =>
898
- Effect.sync(() => {
899
- requests.push(request);
900
- return { body: response(requests.length), status: 200, headers: {} };
901
- }),
902
- errorClasses,
903
- );
904
- return { client, requests };
905
- };
906
-
907
- test("a safely retryable query reuses the document and variables", async () => {
908
- const { client, requests } = retried((attempt) =>
909
- attempt === 1 ? unavailable() : { data: { services: { edges: [] } } },
910
- );
911
- const result = await Effect.runPromise(
912
- client.query({
913
- services: {
914
- where: { first: 1 },
915
- select: { edges: { node: { id: true } } },
916
- },
917
- }),
918
- );
919
- expect(result).toEqual({ services: { edges: [] } });
920
- expect(requests).toHaveLength(2);
921
- expect(requests[0]!.query).toBe(requests[1]!.query);
922
- expect(requests[0]!.variables).toEqual(requests[1]!.variables);
923
- });
924
-
925
- test("retryable query failures stop after five retries", async () => {
926
- const { client, requests } = retried(() => unavailable());
927
- const error = await failure(
928
- client.query({ services: { select: { edges: { node: { id: true } } } } }),
929
- );
930
- expect(error._tag).toBe("GraphQLFailure");
931
- expect(requests).toHaveLength(6);
932
- }, 15_000);
933
-
934
- test("an aggregate containing a nonretryable issue is never retried", async () => {
935
- const { client, requests } = retried(() => ({
936
- data: null,
937
- errors: [
938
- ...unavailable().errors,
939
- { message: "Unauthorized", extensions: { code: "UNAUTHENTICATED" } },
940
- ],
941
- }));
942
- const error = await failure(
943
- client.query({ services: { select: { edges: { node: { id: true } } } } }),
944
- );
945
- expect(error._tag).toBe("GraphQLFailure");
946
- expect(requests).toHaveLength(1);
947
- });
948
-
949
- test("mutations never retry even when the field failure is marked retryable", async () => {
950
- const { client, requests } = retried(() => ({
951
- data: null,
952
- errors: [
953
- {
954
- message: "Services unavailable",
955
- path: ["editProject", "services"],
956
- extensions: { code: "UNAVAILABLE" },
957
- },
958
- ],
959
- }));
960
- const error = await failure(
961
- client.mutation({
962
- editProject: {
963
- where: { id: "p1", name: "Updated" },
964
- select: { services: { select: { edges: { node: { id: true } } } } },
965
- },
966
- }),
967
- );
968
- expect(error._tag).toBe("GraphQLFailure");
969
- if (error._tag !== "GraphQLFailure")
970
- throw new Error("Expected GraphQLFailure");
971
- expect(error.errors[0]!._tag).toBe("ServicesUnavailable");
972
- expect(requests).toHaveLength(1);
191
+ ).toEqual({ name: "web", deployment: { id: "d1", status: "SUCCESS" } });
192
+ expect(documents[0]).toMatch(/latestDeployment \{\s+id\s+status/);
193
+ expect(await Effect.runPromise(program.pipe(Effect.provide(respond(null))))).toEqual({
194
+ name: "web",
195
+ deployment: null,
196
+ });
973
197
  });
974
198
  });