@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,7 +1,5 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { buildSchema, introspectionFromSchema, parse, validate } from "graphql";
3
- import { compile } from "../graphql.ts";
4
- import { fixtureSDL } from "../graphql.fixture.ts";
1
+ import { stripTypeScriptTypes } from "node:module";
2
+ import { describe, expect, test } from "vitest";
5
3
  import { applyOperation } from "../json-patch.ts";
6
4
  import {
7
5
  convertGraphQLClient,
@@ -12,35 +10,18 @@ import {
12
10
 
13
11
  const named = (name: string, kind = "OBJECT") => ({ kind, name });
14
12
  const required = (ofType: object) => ({ kind: "NON_NULL", ofType });
15
- const list = (ofType: object) => ({ kind: "LIST", ofType });
16
13
  const field = (name: string, type: object, args: object[] = []) => ({
17
14
  name,
18
15
  type,
19
16
  args,
20
17
  });
18
+
21
19
  const fixture = {
22
20
  queryType: { name: "Query" },
23
21
  mutationType: { name: "Mutation" },
24
22
  types: [
25
23
  { kind: "SCALAR", name: "String" },
26
24
  { kind: "SCALAR", name: "Int" },
27
- { kind: "SCALAR", name: "JSON" },
28
- {
29
- kind: "ENUM",
30
- name: "Status",
31
- enumValues: [{ name: "ACTIVE" }, { name: "OLD", isDeprecated: true }],
32
- },
33
- {
34
- kind: "INPUT_OBJECT",
35
- name: "Filter",
36
- inputFields: [
37
- {
38
- name: "status",
39
- type: named("Status", "ENUM"),
40
- defaultValue: "ACTIVE",
41
- },
42
- ],
43
- },
44
25
  {
45
26
  kind: "OBJECT",
46
27
  name: "Query",
@@ -48,7 +29,13 @@ const fixture = {
48
29
  field("project", required(named("Project")), [
49
30
  { name: "id", type: required(named("String", "SCALAR")) },
50
31
  ]),
51
- field("search", list(named("Search", "UNION"))),
32
+ field("me", required(named("User"))),
33
+ field("projects", required(named("ProjectConnection")), [
34
+ {
35
+ name: "first",
36
+ type: named("Int", "SCALAR"),
37
+ },
38
+ ]),
52
39
  ],
53
40
  },
54
41
  {
@@ -56,47 +43,54 @@ const fixture = {
56
43
  name: "Mutation",
57
44
  fields: [
58
45
  field("projectCreate", named("Project"), [
59
- { name: "filter", type: named("Filter", "INPUT_OBJECT") },
46
+ { name: "name", type: required(named("String", "SCALAR")) },
60
47
  ]),
61
48
  ],
62
49
  },
63
50
  {
64
51
  kind: "OBJECT",
65
52
  name: "Project",
66
- interfaces: [named("Node", "INTERFACE")],
67
53
  fields: [
68
54
  field("id", required(named("String", "SCALAR"))),
69
- field("children", required(list(required(named("Project")))), [
70
- {
71
- name: "first",
72
- type: required(named("Int", "SCALAR")),
73
- defaultValue: "20",
74
- },
75
- { name: "filter", type: named("Filter", "INPUT_OBJECT") },
55
+ field("name", required(named("String", "SCALAR"))),
56
+ ],
57
+ },
58
+ {
59
+ kind: "OBJECT",
60
+ name: "User",
61
+ fields: [
62
+ field("email", required(named("String", "SCALAR"))),
63
+ field("name", named("String", "SCALAR")),
64
+ field("projects", required(named("ProjectConnection")), [
65
+ { name: "first", type: named("Int", "SCALAR") },
76
66
  ]),
77
- {
78
- ...field("legacy", named("String", "SCALAR")),
79
- isDeprecated: true,
80
- deprecationReason: "Use id",
81
- },
82
67
  ],
83
68
  },
84
69
  {
85
70
  kind: "OBJECT",
86
- name: "Service",
87
- fields: [field("id", required(named("String", "SCALAR")))],
71
+ name: "ProjectConnection",
72
+ fields: [
73
+ field("edges", required({ kind: "LIST", ofType: named("ProjectEdge") })),
74
+ field("pageInfo", required(named("PageInfo"))),
75
+ ],
88
76
  },
89
77
  {
90
- kind: "INTERFACE",
91
- name: "Node",
92
- fields: [field("id", required(named("String", "SCALAR")))],
93
- possibleTypes: [named("Project")],
78
+ kind: "OBJECT",
79
+ name: "ProjectEdge",
80
+ fields: [
81
+ field("cursor", required(named("String", "SCALAR"))),
82
+ field("node", required(named("Project"))),
83
+ ],
94
84
  },
95
85
  {
96
- kind: "UNION",
97
- name: "Search",
98
- possibleTypes: [named("Project"), named("Service")],
86
+ kind: "OBJECT",
87
+ name: "PageInfo",
88
+ fields: [
89
+ field("endCursor", named("String", "SCALAR")),
90
+ field("hasNextPage", required(named("Boolean", "SCALAR"))),
91
+ ],
99
92
  },
93
+ { kind: "SCALAR", name: "Boolean" },
100
94
  ],
101
95
  };
102
96
 
@@ -104,283 +98,92 @@ const options = {
104
98
  transportImport: "./transport.ts",
105
99
  requirementsImport: "./transport.ts",
106
100
  requirementsType: "Requirements",
107
- operationAliases: { createProject: "projectCreate" },
101
+ sdkName: "Railway",
108
102
  };
109
103
 
110
- describe("GraphQL-native compiler", () => {
111
- test("root helpers allocate safe unique names without hiding schema operations", () => {
112
- const source = buildSchema(`
113
- type Query {
114
- query: String report: String schema: String transport: String G: String
115
- catchTags: String delete: String Scalars: String Selection: String
116
- same: String mutationSame: String
117
- }
118
- type Mutation { same: String }
119
- `);
120
- const model = convertGraphQLClient(introspectionFromSchema(source));
121
- const output = generateGraphQLClient(model, {
122
- ...options,
123
- operationAliases: {
124
- default: "delete",
125
- deleteField: "delete",
126
- transport: "report",
127
- readReport: "report",
128
- },
129
- });
130
- expect(output).toContain(
131
- 'export const queryQuery = client.operation("query", "query");',
132
- );
133
- expect(output).toContain(
134
- 'export const queryReport = client.operation("query", "report");',
135
- );
136
- expect(output).toContain(
137
- 'export const queryTransport = client.operation("query", "transport");',
138
- );
139
- expect(output).toContain(
140
- 'export const queryDelete = client.operation("query", "delete");',
141
- );
142
- expect(output).toContain(
143
- 'export const queryScalars = client.operation("query", "Scalars");',
144
- );
145
- expect(output).toContain(
146
- 'export const mutationSame2 = client.operation("mutation", "same");',
147
- );
148
- expect(output).toContain("export const deleteField = queryDelete;");
149
- expect(output).toContain("export const readReport = queryReport;");
150
- expect(output).not.toContain("export const default =");
151
- expect(
152
- [...output.matchAll(/export const ([A-Za-z_$][\w$]*) =/g)].map(
153
- (match) => match[1],
154
- ),
155
- ).toEqual(expect.arrayContaining(["query", "report", "queryCatchTags"]));
156
- const declarations = [
157
- ...output.matchAll(/export (?:const|class|type) ([A-Za-z_$][\w$]*)/g),
158
- ].map((match) => match[1]);
159
- expect(new Set(declarations).size).toBe(declarations.length);
160
- expect(output.match(/client\.operation\(/g)).toHaveLength(12);
161
- expect(() =>
162
- new Bun.Transpiler({ loader: "ts" }).transformSync(output),
163
- ).not.toThrow();
164
- expect(
165
- generateGraphQLClient(model, {
166
- ...options,
167
- operationAliases: {
168
- default: "delete",
169
- deleteField: "delete",
170
- transport: "report",
171
- readReport: "report",
172
- },
173
- }),
174
- ).toBe(output);
175
- });
176
-
177
- test("schema type names and patched error tags cannot shadow generated support symbols", () => {
178
- const model = convertGraphQLClient(
179
- introspectionFromSchema(
180
- buildSchema(`
181
- type Query { data: Schema }
182
- type Schema { selection: Selection }
183
- type Selection { id: ID! }
184
- `),
185
- ),
186
- );
187
- for (const tag of ["Schema", "catchTags", "class"]) {
188
- applyOperation(model, {
189
- op: "add",
190
- path: `/errors/${tag}`,
191
- value: { matchers: [{ code: tag }] },
192
- });
193
- applyOperation(model, {
194
- op: "add",
195
- path: "/types/Query/fields/data/errors/-",
196
- value: tag,
197
- });
198
- }
104
+ describe("GraphQL Query SDK generator", () => {
105
+ test("emits TypeMeta, interfaces, and named roots on the SDK object", () => {
106
+ const model = convertGraphQLClient(fixture);
199
107
  const output = generateGraphQLClient(model, options);
200
- expect(output).toContain(
201
- 'export class SchemaError extends S.TaggedError<SchemaError>()("Schema", G.errorFields)',
202
- );
203
- expect(output).toContain(
204
- 'export class catchTagsError extends S.TaggedError<catchTagsError>()("catchTags", G.errorFields)',
205
- );
206
- expect(output).toContain(
207
- 'export class classError extends S.TaggedError<classError>()("class", G.errorFields)',
208
- );
209
- expect(output).toContain('"Schema": SchemaError;');
210
- expect(output).toContain('"Schema": {');
211
- expect(output).toContain('"Selection": {');
212
- expect(() =>
213
- new Bun.Transpiler({ loader: "ts" }).transformSync(output),
214
- ).not.toThrow();
108
+ expect(output).toContain("export interface Project");
109
+ expect(output).toContain("export interface User");
110
+ expect(output).toContain('export const Project: TypeMeta = { name: "Project", fields: {} };');
111
+ expect(output).toContain("export const Railway = {");
112
+ expect(output).toContain('root("query", "me", User, undefined, undefined, globalErrors)');
113
+ expect(output).toContain('root("query", "project", Project');
114
+ expect(output).toContain('rootConnection("query", "projects", Project');
115
+ expect(output).toContain("Query<ReadonlyArray<Project>, RailwayGlobalError>");
116
+ expect(output).toContain('connectionField("projects", Project, { first: "Int" })');
117
+ expect(output).toContain('root("mutation", "projectCreate", Project');
118
+ // Nullable GraphQL types include null; non-null ones do not.
119
+ expect(output).toContain("readonly name: string | null;");
120
+ expect(output).toContain("readonly email: string;");
121
+ expect(output).toContain("Query<Project | null, RailwayGlobalError>");
122
+ expect(output).toContain("args?: { readonly first?: number | null }");
123
+ expect(output).toContain('from "@distilled.cloud/core/graphql"');
124
+ expect(() => stripTypeScriptTypes(output)).not.toThrow();
215
125
  });
216
126
 
217
- test("real introspection round-trips abstract fragments, recursive selections and defaulted required arguments", () => {
218
- const schema = buildSchema(
219
- fixtureSDL.replaceAll("first: Int = 20", "first: Int! = 20"),
220
- );
221
- const model = convertGraphQLClient(introspectionFromSchema(schema));
222
- const compiled = compile(model, "query", {
223
- node: {
224
- where: { id: "node-id" },
225
- select: {
226
- id: true,
227
- __typename: true,
228
- __on: {
229
- Project: {
230
- services: { select: { edges: { node: { id: true } } } },
231
- },
232
- Service: { status: true, project: { parent: { name: true } } },
233
- },
127
+ test("emits error classes and scopes them to roots", () => {
128
+ const model = convertGraphQLClient(fixture);
129
+ for (const operation of [
130
+ {
131
+ op: "add" as const,
132
+ path: "/errors/RailwayNotFound",
133
+ value: {
134
+ description: "Missing.",
135
+ category: "notFound",
136
+ matchers: [{ code: "NOT_FOUND" }],
234
137
  },
235
138
  },
236
- search: {
237
- where: { text: "query" },
238
- select: {
239
- __typename: true,
240
- __on: { Project: { name: true }, Service: { status: true } },
139
+ {
140
+ op: "add" as const,
141
+ path: "/errors/RailwayRateLimited",
142
+ value: {
143
+ category: "throttling",
144
+ retryable: true,
145
+ matchers: [{ code: "RATE_LIMITED" }],
241
146
  },
242
147
  },
243
- });
244
- expect(validate(schema, parse(compiled.query))).toEqual([]);
245
- expect(compiled.query).not.toContain("first:");
246
- expect(model.types.Project!.fields!.services!.args.first).toEqual({
247
- type: "Int!",
248
- defaultValue: "20",
249
- });
250
- expect(model.types.ProjectFilter!.inputFields!.statuses).toEqual({
251
- type: "[Status!]",
252
- defaultValue: "[ACTIVE]",
253
- });
254
- const output = generateGraphQLClient(model, options);
255
- expect(output).toContain('"first"?: Scalars["Int"]');
256
- expect(output).toContain('"Node": "Project" | "Service"');
257
- expect(output).toContain('"SearchResult": "Project" | "Service"');
258
- });
259
-
260
- test("required input fields with defaults are omittable but other required fields stay required", () => {
261
- const schema = buildSchema(`
262
- input Paging { limit: Int! = 20, owner: ID!, statuses: [String!]! = [] }
263
- type Query { names(paging: Paging!): [String!]! }
264
- `);
265
- const model = convertGraphQLClient(introspectionFromSchema(schema));
266
- const output = generateGraphQLClient(model, options);
267
- expect(output).toContain('"limit"?: Scalars["Int"]');
268
- expect(output).toContain('"owner": Scalars["ID"]');
269
- expect(output).toContain('"statuses"?: ReadonlyArray<Scalars["String"]>');
270
- const compiled = compile(model, "query", {
271
- names: { where: { paging: { owner: "account" } } },
272
- });
273
- expect(Object.values(compiled.variables)).toEqual([{ owner: "account" }]);
274
- expect(validate(schema, parse(compiled.query))).toEqual([]);
275
- expect(() =>
276
- compile(model, "query", { names: { where: { paging: {} } } }),
277
- ).toThrow("owner is required");
278
- });
279
-
280
- test("provider scalar overrides stay aligned in runtime metadata and generated scalar types", () => {
281
- const schema = buildSchema(
282
- `scalar DateTime scalar JSON type Query { date: DateTime! blob: JSON! }`,
283
- );
284
- const model = convertGraphQLClient(introspectionFromSchema(schema), {
285
- scalars: { DateTime: "string" },
286
- });
287
- const output = generateGraphQLClient(model, options);
288
- expect(model.types.DateTime!.scalar).toBe("string");
289
- expect(model.types.JSON!.scalar).toBe("unknown");
290
- expect(output).toContain('"DateTime": string;');
291
- expect(output).toContain('"JSON": unknown;');
292
- expect(
293
- validate(
294
- schema,
295
- parse(compile(model, "query", { date: true, blob: true }).query),
296
- ),
297
- ).toEqual([]);
298
- });
299
-
300
- test("preserves nested required arguments, defaults, recursion, union/interface members and deprecated fields", () => {
301
- const model = convertGraphQLClient({ data: { __schema: fixture } });
302
- expect(model.types.Project!.fields!.children).toEqual({
303
- type: "[Project!]!",
304
- args: {
305
- first: { type: "Int!", defaultValue: "20" },
306
- filter: { type: "Filter" },
148
+ {
149
+ op: "add" as const,
150
+ path: "/globalErrors/-",
151
+ value: "RailwayRateLimited",
307
152
  },
308
- errors: [],
309
- });
310
- expect(model.types.Project!.interfaces).toEqual(["Node"]);
311
- expect(model.types.Node!.possibleTypes).toEqual(["Project"]);
312
- expect(model.types.Search!.possibleTypes).toEqual(["Project", "Service"]);
313
- expect(model.types.Project!.fields!.legacy!.deprecated).toBe("Use id");
314
- expect(model.types.Status!.enumValues).toEqual(["ACTIVE", "OLD"]);
315
- expect(model.types.Filter!.inputFields!.status!.defaultValue).toBe(
316
- "ACTIVE",
317
- );
318
- expect(model.types.JSON!.scalar).toBe("unknown");
319
- validateGraphQLModel(model);
320
- });
321
-
322
- test("field errors patch the one model used by emitted type unions and runtime classification", () => {
323
- const model = convertGraphQLClient(fixture);
324
- applyOperation(model, {
325
- op: "add",
326
- path: "/errors/ChildrenUnavailable",
327
- value: {
328
- category: "server",
329
- matchers: [{ code: "CHILDREN_UNAVAILABLE" }],
153
+ {
154
+ op: "add" as const,
155
+ path: "/types/Query/fields/project/errors/-",
156
+ value: "RailwayNotFound",
330
157
  },
331
- });
332
- applyOperation(model, {
333
- op: "add",
334
- path: "/types/Project/fields/children/errors/-",
335
- value: "ChildrenUnavailable",
336
- });
158
+ ])
159
+ applyOperation(model, operation);
337
160
  const output = generateGraphQLClient(model, options);
338
161
  expect(output).toContain(
339
- '"children": G.Field<{ "first"?: Scalars["Int"]; "filter"?: Inputs["Filter"] | null }, "[Project!]!", "ChildrenUnavailable">;',
162
+ 'export class RailwayNotFound extends S.TaggedError<RailwayNotFound>()("RailwayNotFound", errorFields).pipe(Category.withNotFoundError) {}',
340
163
  );
341
164
  expect(output).toContain(
342
- 'export class ChildrenUnavailable extends S.TaggedError<ChildrenUnavailable>()("ChildrenUnavailable", G.errorFields).pipe(Category.withServerError)',
165
+ "Category.withThrottlingError, Category.withRetryable({ throttling: true })",
343
166
  );
344
- expect(output).toContain('"errors":["ChildrenUnavailable"]');
345
- expect(output).toContain('"matchers":[{"code":"CHILDREN_UNAVAILABLE"}]');
346
167
  expect(output).toContain(
347
- 'export const projectCreate = client.operation("mutation", "projectCreate");',
168
+ 'errorSpec(RailwayRateLimited, "RailwayRateLimited", [{"code":"RATE_LIMITED"}], { retryable: true, global: true })',
348
169
  );
349
- expect(output).toContain("export const createProject = projectCreate;");
170
+ expect(output).toContain("export type RailwayGlobalError = RailwayRateLimited;");
171
+ expect(output).toContain("Query<Project, RailwayNotFound | RailwayGlobalError>");
172
+ expect(output).toContain("[RailwayNotFoundSpec, ...globalErrors]");
173
+ expect(output).toContain("Query<User, RailwayGlobalError>");
174
+ expect(() => stripTypeScriptTypes(output)).not.toThrow();
350
175
  });
351
176
 
352
- test("generation has no fixed depth projection or artificial output optionality", () => {
353
- const output = generateGraphQLClient(
354
- convertGraphQLClient(fixture),
355
- options,
356
- );
357
- expect(output).toContain('"Project": {');
358
- expect(output).toContain('"Search": "Project" | "Service";');
359
- expect(output).toContain('"Node": "Project";');
360
- expect(output).toContain('"id": G.Field<{ }, "String!", never>;');
361
- expect(output).not.toContain('"id"?: G.Field');
362
- expect(output).not.toContain("maxDepth");
363
- });
364
-
365
- test("unknown coordinates, incomplete references and stale patches fail immediately", () => {
177
+ test("unknown coordinates fail validateGraphQLModel", () => {
366
178
  const model = convertGraphQLClient(fixture);
367
179
  applyOperation(model, {
368
180
  op: "add",
369
- path: "/types/Project/fields/children/errors/-",
181
+ path: "/types/Project/fields/id/errors/-",
370
182
  value: "MissingError",
371
183
  });
372
184
  expect(() => validateGraphQLModel(model)).toThrow(
373
- "Project.children: unknown GraphQL error MissingError",
374
- );
375
- expect(() => graphqlTypeString({ kind: "NON_NULL" })).toThrow(
376
- "Incomplete GraphQL NON_NULL",
185
+ "Project.id: unknown GraphQL error MissingError",
377
186
  );
378
- expect(() =>
379
- applyOperation(model, {
380
- op: "replace",
381
- path: "/types/Project/fields/removed/errors",
382
- value: [],
383
- }),
384
- ).toThrow();
187
+ expect(() => graphqlTypeString({ kind: "NON_NULL" })).toThrow("Incomplete GraphQL NON_NULL");
385
188
  });
386
189
  });