@effect-gql/core 0.1.0 → 1.0.0

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 (164) hide show
  1. package/README.md +100 -0
  2. package/builder/index.cjs +1431 -0
  3. package/builder/index.cjs.map +1 -0
  4. package/builder/index.d.cts +259 -0
  5. package/{dist/builder/pipe-api.d.ts → builder/index.d.ts} +49 -21
  6. package/builder/index.js +1390 -0
  7. package/builder/index.js.map +1 -0
  8. package/index.cjs +3419 -0
  9. package/index.cjs.map +1 -0
  10. package/index.d.cts +523 -0
  11. package/index.d.ts +523 -0
  12. package/index.js +3242 -0
  13. package/index.js.map +1 -0
  14. package/package.json +19 -28
  15. package/schema-builder-Cvdq7Kz_.d.cts +963 -0
  16. package/schema-builder-Cvdq7Kz_.d.ts +963 -0
  17. package/server/index.cjs +1555 -0
  18. package/server/index.cjs.map +1 -0
  19. package/server/index.d.cts +680 -0
  20. package/server/index.d.ts +680 -0
  21. package/server/index.js +1524 -0
  22. package/server/index.js.map +1 -0
  23. package/dist/analyzer-extension.d.ts +0 -105
  24. package/dist/analyzer-extension.d.ts.map +0 -1
  25. package/dist/analyzer-extension.js +0 -137
  26. package/dist/analyzer-extension.js.map +0 -1
  27. package/dist/builder/execute.d.ts +0 -26
  28. package/dist/builder/execute.d.ts.map +0 -1
  29. package/dist/builder/execute.js +0 -104
  30. package/dist/builder/execute.js.map +0 -1
  31. package/dist/builder/field-builders.d.ts +0 -30
  32. package/dist/builder/field-builders.d.ts.map +0 -1
  33. package/dist/builder/field-builders.js +0 -200
  34. package/dist/builder/field-builders.js.map +0 -1
  35. package/dist/builder/index.d.ts +0 -7
  36. package/dist/builder/index.d.ts.map +0 -1
  37. package/dist/builder/index.js +0 -31
  38. package/dist/builder/index.js.map +0 -1
  39. package/dist/builder/pipe-api.d.ts.map +0 -1
  40. package/dist/builder/pipe-api.js +0 -151
  41. package/dist/builder/pipe-api.js.map +0 -1
  42. package/dist/builder/schema-builder.d.ts +0 -301
  43. package/dist/builder/schema-builder.d.ts.map +0 -1
  44. package/dist/builder/schema-builder.js +0 -566
  45. package/dist/builder/schema-builder.js.map +0 -1
  46. package/dist/builder/type-registry.d.ts +0 -80
  47. package/dist/builder/type-registry.d.ts.map +0 -1
  48. package/dist/builder/type-registry.js +0 -505
  49. package/dist/builder/type-registry.js.map +0 -1
  50. package/dist/builder/types.d.ts +0 -283
  51. package/dist/builder/types.d.ts.map +0 -1
  52. package/dist/builder/types.js +0 -3
  53. package/dist/builder/types.js.map +0 -1
  54. package/dist/cli/generate-schema.d.ts +0 -29
  55. package/dist/cli/generate-schema.d.ts.map +0 -1
  56. package/dist/cli/generate-schema.js +0 -233
  57. package/dist/cli/generate-schema.js.map +0 -1
  58. package/dist/cli/index.d.ts +0 -19
  59. package/dist/cli/index.d.ts.map +0 -1
  60. package/dist/cli/index.js +0 -24
  61. package/dist/cli/index.js.map +0 -1
  62. package/dist/context.d.ts +0 -18
  63. package/dist/context.d.ts.map +0 -1
  64. package/dist/context.js +0 -11
  65. package/dist/context.js.map +0 -1
  66. package/dist/error.d.ts +0 -45
  67. package/dist/error.d.ts.map +0 -1
  68. package/dist/error.js +0 -29
  69. package/dist/error.js.map +0 -1
  70. package/dist/extensions.d.ts +0 -130
  71. package/dist/extensions.d.ts.map +0 -1
  72. package/dist/extensions.js +0 -78
  73. package/dist/extensions.js.map +0 -1
  74. package/dist/index.d.ts +0 -12
  75. package/dist/index.d.ts.map +0 -1
  76. package/dist/index.js +0 -47
  77. package/dist/index.js.map +0 -1
  78. package/dist/loader.d.ts +0 -169
  79. package/dist/loader.d.ts.map +0 -1
  80. package/dist/loader.js +0 -237
  81. package/dist/loader.js.map +0 -1
  82. package/dist/resolver-context.d.ts +0 -154
  83. package/dist/resolver-context.d.ts.map +0 -1
  84. package/dist/resolver-context.js +0 -184
  85. package/dist/resolver-context.js.map +0 -1
  86. package/dist/schema-mapping.d.ts +0 -30
  87. package/dist/schema-mapping.d.ts.map +0 -1
  88. package/dist/schema-mapping.js +0 -280
  89. package/dist/schema-mapping.js.map +0 -1
  90. package/dist/server/cache-control.d.ts +0 -96
  91. package/dist/server/cache-control.d.ts.map +0 -1
  92. package/dist/server/cache-control.js +0 -308
  93. package/dist/server/cache-control.js.map +0 -1
  94. package/dist/server/complexity.d.ts +0 -165
  95. package/dist/server/complexity.d.ts.map +0 -1
  96. package/dist/server/complexity.js +0 -433
  97. package/dist/server/complexity.js.map +0 -1
  98. package/dist/server/config.d.ts +0 -66
  99. package/dist/server/config.d.ts.map +0 -1
  100. package/dist/server/config.js +0 -104
  101. package/dist/server/config.js.map +0 -1
  102. package/dist/server/graphiql.d.ts +0 -5
  103. package/dist/server/graphiql.d.ts.map +0 -1
  104. package/dist/server/graphiql.js +0 -43
  105. package/dist/server/graphiql.js.map +0 -1
  106. package/dist/server/index.d.ts +0 -18
  107. package/dist/server/index.d.ts.map +0 -1
  108. package/dist/server/index.js +0 -48
  109. package/dist/server/index.js.map +0 -1
  110. package/dist/server/router.d.ts +0 -79
  111. package/dist/server/router.d.ts.map +0 -1
  112. package/dist/server/router.js +0 -232
  113. package/dist/server/router.js.map +0 -1
  114. package/dist/server/schema-builder-extensions.d.ts +0 -42
  115. package/dist/server/schema-builder-extensions.d.ts.map +0 -1
  116. package/dist/server/schema-builder-extensions.js +0 -48
  117. package/dist/server/schema-builder-extensions.js.map +0 -1
  118. package/dist/server/sse-adapter.d.ts +0 -64
  119. package/dist/server/sse-adapter.d.ts.map +0 -1
  120. package/dist/server/sse-adapter.js +0 -227
  121. package/dist/server/sse-adapter.js.map +0 -1
  122. package/dist/server/sse-types.d.ts +0 -192
  123. package/dist/server/sse-types.d.ts.map +0 -1
  124. package/dist/server/sse-types.js +0 -63
  125. package/dist/server/sse-types.js.map +0 -1
  126. package/dist/server/ws-adapter.d.ts +0 -39
  127. package/dist/server/ws-adapter.d.ts.map +0 -1
  128. package/dist/server/ws-adapter.js +0 -247
  129. package/dist/server/ws-adapter.js.map +0 -1
  130. package/dist/server/ws-types.d.ts +0 -169
  131. package/dist/server/ws-types.d.ts.map +0 -1
  132. package/dist/server/ws-types.js +0 -11
  133. package/dist/server/ws-types.js.map +0 -1
  134. package/dist/server/ws-utils.d.ts +0 -42
  135. package/dist/server/ws-utils.d.ts.map +0 -1
  136. package/dist/server/ws-utils.js +0 -99
  137. package/dist/server/ws-utils.js.map +0 -1
  138. package/src/analyzer-extension.ts +0 -254
  139. package/src/builder/execute.ts +0 -153
  140. package/src/builder/field-builders.ts +0 -322
  141. package/src/builder/index.ts +0 -48
  142. package/src/builder/pipe-api.ts +0 -312
  143. package/src/builder/schema-builder.ts +0 -970
  144. package/src/builder/type-registry.ts +0 -670
  145. package/src/builder/types.ts +0 -305
  146. package/src/context.ts +0 -23
  147. package/src/error.ts +0 -32
  148. package/src/extensions.ts +0 -240
  149. package/src/index.ts +0 -32
  150. package/src/loader.ts +0 -363
  151. package/src/resolver-context.ts +0 -253
  152. package/src/schema-mapping.ts +0 -307
  153. package/src/server/cache-control.ts +0 -590
  154. package/src/server/complexity.ts +0 -774
  155. package/src/server/config.ts +0 -174
  156. package/src/server/graphiql.ts +0 -38
  157. package/src/server/index.ts +0 -96
  158. package/src/server/router.ts +0 -432
  159. package/src/server/schema-builder-extensions.ts +0 -51
  160. package/src/server/sse-adapter.ts +0 -327
  161. package/src/server/sse-types.ts +0 -234
  162. package/src/server/ws-adapter.ts +0 -355
  163. package/src/server/ws-types.ts +0 -192
  164. package/src/server/ws-utils.ts +0 -136
@@ -0,0 +1,1431 @@
1
+ 'use strict';
2
+
3
+ var graphql = require('graphql');
4
+ var effect = require('effect');
5
+ var S2 = require('effect/Schema');
6
+ var AST = require('effect/SchemaAST');
7
+
8
+ function _interopNamespace(e) {
9
+ if (e && e.__esModule) return e;
10
+ var n = Object.create(null);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n.default = e;
23
+ return Object.freeze(n);
24
+ }
25
+
26
+ var S2__namespace = /*#__PURE__*/_interopNamespace(S2);
27
+ var AST__namespace = /*#__PURE__*/_interopNamespace(AST);
28
+
29
+ // src/builder/index.ts
30
+ var isIntegerType = (ast) => {
31
+ if (ast._tag === "Refinement") {
32
+ const refinement = ast;
33
+ const annotations = refinement.annotations;
34
+ if (annotations) {
35
+ const identifier = AST__namespace.getIdentifierAnnotation(refinement);
36
+ if (identifier._tag === "Some" && identifier.value === "Int") {
37
+ return true;
38
+ }
39
+ }
40
+ return isIntegerType(refinement.from);
41
+ }
42
+ return false;
43
+ };
44
+ var toGraphQLType = (schema) => {
45
+ const ast = schema.ast;
46
+ if (ast._tag === "StringKeyword") return graphql.GraphQLString;
47
+ if (ast._tag === "NumberKeyword") return graphql.GraphQLFloat;
48
+ if (ast._tag === "BooleanKeyword") return graphql.GraphQLBoolean;
49
+ if (ast._tag === "Refinement") {
50
+ if (isIntegerType(ast)) {
51
+ return graphql.GraphQLInt;
52
+ }
53
+ return toGraphQLType(S2__namespace.make(ast.from));
54
+ }
55
+ if (ast._tag === "Literal") {
56
+ if (typeof ast.literal === "string") return graphql.GraphQLString;
57
+ if (typeof ast.literal === "number") {
58
+ return Number.isInteger(ast.literal) ? graphql.GraphQLInt : graphql.GraphQLFloat;
59
+ }
60
+ if (typeof ast.literal === "boolean") return graphql.GraphQLBoolean;
61
+ }
62
+ if (ast._tag === "TupleType") {
63
+ const elements = ast.elements;
64
+ if (elements.length > 0) {
65
+ const elementSchema = S2__namespace.make(elements[0].type);
66
+ return new graphql.GraphQLList(toGraphQLType(elementSchema));
67
+ }
68
+ }
69
+ if (ast._tag === "TypeLiteral") {
70
+ const fields = {};
71
+ for (const field2 of ast.propertySignatures) {
72
+ const fieldName = String(field2.name);
73
+ const fieldSchema = S2__namespace.make(field2.type);
74
+ let fieldType = toGraphQLType(fieldSchema);
75
+ if (!field2.isOptional) {
76
+ fieldType = new graphql.GraphQLNonNull(fieldType);
77
+ }
78
+ fields[fieldName] = { type: fieldType };
79
+ }
80
+ const typeName = schema.annotations?.identifier || `Object_${Math.random().toString(36).slice(2, 11)}`;
81
+ return new graphql.GraphQLObjectType({
82
+ name: typeName,
83
+ fields
84
+ });
85
+ }
86
+ if (ast._tag === "Transformation") {
87
+ return toGraphQLType(S2__namespace.make(ast.to));
88
+ }
89
+ if (ast._tag === "Union") {
90
+ const types = ast.types;
91
+ if (types.length > 0) {
92
+ return toGraphQLType(S2__namespace.make(types[0]));
93
+ }
94
+ }
95
+ if (ast._tag === "Suspend") {
96
+ const innerAst = ast.f();
97
+ return toGraphQLType(S2__namespace.make(innerAst));
98
+ }
99
+ return graphql.GraphQLString;
100
+ };
101
+ var toGraphQLInputType = (schema) => {
102
+ const ast = schema.ast;
103
+ if (ast._tag === "StringKeyword") return graphql.GraphQLString;
104
+ if (ast._tag === "NumberKeyword") return graphql.GraphQLFloat;
105
+ if (ast._tag === "BooleanKeyword") return graphql.GraphQLBoolean;
106
+ if (ast._tag === "Refinement") {
107
+ if (isIntegerType(ast)) {
108
+ return graphql.GraphQLInt;
109
+ }
110
+ return toGraphQLInputType(S2__namespace.make(ast.from));
111
+ }
112
+ if (ast._tag === "Literal") {
113
+ if (typeof ast.literal === "string") return graphql.GraphQLString;
114
+ if (typeof ast.literal === "number") {
115
+ return Number.isInteger(ast.literal) ? graphql.GraphQLInt : graphql.GraphQLFloat;
116
+ }
117
+ if (typeof ast.literal === "boolean") return graphql.GraphQLBoolean;
118
+ }
119
+ if (ast._tag === "TupleType") {
120
+ const elements = ast.elements;
121
+ if (elements.length > 0) {
122
+ const elementSchema = S2__namespace.make(elements[0].type);
123
+ return new graphql.GraphQLList(toGraphQLInputType(elementSchema));
124
+ }
125
+ }
126
+ if (ast._tag === "TypeLiteral") {
127
+ const fields = {};
128
+ for (const field2 of ast.propertySignatures) {
129
+ const fieldName = String(field2.name);
130
+ const fieldSchema = S2__namespace.make(field2.type);
131
+ let fieldType = toGraphQLInputType(fieldSchema);
132
+ if (!field2.isOptional) {
133
+ fieldType = new graphql.GraphQLNonNull(fieldType);
134
+ }
135
+ fields[fieldName] = { type: fieldType };
136
+ }
137
+ const typeName = schema.annotations?.identifier || `Input_${Math.random().toString(36).slice(2, 11)}`;
138
+ return new graphql.GraphQLInputObjectType({
139
+ name: typeName,
140
+ fields
141
+ });
142
+ }
143
+ if (ast._tag === "Transformation") {
144
+ return toGraphQLInputType(S2__namespace.make(ast.from));
145
+ }
146
+ if (ast._tag === "Union") {
147
+ const types = ast.types;
148
+ if (types.length > 0) {
149
+ return toGraphQLInputType(S2__namespace.make(types[0]));
150
+ }
151
+ }
152
+ if (ast._tag === "Suspend") {
153
+ const innerAst = ast.f();
154
+ return toGraphQLInputType(S2__namespace.make(innerAst));
155
+ }
156
+ return graphql.GraphQLString;
157
+ };
158
+ var toGraphQLArgs = (schema) => {
159
+ const ast = schema.ast;
160
+ if (ast._tag === "TypeLiteral") {
161
+ const args = {};
162
+ for (const field2 of ast.propertySignatures) {
163
+ const fieldName = String(field2.name);
164
+ const fieldSchema = S2__namespace.make(field2.type);
165
+ let fieldType = toGraphQLInputType(fieldSchema);
166
+ if (!field2.isOptional) {
167
+ fieldType = new graphql.GraphQLNonNull(fieldType);
168
+ }
169
+ args[fieldName] = { type: fieldType };
170
+ }
171
+ return args;
172
+ }
173
+ throw new Error(`Schema must be an object type to convert to GraphQL arguments`);
174
+ };
175
+
176
+ // src/builder/type-registry.ts
177
+ function getSchemaName(schema) {
178
+ const ast = schema.ast;
179
+ if (ast._tag === "Transformation") {
180
+ const identifier = AST__namespace.getIdentifierAnnotation(ast.to);
181
+ if (identifier._tag === "Some") {
182
+ return identifier.value;
183
+ }
184
+ }
185
+ if (ast._tag === "TypeLiteral") {
186
+ const tagProp = ast.propertySignatures.find((p) => String(p.name) === "_tag");
187
+ if (tagProp && tagProp.type._tag === "Literal" && typeof tagProp.type.literal === "string") {
188
+ return tagProp.type.literal;
189
+ }
190
+ }
191
+ return void 0;
192
+ }
193
+ function buildReverseLookups(ctx) {
194
+ if (!ctx.schemaToTypeName) {
195
+ ctx.schemaToTypeName = /* @__PURE__ */ new Map();
196
+ ctx.astToTypeName = /* @__PURE__ */ new Map();
197
+ for (const [typeName, typeReg] of ctx.types) {
198
+ ctx.schemaToTypeName.set(typeReg.schema, typeName);
199
+ ctx.astToTypeName.set(typeReg.schema.ast, typeName);
200
+ }
201
+ }
202
+ if (!ctx.schemaToInterfaceName) {
203
+ ctx.schemaToInterfaceName = /* @__PURE__ */ new Map();
204
+ ctx.astToInterfaceName = /* @__PURE__ */ new Map();
205
+ for (const [interfaceName, interfaceReg] of ctx.interfaces) {
206
+ ctx.schemaToInterfaceName.set(interfaceReg.schema, interfaceName);
207
+ ctx.astToInterfaceName.set(interfaceReg.schema.ast, interfaceName);
208
+ }
209
+ }
210
+ if (!ctx.schemaToInputName) {
211
+ ctx.schemaToInputName = /* @__PURE__ */ new Map();
212
+ ctx.astToInputName = /* @__PURE__ */ new Map();
213
+ for (const [inputName, inputReg] of ctx.inputs) {
214
+ ctx.schemaToInputName.set(inputReg.schema, inputName);
215
+ ctx.astToInputName.set(inputReg.schema.ast, inputName);
216
+ }
217
+ }
218
+ if (!ctx.enumSortedValues) {
219
+ ctx.enumSortedValues = /* @__PURE__ */ new Map();
220
+ ctx.literalToEnumName = /* @__PURE__ */ new Map();
221
+ for (const [enumName, enumReg] of ctx.enums) {
222
+ ctx.enumSortedValues.set(enumName, [...enumReg.values].sort());
223
+ for (const value of enumReg.values) {
224
+ ctx.literalToEnumName.set(value, enumName);
225
+ }
226
+ }
227
+ }
228
+ if (!ctx.unionSortedTypes) {
229
+ ctx.unionSortedTypes = /* @__PURE__ */ new Map();
230
+ for (const [unionName, unionReg] of ctx.unions) {
231
+ ctx.unionSortedTypes.set(unionName, [...unionReg.types].sort());
232
+ }
233
+ }
234
+ }
235
+ var nonNullCache = /* @__PURE__ */ new WeakMap();
236
+ function getNonNull(type) {
237
+ let cached = nonNullCache.get(type);
238
+ if (!cached) {
239
+ cached = new graphql.GraphQLNonNull(type);
240
+ nonNullCache.set(type, cached);
241
+ }
242
+ return cached;
243
+ }
244
+ function toGraphQLTypeWithRegistry(schema, ctx) {
245
+ buildReverseLookups(ctx);
246
+ const ast = schema.ast;
247
+ const registeredType = findRegisteredType(schema, ast, ctx);
248
+ if (registeredType) return registeredType;
249
+ const registeredInterface = findRegisteredInterface(schema, ast, ctx);
250
+ if (registeredInterface) return registeredInterface;
251
+ if (ast._tag === "Transformation") {
252
+ return handleTransformationAST(ast, ctx);
253
+ }
254
+ if (ast._tag === "Union") {
255
+ return handleUnionAST(ast, ctx);
256
+ }
257
+ if (ast._tag === "Literal") {
258
+ const enumType2 = findEnumForLiteral(ast, ctx);
259
+ if (enumType2) return enumType2;
260
+ }
261
+ if (ast._tag === "TupleType") {
262
+ return handleTupleTypeAST(ast, ctx);
263
+ }
264
+ if (ast._tag === "Suspend") {
265
+ const innerAst = ast.f();
266
+ return toGraphQLTypeWithRegistry(S2__namespace.make(innerAst), ctx);
267
+ }
268
+ return toGraphQLType(schema);
269
+ }
270
+ function findRegisteredType(schema, ast, ctx) {
271
+ const typeName = ctx.schemaToTypeName?.get(schema) ?? ctx.astToTypeName?.get(ast);
272
+ if (typeName) {
273
+ return ctx.typeRegistry.get(typeName);
274
+ }
275
+ return void 0;
276
+ }
277
+ function findRegisteredInterface(schema, ast, ctx) {
278
+ const interfaceName = ctx.schemaToInterfaceName?.get(schema) ?? ctx.astToInterfaceName?.get(ast);
279
+ if (interfaceName) {
280
+ return ctx.interfaceRegistry.get(interfaceName);
281
+ }
282
+ return void 0;
283
+ }
284
+ function handleTransformationAST(ast, ctx) {
285
+ const toAst = ast.to;
286
+ if (toAst._tag === "TupleType") {
287
+ if (toAst.rest && toAst.rest.length > 0) {
288
+ const elementSchema = S2__namespace.make(toAst.rest[0].type);
289
+ const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
290
+ return new graphql.GraphQLList(elementType);
291
+ } else if (toAst.elements.length > 0) {
292
+ const elementSchema = S2__namespace.make(toAst.elements[0].type);
293
+ const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
294
+ return new graphql.GraphQLList(elementType);
295
+ }
296
+ }
297
+ return toGraphQLTypeWithRegistry(S2__namespace.make(ast.to), ctx);
298
+ }
299
+ function handleUnionAST(ast, ctx) {
300
+ const allLiterals = ast.types.every((t) => t._tag === "Literal");
301
+ if (allLiterals) {
302
+ const enumType2 = findEnumForLiteralUnion(ast.types, ctx);
303
+ if (enumType2) return enumType2;
304
+ } else {
305
+ const unionType2 = findRegisteredUnion(ast.types, ctx);
306
+ if (unionType2) return unionType2;
307
+ }
308
+ if (ast.types.length > 0) {
309
+ return toGraphQLTypeWithRegistry(S2__namespace.make(ast.types[0]), ctx);
310
+ }
311
+ return toGraphQLType(S2__namespace.make(ast));
312
+ }
313
+ function findEnumForLiteralUnion(types, ctx) {
314
+ const literalValues = types.map((t) => String(t.literal)).sort();
315
+ for (const [enumName] of ctx.enums) {
316
+ const enumValues = ctx.enumSortedValues?.get(enumName);
317
+ if (enumValues && literalValues.length === enumValues.length && literalValues.every((v, i) => v === enumValues[i])) {
318
+ return ctx.enumRegistry.get(enumName);
319
+ }
320
+ }
321
+ return void 0;
322
+ }
323
+ function findRegisteredUnion(types, ctx) {
324
+ const memberTags = [];
325
+ for (const memberAst of types) {
326
+ if (memberAst._tag === "TypeLiteral") {
327
+ const tagProp = memberAst.propertySignatures.find((p) => String(p.name) === "_tag");
328
+ if (tagProp && tagProp.type._tag === "Literal") {
329
+ memberTags.push(String(tagProp.type.literal));
330
+ }
331
+ }
332
+ }
333
+ if (memberTags.length === types.length) {
334
+ const sortedTags = memberTags.sort();
335
+ for (const [unionName] of ctx.unions) {
336
+ const unionTypes = ctx.unionSortedTypes?.get(unionName);
337
+ if (unionTypes && sortedTags.length === unionTypes.length && sortedTags.every((tag, i) => tag === unionTypes[i])) {
338
+ return ctx.unionRegistry.get(unionName);
339
+ }
340
+ }
341
+ }
342
+ return void 0;
343
+ }
344
+ function findEnumForLiteral(ast, ctx) {
345
+ const literalValue = String(ast.literal);
346
+ const enumName = ctx.literalToEnumName?.get(literalValue);
347
+ if (enumName) {
348
+ return ctx.enumRegistry.get(enumName);
349
+ }
350
+ return void 0;
351
+ }
352
+ function handleTupleTypeAST(ast, ctx) {
353
+ if (ast.rest && ast.rest.length > 0) {
354
+ const elementSchema = S2__namespace.make(ast.rest[0].type);
355
+ const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
356
+ return new graphql.GraphQLList(elementType);
357
+ } else if (ast.elements && ast.elements.length > 0) {
358
+ const elementSchema = S2__namespace.make(ast.elements[0].type);
359
+ const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
360
+ return new graphql.GraphQLList(elementType);
361
+ }
362
+ return toGraphQLType(S2__namespace.make(ast));
363
+ }
364
+ function schemaToFields(schema, ctx) {
365
+ let ast = schema.ast;
366
+ if (ast._tag === "Transformation") {
367
+ ast = ast.to;
368
+ }
369
+ if (ast._tag === "Declaration") {
370
+ const typeParams = ast.typeParameters;
371
+ if (typeParams && typeParams.length > 0 && typeParams[0]._tag === "TypeLiteral") {
372
+ ast = typeParams[0];
373
+ }
374
+ }
375
+ if (ast._tag === "TypeLiteral") {
376
+ const fields = {};
377
+ for (const field2 of ast.propertySignatures) {
378
+ const fieldName = String(field2.name);
379
+ const fieldSchema = S2__namespace.make(field2.type);
380
+ let fieldType = toGraphQLTypeWithRegistry(fieldSchema, ctx);
381
+ if (!field2.isOptional) {
382
+ fieldType = getNonNull(fieldType);
383
+ }
384
+ fields[fieldName] = { type: fieldType };
385
+ }
386
+ return fields;
387
+ }
388
+ return {};
389
+ }
390
+ function schemaToInputFields(schema, enumRegistry, inputRegistry, inputs, enums, cache) {
391
+ const ast = schema.ast;
392
+ if (ast._tag === "TypeLiteral") {
393
+ const fields = {};
394
+ for (const field2 of ast.propertySignatures) {
395
+ const fieldName = String(field2.name);
396
+ const fieldSchema = S2__namespace.make(field2.type);
397
+ let fieldType = toGraphQLInputTypeWithRegistry(
398
+ fieldSchema,
399
+ enumRegistry,
400
+ inputRegistry,
401
+ inputs,
402
+ enums,
403
+ cache
404
+ );
405
+ if (!field2.isOptional) {
406
+ fieldType = getNonNull(fieldType);
407
+ }
408
+ fields[fieldName] = { type: fieldType };
409
+ }
410
+ return fields;
411
+ }
412
+ return {};
413
+ }
414
+ function buildInputTypeLookupCache(inputs, enums) {
415
+ const cache = {
416
+ schemaToInputName: /* @__PURE__ */ new Map(),
417
+ astToInputName: /* @__PURE__ */ new Map(),
418
+ literalToEnumName: /* @__PURE__ */ new Map(),
419
+ enumSortedValues: /* @__PURE__ */ new Map()
420
+ };
421
+ for (const [inputName, inputReg] of inputs) {
422
+ cache.schemaToInputName.set(inputReg.schema, inputName);
423
+ cache.astToInputName.set(inputReg.schema.ast, inputName);
424
+ }
425
+ for (const [enumName, enumReg] of enums) {
426
+ cache.enumSortedValues.set(enumName, [...enumReg.values].sort());
427
+ for (const value of enumReg.values) {
428
+ cache.literalToEnumName.set(value, enumName);
429
+ }
430
+ }
431
+ return cache;
432
+ }
433
+ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inputs, enums, cache) {
434
+ const ast = schema.ast;
435
+ if (ast._tag === "Transformation") {
436
+ const toAst = ast.to;
437
+ return toGraphQLInputTypeWithRegistry(
438
+ S2__namespace.make(toAst),
439
+ enumRegistry,
440
+ inputRegistry,
441
+ inputs,
442
+ enums,
443
+ cache
444
+ );
445
+ }
446
+ if (cache?.schemaToInputName || cache?.astToInputName) {
447
+ const inputName = cache.schemaToInputName?.get(schema) ?? cache.astToInputName?.get(ast);
448
+ if (inputName) {
449
+ const result = inputRegistry.get(inputName);
450
+ if (result) return result;
451
+ }
452
+ } else {
453
+ for (const [inputName, inputReg] of inputs) {
454
+ if (inputReg.schema.ast === ast || inputReg.schema === schema) {
455
+ const result = inputRegistry.get(inputName);
456
+ if (result) return result;
457
+ }
458
+ }
459
+ }
460
+ if (ast._tag === "Union") {
461
+ const unionAst = ast;
462
+ const nonUndefinedTypes = unionAst.types.filter((t) => t._tag !== "UndefinedKeyword");
463
+ if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "Union") {
464
+ return toGraphQLInputTypeWithRegistry(
465
+ S2__namespace.make(nonUndefinedTypes[0]),
466
+ enumRegistry,
467
+ inputRegistry,
468
+ inputs,
469
+ enums,
470
+ cache
471
+ );
472
+ }
473
+ if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "TypeLiteral") {
474
+ return toGraphQLInputTypeWithRegistry(
475
+ S2__namespace.make(nonUndefinedTypes[0]),
476
+ enumRegistry,
477
+ inputRegistry,
478
+ inputs,
479
+ enums,
480
+ cache
481
+ );
482
+ }
483
+ const allLiterals = unionAst.types.every((t) => t._tag === "Literal");
484
+ if (allLiterals) {
485
+ const literalValues = unionAst.types.map((t) => String(t.literal)).sort();
486
+ for (const [enumName] of enums) {
487
+ const enumValues = cache?.enumSortedValues?.get(enumName) ?? [...enums.get(enumName).values].sort();
488
+ if (literalValues.length === enumValues.length && literalValues.every((v, i) => v === enumValues[i])) {
489
+ const result = enumRegistry.get(enumName);
490
+ if (result) return result;
491
+ }
492
+ }
493
+ }
494
+ }
495
+ if (ast._tag === "Literal") {
496
+ const literalValue = String(ast.literal);
497
+ if (cache?.literalToEnumName) {
498
+ const enumName = cache.literalToEnumName.get(literalValue);
499
+ if (enumName) {
500
+ const result = enumRegistry.get(enumName);
501
+ if (result) return result;
502
+ }
503
+ } else {
504
+ for (const [enumName, enumReg] of enums) {
505
+ if (enumReg.values.includes(literalValue)) {
506
+ const result = enumRegistry.get(enumName);
507
+ if (result) return result;
508
+ }
509
+ }
510
+ }
511
+ }
512
+ if (ast._tag === "Suspend") {
513
+ const innerAst = ast.f();
514
+ return toGraphQLInputTypeWithRegistry(
515
+ S2__namespace.make(innerAst),
516
+ enumRegistry,
517
+ inputRegistry,
518
+ inputs,
519
+ enums,
520
+ cache
521
+ );
522
+ }
523
+ return toGraphQLInputType(schema);
524
+ }
525
+ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs, enums, cache) {
526
+ const ast = schema.ast;
527
+ if (ast._tag === "TypeLiteral") {
528
+ const args = {};
529
+ for (const field2 of ast.propertySignatures) {
530
+ const fieldName = String(field2.name);
531
+ const fieldSchema = S2__namespace.make(field2.type);
532
+ let fieldType = toGraphQLInputTypeWithRegistry(
533
+ fieldSchema,
534
+ enumRegistry,
535
+ inputRegistry,
536
+ inputs,
537
+ enums,
538
+ cache
539
+ );
540
+ if (!field2.isOptional) {
541
+ fieldType = getNonNull(fieldType);
542
+ }
543
+ args[fieldName] = { type: fieldType };
544
+ }
545
+ return args;
546
+ }
547
+ return toGraphQLArgs(schema);
548
+ }
549
+ function applyDirectives(effect, directives, directiveRegistrations) {
550
+ if (!directives) return effect;
551
+ let wrapped = effect;
552
+ for (const directiveApp of directives) {
553
+ const directiveReg = directiveRegistrations.get(directiveApp.name);
554
+ if (directiveReg?.apply) {
555
+ wrapped = directiveReg.apply(directiveApp.args ?? {})(wrapped);
556
+ }
557
+ }
558
+ return wrapped;
559
+ }
560
+ function applyMiddleware(effect, context, middlewares) {
561
+ if (middlewares.length === 0) return effect;
562
+ let wrapped = effect;
563
+ for (let i = middlewares.length - 1; i >= 0; i--) {
564
+ const middleware2 = middlewares[i];
565
+ if (middleware2.match && !middleware2.match(context.info)) {
566
+ continue;
567
+ }
568
+ wrapped = middleware2.apply(wrapped, context);
569
+ }
570
+ return wrapped;
571
+ }
572
+ function buildField(config, ctx) {
573
+ const fieldConfig = {
574
+ type: toGraphQLTypeWithRegistry(config.type, ctx),
575
+ resolve: async (_parent, args, context, info) => {
576
+ let effect$1 = applyDirectives(
577
+ config.resolve(args),
578
+ config.directives,
579
+ ctx.directiveRegistrations
580
+ );
581
+ const middlewareContext = { parent: _parent, args, info };
582
+ effect$1 = applyMiddleware(effect$1, middlewareContext, ctx.middlewares);
583
+ return await effect.Runtime.runPromise(context.runtime)(effect$1);
584
+ }
585
+ };
586
+ if (config.args) {
587
+ fieldConfig.args = toGraphQLArgsWithRegistry(
588
+ config.args,
589
+ ctx.enumRegistry,
590
+ ctx.inputRegistry,
591
+ ctx.inputs,
592
+ ctx.enums,
593
+ ctx.inputTypeLookupCache
594
+ );
595
+ }
596
+ if (config.description) {
597
+ fieldConfig.description = config.description;
598
+ }
599
+ return fieldConfig;
600
+ }
601
+ function buildObjectField(config, ctx) {
602
+ const fieldConfig = {
603
+ type: toGraphQLTypeWithRegistry(config.type, ctx),
604
+ resolve: async (parent, args, context, info) => {
605
+ let effect$1 = applyDirectives(
606
+ config.resolve(parent, args),
607
+ config.directives,
608
+ ctx.directiveRegistrations
609
+ );
610
+ const middlewareContext = { parent, args, info };
611
+ effect$1 = applyMiddleware(effect$1, middlewareContext, ctx.middlewares);
612
+ return await effect.Runtime.runPromise(context.runtime)(effect$1);
613
+ }
614
+ };
615
+ if (config.args) {
616
+ fieldConfig.args = toGraphQLArgsWithRegistry(
617
+ config.args,
618
+ ctx.enumRegistry,
619
+ ctx.inputRegistry,
620
+ ctx.inputs,
621
+ ctx.enums,
622
+ ctx.inputTypeLookupCache
623
+ );
624
+ }
625
+ if (config.description) {
626
+ fieldConfig.description = config.description;
627
+ }
628
+ return fieldConfig;
629
+ }
630
+ function buildSubscriptionField(config, ctx) {
631
+ const fieldConfig = {
632
+ type: toGraphQLTypeWithRegistry(config.type, ctx),
633
+ // The subscribe function returns an AsyncIterator
634
+ subscribe: async (_parent, args, context, info) => {
635
+ let subscribeEffect = config.subscribe(args);
636
+ subscribeEffect = applyDirectives(
637
+ subscribeEffect,
638
+ config.directives,
639
+ ctx.directiveRegistrations
640
+ );
641
+ const middlewareContext = { parent: _parent, args, info };
642
+ subscribeEffect = applyMiddleware(subscribeEffect, middlewareContext, ctx.middlewares);
643
+ const stream = await effect.Runtime.runPromise(context.runtime)(subscribeEffect);
644
+ return streamToAsyncIterator(stream, context.runtime);
645
+ },
646
+ // The resolve function transforms each yielded value
647
+ // If no custom resolve is provided, return the payload directly
648
+ resolve: config.resolve ? async (value, args, context, info) => {
649
+ let effect$1 = config.resolve(value, args);
650
+ const middlewareContext = { parent: value, args, info };
651
+ effect$1 = applyMiddleware(effect$1, middlewareContext, ctx.middlewares);
652
+ return await effect.Runtime.runPromise(context.runtime)(effect$1);
653
+ } : (value) => value
654
+ };
655
+ if (config.args) {
656
+ fieldConfig.args = toGraphQLArgsWithRegistry(
657
+ config.args,
658
+ ctx.enumRegistry,
659
+ ctx.inputRegistry,
660
+ ctx.inputs,
661
+ ctx.enums,
662
+ ctx.inputTypeLookupCache
663
+ );
664
+ }
665
+ if (config.description) {
666
+ fieldConfig.description = config.description;
667
+ }
668
+ return fieldConfig;
669
+ }
670
+ function streamToAsyncIterator(stream, runtime) {
671
+ let queue;
672
+ let fiber;
673
+ let initialized = false;
674
+ let done = false;
675
+ const initialize = async () => {
676
+ if (initialized) return;
677
+ initialized = true;
678
+ queue = await effect.Runtime.runPromise(runtime)(effect.Queue.unbounded());
679
+ fiber = effect.Runtime.runFork(runtime)(
680
+ effect.Effect.ensuring(
681
+ effect.Stream.runForEach(stream, (value) => effect.Queue.offer(queue, effect.Option.some(value))),
682
+ // Signal completion by pushing None
683
+ effect.Queue.offer(queue, effect.Option.none())
684
+ )
685
+ );
686
+ };
687
+ return {
688
+ [Symbol.asyncIterator]() {
689
+ return this;
690
+ },
691
+ async next() {
692
+ await initialize();
693
+ if (done) {
694
+ return { done: true, value: void 0 };
695
+ }
696
+ try {
697
+ const optionValue = await effect.Runtime.runPromise(runtime)(effect.Queue.take(queue));
698
+ if (effect.Option.isNone(optionValue)) {
699
+ done = true;
700
+ return { done: true, value: void 0 };
701
+ }
702
+ return { done: false, value: optionValue.value };
703
+ } catch (error) {
704
+ done = true;
705
+ throw error;
706
+ }
707
+ },
708
+ async return() {
709
+ done = true;
710
+ if (initialized) {
711
+ try {
712
+ await effect.Runtime.runPromise(runtime)(
713
+ effect.Fiber.interrupt(fiber)
714
+ );
715
+ await effect.Runtime.runPromise(runtime)(effect.Queue.shutdown(queue));
716
+ } catch {
717
+ }
718
+ }
719
+ return { done: true, value: void 0 };
720
+ }
721
+ };
722
+ }
723
+
724
+ // src/builder/schema-builder.ts
725
+ function updateState(state, key, value) {
726
+ return { ...state, [key]: value };
727
+ }
728
+ var GraphQLSchemaBuilder = class _GraphQLSchemaBuilder {
729
+ constructor(state) {
730
+ this.state = state;
731
+ }
732
+ pipe() {
733
+ return effect.Pipeable.pipeArguments(this, arguments);
734
+ }
735
+ /**
736
+ * Create an empty schema builder
737
+ */
738
+ static empty = new _GraphQLSchemaBuilder({
739
+ types: /* @__PURE__ */ new Map(),
740
+ interfaces: /* @__PURE__ */ new Map(),
741
+ enums: /* @__PURE__ */ new Map(),
742
+ unions: /* @__PURE__ */ new Map(),
743
+ inputs: /* @__PURE__ */ new Map(),
744
+ directives: /* @__PURE__ */ new Map(),
745
+ middlewares: [],
746
+ extensions: [],
747
+ queries: /* @__PURE__ */ new Map(),
748
+ mutations: /* @__PURE__ */ new Map(),
749
+ subscriptions: /* @__PURE__ */ new Map(),
750
+ objectFields: /* @__PURE__ */ new Map()
751
+ });
752
+ /**
753
+ * Create a new builder with updated state
754
+ */
755
+ with(newState) {
756
+ return new _GraphQLSchemaBuilder(newState);
757
+ }
758
+ // ============================================================================
759
+ // Registration Methods
760
+ // ============================================================================
761
+ /**
762
+ * Add a query field
763
+ */
764
+ query(name, config) {
765
+ const newQueries = new Map(this.state.queries);
766
+ newQueries.set(name, config);
767
+ return this.with(updateState(this.state, "queries", newQueries));
768
+ }
769
+ /**
770
+ * Add a mutation field
771
+ */
772
+ mutation(name, config) {
773
+ const newMutations = new Map(this.state.mutations);
774
+ newMutations.set(name, config);
775
+ return this.with(updateState(this.state, "mutations", newMutations));
776
+ }
777
+ /**
778
+ * Add a subscription field
779
+ *
780
+ * Subscriptions return a Stream that yields values over time.
781
+ * The subscribe function returns an Effect that produces a Stream.
782
+ *
783
+ * @example
784
+ * ```typescript
785
+ * builder.subscription("userCreated", {
786
+ * type: User,
787
+ * subscribe: Effect.gen(function*() {
788
+ * const pubsub = yield* PubSubService
789
+ * return pubsub.subscribe("USER_CREATED")
790
+ * }),
791
+ * })
792
+ * ```
793
+ */
794
+ subscription(name, config) {
795
+ const newSubscriptions = new Map(this.state.subscriptions);
796
+ newSubscriptions.set(name, config);
797
+ return this.with(updateState(this.state, "subscriptions", newSubscriptions));
798
+ }
799
+ /**
800
+ * Register an object type from a schema
801
+ */
802
+ objectType(config) {
803
+ const { schema, implements: implementsInterfaces, directives, cacheControl, fields } = config;
804
+ const name = config.name ?? getSchemaName(schema);
805
+ if (!name) {
806
+ throw new Error(
807
+ "objectType requires a name. Either provide one explicitly or use a TaggedStruct/TaggedClass/Schema.Class"
808
+ );
809
+ }
810
+ const newTypes = new Map(this.state.types);
811
+ newTypes.set(name, { name, schema, implements: implementsInterfaces, directives, cacheControl });
812
+ let newObjectFields = this.state.objectFields;
813
+ if (fields) {
814
+ newObjectFields = new Map(this.state.objectFields);
815
+ const typeFields = /* @__PURE__ */ new Map();
816
+ for (const [fieldName, fieldConfig] of Object.entries(fields)) {
817
+ typeFields.set(fieldName, fieldConfig);
818
+ }
819
+ newObjectFields.set(name, typeFields);
820
+ }
821
+ return this.with({
822
+ ...this.state,
823
+ types: newTypes,
824
+ objectFields: newObjectFields
825
+ });
826
+ }
827
+ /**
828
+ * Register an interface type from a schema
829
+ */
830
+ interfaceType(config) {
831
+ const { schema, resolveType, directives } = config;
832
+ const name = config.name ?? getSchemaName(schema);
833
+ if (!name) {
834
+ throw new Error(
835
+ "interfaceType requires a name. Either provide one explicitly or use a TaggedStruct/TaggedClass/Schema.Class"
836
+ );
837
+ }
838
+ const newInterfaces = new Map(this.state.interfaces);
839
+ newInterfaces.set(name, {
840
+ name,
841
+ schema,
842
+ resolveType: resolveType ?? ((value) => value._tag),
843
+ directives
844
+ });
845
+ return this.with(updateState(this.state, "interfaces", newInterfaces));
846
+ }
847
+ /**
848
+ * Register an enum type
849
+ */
850
+ enumType(config) {
851
+ const { name, values, description, directives } = config;
852
+ const newEnums = new Map(this.state.enums);
853
+ newEnums.set(name, { name, values, description, directives });
854
+ return this.with(updateState(this.state, "enums", newEnums));
855
+ }
856
+ /**
857
+ * Register a union type
858
+ */
859
+ unionType(config) {
860
+ const { name, types, resolveType, directives } = config;
861
+ const newUnions = new Map(this.state.unions);
862
+ newUnions.set(name, {
863
+ name,
864
+ types,
865
+ resolveType: resolveType ?? ((value) => value._tag),
866
+ directives
867
+ });
868
+ return this.with(updateState(this.state, "unions", newUnions));
869
+ }
870
+ /**
871
+ * Register an input type
872
+ */
873
+ inputType(config) {
874
+ const { schema, description, directives } = config;
875
+ const name = config.name ?? getSchemaName(schema);
876
+ if (!name) {
877
+ throw new Error(
878
+ "inputType requires a name. Either provide one explicitly or use a TaggedStruct/TaggedClass/Schema.Class"
879
+ );
880
+ }
881
+ const newInputs = new Map(this.state.inputs);
882
+ newInputs.set(name, { name, schema, description, directives });
883
+ return this.with(updateState(this.state, "inputs", newInputs));
884
+ }
885
+ /**
886
+ * Register a directive
887
+ */
888
+ directive(config) {
889
+ const newDirectives = new Map(this.state.directives);
890
+ newDirectives.set(config.name, config);
891
+ return this.with(updateState(this.state, "directives", newDirectives));
892
+ }
893
+ /**
894
+ * Register a middleware
895
+ *
896
+ * Middleware wraps all resolvers (or those matching a pattern) and executes
897
+ * in an "onion" model - first registered middleware is the outermost layer.
898
+ *
899
+ * @param config.name - Middleware name (for debugging/logging)
900
+ * @param config.description - Optional description
901
+ * @param config.match - Optional predicate to filter which fields this applies to
902
+ * @param config.apply - Function that transforms the resolver Effect
903
+ *
904
+ * @example
905
+ * ```typescript
906
+ * builder.middleware({
907
+ * name: "logging",
908
+ * apply: (effect, ctx) => Effect.gen(function*() {
909
+ * yield* Effect.logInfo(`Resolving ${ctx.info.fieldName}`)
910
+ * const start = Date.now()
911
+ * const result = yield* effect
912
+ * yield* Effect.logInfo(`Resolved in ${Date.now() - start}ms`)
913
+ * return result
914
+ * })
915
+ * })
916
+ * ```
917
+ */
918
+ middleware(config) {
919
+ const newMiddlewares = [...this.state.middlewares, config];
920
+ return this.with({ ...this.state, middlewares: newMiddlewares });
921
+ }
922
+ /**
923
+ * Register an extension
924
+ *
925
+ * Extensions provide lifecycle hooks that run at each phase of request processing
926
+ * (parse, validate, execute) and can contribute data to the response's extensions field.
927
+ *
928
+ * @param config.name - Extension name (for debugging/logging)
929
+ * @param config.description - Optional description
930
+ * @param config.onParse - Called after query parsing
931
+ * @param config.onValidate - Called after validation
932
+ * @param config.onExecuteStart - Called before execution begins
933
+ * @param config.onExecuteEnd - Called after execution completes
934
+ *
935
+ * @example
936
+ * ```typescript
937
+ * builder.extension({
938
+ * name: "tracing",
939
+ * onExecuteStart: () => Effect.gen(function*() {
940
+ * const ext = yield* ExtensionsService
941
+ * yield* ext.set("tracing", { startTime: Date.now() })
942
+ * }),
943
+ * onExecuteEnd: () => Effect.gen(function*() {
944
+ * const ext = yield* ExtensionsService
945
+ * yield* ext.merge("tracing", { endTime: Date.now() })
946
+ * }),
947
+ * })
948
+ * ```
949
+ */
950
+ extension(config) {
951
+ const newExtensions = [...this.state.extensions, config];
952
+ return this.with({ ...this.state, extensions: newExtensions });
953
+ }
954
+ /**
955
+ * Get the registered extensions for use by the execution layer
956
+ */
957
+ getExtensions() {
958
+ return this.state.extensions;
959
+ }
960
+ /**
961
+ * Add a computed/relational field to an object type
962
+ */
963
+ field(typeName, fieldName, config) {
964
+ const newObjectFields = new Map(this.state.objectFields);
965
+ const typeFields = newObjectFields.get(typeName) || /* @__PURE__ */ new Map();
966
+ typeFields.set(fieldName, config);
967
+ newObjectFields.set(typeName, typeFields);
968
+ return this.with(updateState(this.state, "objectFields", newObjectFields));
969
+ }
970
+ // ============================================================================
971
+ // Schema Building
972
+ // ============================================================================
973
+ /**
974
+ * Get the field complexity map for use in complexity validation.
975
+ * Maps "TypeName.fieldName" to the complexity value or function.
976
+ */
977
+ getFieldComplexities() {
978
+ const complexities = /* @__PURE__ */ new Map();
979
+ for (const [name, config] of this.state.queries) {
980
+ if (config.complexity !== void 0) {
981
+ complexities.set(`Query.${name}`, config.complexity);
982
+ }
983
+ }
984
+ for (const [name, config] of this.state.mutations) {
985
+ if (config.complexity !== void 0) {
986
+ complexities.set(`Mutation.${name}`, config.complexity);
987
+ }
988
+ }
989
+ for (const [name, config] of this.state.subscriptions) {
990
+ if (config.complexity !== void 0) {
991
+ complexities.set(`Subscription.${name}`, config.complexity);
992
+ }
993
+ }
994
+ for (const [typeName, fields] of this.state.objectFields) {
995
+ for (const [fieldName, config] of fields) {
996
+ if (config.complexity !== void 0) {
997
+ complexities.set(`${typeName}.${fieldName}`, config.complexity);
998
+ }
999
+ }
1000
+ }
1001
+ return complexities;
1002
+ }
1003
+ /**
1004
+ * Get the cache hint map for use in cache control calculation.
1005
+ * Maps "TypeName.fieldName" to the cache hint for field-level hints,
1006
+ * or "TypeName" to the cache hint for type-level hints.
1007
+ */
1008
+ getCacheHints() {
1009
+ const hints = /* @__PURE__ */ new Map();
1010
+ for (const [typeName, typeReg] of this.state.types) {
1011
+ if (typeReg.cacheControl !== void 0) {
1012
+ hints.set(typeName, typeReg.cacheControl);
1013
+ }
1014
+ }
1015
+ for (const [name, config] of this.state.queries) {
1016
+ if (config.cacheControl !== void 0) {
1017
+ hints.set(`Query.${name}`, config.cacheControl);
1018
+ }
1019
+ }
1020
+ for (const [name, config] of this.state.subscriptions) {
1021
+ if (config.cacheControl !== void 0) {
1022
+ hints.set(`Subscription.${name}`, config.cacheControl);
1023
+ }
1024
+ }
1025
+ for (const [typeName, fields] of this.state.objectFields) {
1026
+ for (const [fieldName, config] of fields) {
1027
+ if (config.cacheControl !== void 0) {
1028
+ hints.set(`${typeName}.${fieldName}`, config.cacheControl);
1029
+ }
1030
+ }
1031
+ }
1032
+ return hints;
1033
+ }
1034
+ /**
1035
+ * Build the GraphQL schema (no services required)
1036
+ */
1037
+ buildSchema() {
1038
+ const directiveRegistry = this.buildDirectiveRegistry();
1039
+ const enumRegistry = this.buildEnumRegistry();
1040
+ const inputRegistry = this.buildInputRegistry(enumRegistry);
1041
+ const interfaceRegistry = this.buildInterfaceRegistry(enumRegistry);
1042
+ const { typeRegistry, unionRegistry } = this.buildTypeAndUnionRegistries(
1043
+ enumRegistry,
1044
+ interfaceRegistry
1045
+ );
1046
+ const fieldCtx = this.createFieldBuilderContext(
1047
+ typeRegistry,
1048
+ interfaceRegistry,
1049
+ enumRegistry,
1050
+ unionRegistry,
1051
+ inputRegistry
1052
+ );
1053
+ const queryFields = this.buildQueryFields(fieldCtx);
1054
+ const mutationFields = this.buildMutationFields(fieldCtx);
1055
+ const subscriptionFields = this.buildSubscriptionFields(fieldCtx);
1056
+ return this.assembleSchema({
1057
+ directiveRegistry,
1058
+ enumRegistry,
1059
+ inputRegistry,
1060
+ interfaceRegistry,
1061
+ typeRegistry,
1062
+ unionRegistry,
1063
+ queryFields,
1064
+ mutationFields,
1065
+ subscriptionFields
1066
+ });
1067
+ }
1068
+ buildDirectiveRegistry() {
1069
+ const registry = /* @__PURE__ */ new Map();
1070
+ const cache = buildInputTypeLookupCache(this.state.inputs, this.state.enums);
1071
+ for (const [name, reg] of this.state.directives) {
1072
+ const graphqlDirective = new graphql.GraphQLDirective({
1073
+ name,
1074
+ description: reg.description,
1075
+ locations: [...reg.locations],
1076
+ args: reg.args ? toGraphQLArgsWithRegistry(
1077
+ reg.args,
1078
+ /* @__PURE__ */ new Map(),
1079
+ /* @__PURE__ */ new Map(),
1080
+ this.state.inputs,
1081
+ this.state.enums,
1082
+ cache
1083
+ ) : void 0
1084
+ });
1085
+ registry.set(name, graphqlDirective);
1086
+ }
1087
+ return registry;
1088
+ }
1089
+ buildEnumRegistry() {
1090
+ const registry = /* @__PURE__ */ new Map();
1091
+ for (const [name, reg] of this.state.enums) {
1092
+ const enumValues = {};
1093
+ for (const value of reg.values) {
1094
+ enumValues[value] = { value };
1095
+ }
1096
+ registry.set(
1097
+ name,
1098
+ new graphql.GraphQLEnumType({
1099
+ name,
1100
+ values: enumValues,
1101
+ description: reg.description,
1102
+ extensions: reg.directives ? { directives: reg.directives } : void 0
1103
+ })
1104
+ );
1105
+ }
1106
+ return registry;
1107
+ }
1108
+ buildInputRegistry(enumRegistry) {
1109
+ const registry = /* @__PURE__ */ new Map();
1110
+ const cache = buildInputTypeLookupCache(this.state.inputs, this.state.enums);
1111
+ for (const [name, reg] of this.state.inputs) {
1112
+ const inputType2 = new graphql.GraphQLInputObjectType({
1113
+ name,
1114
+ description: reg.description,
1115
+ fields: () => schemaToInputFields(
1116
+ reg.schema,
1117
+ enumRegistry,
1118
+ registry,
1119
+ this.state.inputs,
1120
+ this.state.enums,
1121
+ cache
1122
+ ),
1123
+ extensions: reg.directives ? { directives: reg.directives } : void 0
1124
+ });
1125
+ registry.set(name, inputType2);
1126
+ }
1127
+ return registry;
1128
+ }
1129
+ buildInterfaceRegistry(enumRegistry) {
1130
+ const registry = /* @__PURE__ */ new Map();
1131
+ const typeRegistry = /* @__PURE__ */ new Map();
1132
+ const unionRegistry = /* @__PURE__ */ new Map();
1133
+ const sharedCtx = {
1134
+ types: this.state.types,
1135
+ interfaces: this.state.interfaces,
1136
+ enums: this.state.enums,
1137
+ unions: this.state.unions,
1138
+ inputs: this.state.inputs,
1139
+ typeRegistry,
1140
+ interfaceRegistry: registry,
1141
+ enumRegistry,
1142
+ unionRegistry};
1143
+ buildReverseLookups(sharedCtx);
1144
+ for (const [name, reg] of this.state.interfaces) {
1145
+ const interfaceType2 = new graphql.GraphQLInterfaceType({
1146
+ name,
1147
+ fields: () => schemaToFields(reg.schema, sharedCtx),
1148
+ resolveType: reg.resolveType,
1149
+ extensions: reg.directives ? { directives: reg.directives } : void 0
1150
+ });
1151
+ registry.set(name, interfaceType2);
1152
+ }
1153
+ return registry;
1154
+ }
1155
+ buildTypeAndUnionRegistries(enumRegistry, interfaceRegistry) {
1156
+ const typeRegistry = /* @__PURE__ */ new Map();
1157
+ const unionRegistry = /* @__PURE__ */ new Map();
1158
+ const sharedCtx = {
1159
+ types: this.state.types,
1160
+ interfaces: this.state.interfaces,
1161
+ enums: this.state.enums,
1162
+ unions: this.state.unions,
1163
+ inputs: this.state.inputs,
1164
+ typeRegistry,
1165
+ interfaceRegistry,
1166
+ enumRegistry,
1167
+ unionRegistry};
1168
+ buildReverseLookups(sharedCtx);
1169
+ const sharedFieldCtx = this.createFieldBuilderContext(
1170
+ typeRegistry,
1171
+ interfaceRegistry,
1172
+ enumRegistry,
1173
+ unionRegistry,
1174
+ /* @__PURE__ */ new Map()
1175
+ );
1176
+ for (const [typeName, typeReg] of this.state.types) {
1177
+ const implementedInterfaces = typeReg.implements?.map((name) => interfaceRegistry.get(name)).filter(Boolean) ?? [];
1178
+ const graphqlType = new graphql.GraphQLObjectType({
1179
+ name: typeName,
1180
+ fields: () => {
1181
+ const baseFields = schemaToFields(typeReg.schema, sharedCtx);
1182
+ const additionalFields = this.state.objectFields.get(typeName);
1183
+ if (additionalFields) {
1184
+ for (const [fieldName, fieldConfig] of additionalFields) {
1185
+ baseFields[fieldName] = buildObjectField(fieldConfig, sharedFieldCtx);
1186
+ }
1187
+ }
1188
+ return baseFields;
1189
+ },
1190
+ interfaces: implementedInterfaces.length > 0 ? implementedInterfaces : void 0,
1191
+ extensions: typeReg.directives ? { directives: typeReg.directives } : void 0
1192
+ });
1193
+ typeRegistry.set(typeName, graphqlType);
1194
+ }
1195
+ for (const [name, reg] of this.state.unions) {
1196
+ const unionType2 = new graphql.GraphQLUnionType({
1197
+ name,
1198
+ types: () => reg.types.map((typeName) => typeRegistry.get(typeName)).filter(Boolean),
1199
+ resolveType: reg.resolveType,
1200
+ extensions: reg.directives ? { directives: reg.directives } : void 0
1201
+ });
1202
+ unionRegistry.set(name, unionType2);
1203
+ }
1204
+ return { typeRegistry, unionRegistry };
1205
+ }
1206
+ createFieldBuilderContext(typeRegistry, interfaceRegistry, enumRegistry, unionRegistry, inputRegistry) {
1207
+ const inputTypeLookupCache = buildInputTypeLookupCache(this.state.inputs, this.state.enums);
1208
+ return {
1209
+ types: this.state.types,
1210
+ interfaces: this.state.interfaces,
1211
+ enums: this.state.enums,
1212
+ unions: this.state.unions,
1213
+ inputs: this.state.inputs,
1214
+ typeRegistry,
1215
+ interfaceRegistry,
1216
+ enumRegistry,
1217
+ unionRegistry,
1218
+ inputRegistry,
1219
+ directiveRegistrations: this.state.directives,
1220
+ middlewares: this.state.middlewares,
1221
+ inputTypeLookupCache
1222
+ };
1223
+ }
1224
+ buildQueryFields(ctx) {
1225
+ const fields = {};
1226
+ for (const [name, config] of this.state.queries) {
1227
+ fields[name] = buildField(config, ctx);
1228
+ }
1229
+ return fields;
1230
+ }
1231
+ buildMutationFields(ctx) {
1232
+ const fields = {};
1233
+ for (const [name, config] of this.state.mutations) {
1234
+ fields[name] = buildField(config, ctx);
1235
+ }
1236
+ return fields;
1237
+ }
1238
+ buildSubscriptionFields(ctx) {
1239
+ const fields = {};
1240
+ for (const [name, config] of this.state.subscriptions) {
1241
+ fields[name] = buildSubscriptionField(config, ctx);
1242
+ }
1243
+ return fields;
1244
+ }
1245
+ assembleSchema(registries) {
1246
+ const schemaConfig = {
1247
+ types: [
1248
+ ...Array.from(registries.enumRegistry.values()),
1249
+ ...Array.from(registries.inputRegistry.values()),
1250
+ ...Array.from(registries.interfaceRegistry.values()),
1251
+ ...Array.from(registries.typeRegistry.values()),
1252
+ ...Array.from(registries.unionRegistry.values())
1253
+ ],
1254
+ directives: registries.directiveRegistry.size > 0 ? [...Array.from(registries.directiveRegistry.values())] : void 0
1255
+ };
1256
+ if (Object.keys(registries.queryFields).length > 0) {
1257
+ schemaConfig.query = new graphql.GraphQLObjectType({
1258
+ name: "Query",
1259
+ fields: registries.queryFields
1260
+ });
1261
+ }
1262
+ if (Object.keys(registries.mutationFields).length > 0) {
1263
+ schemaConfig.mutation = new graphql.GraphQLObjectType({
1264
+ name: "Mutation",
1265
+ fields: registries.mutationFields
1266
+ });
1267
+ }
1268
+ if (Object.keys(registries.subscriptionFields).length > 0) {
1269
+ schemaConfig.subscription = new graphql.GraphQLObjectType({
1270
+ name: "Subscription",
1271
+ fields: registries.subscriptionFields
1272
+ });
1273
+ }
1274
+ return new graphql.GraphQLSchema(schemaConfig);
1275
+ }
1276
+ };
1277
+
1278
+ // src/builder/pipe-api.ts
1279
+ var objectType = (config) => (builder) => builder.objectType(config);
1280
+ var interfaceType = (config) => (builder) => builder.interfaceType(config);
1281
+ var enumType = (config) => (builder) => builder.enumType(config);
1282
+ var unionType = (config) => (builder) => builder.unionType(config);
1283
+ var inputType = (config) => (builder) => builder.inputType(config);
1284
+ var directive = (config) => (builder) => builder.directive(config);
1285
+ var middleware = (config) => (builder) => builder.middleware(config);
1286
+ var extension = (config) => (builder) => builder.extension(config);
1287
+ var query = (name, config) => (builder) => builder.query(name, config);
1288
+ var mutation = (name, config) => (builder) => builder.mutation(name, config);
1289
+ var subscription = (name, config) => (builder) => builder.subscription(name, config);
1290
+ var field = (typeName, fieldName, config) => (builder) => builder.field(typeName, fieldName, config);
1291
+ var compose = (...operations) => (builder) => operations.reduce((b, op) => op(b), builder);
1292
+ var ExtensionsService = effect.Context.GenericTag(
1293
+ "@effect-gql/ExtensionsService"
1294
+ );
1295
+ function deepMerge(target, source) {
1296
+ const result = { ...target };
1297
+ for (const key of Object.keys(source)) {
1298
+ const sourceValue = source[key];
1299
+ const targetValue = result[key];
1300
+ if (typeof sourceValue === "object" && sourceValue !== null && !Array.isArray(sourceValue) && typeof targetValue === "object" && targetValue !== null && !Array.isArray(targetValue)) {
1301
+ result[key] = deepMerge(
1302
+ targetValue,
1303
+ sourceValue
1304
+ );
1305
+ } else {
1306
+ result[key] = sourceValue;
1307
+ }
1308
+ }
1309
+ return result;
1310
+ }
1311
+ var makeExtensionsService = () => effect.Effect.gen(function* () {
1312
+ const ref = yield* effect.Ref.make({});
1313
+ return ExtensionsService.of({
1314
+ set: (key, value) => effect.Ref.update(ref, (current) => ({ ...current, [key]: value })),
1315
+ merge: (key, value) => effect.Ref.update(ref, (current) => {
1316
+ const existing = current[key];
1317
+ if (typeof existing === "object" && existing !== null && !Array.isArray(existing)) {
1318
+ return {
1319
+ ...current,
1320
+ [key]: deepMerge(existing, value)
1321
+ };
1322
+ }
1323
+ return { ...current, [key]: value };
1324
+ }),
1325
+ get: () => effect.Ref.get(ref)
1326
+ });
1327
+ });
1328
+ var runExtensionHooks = (extensions, hookName, getHookEffect) => effect.Effect.forEach(
1329
+ extensions.filter((ext) => ext[hookName] !== void 0),
1330
+ (ext) => getHookEffect(ext).pipe(
1331
+ effect.Effect.catchAllCause(
1332
+ (cause) => effect.Effect.logWarning(`Extension "${ext.name}" ${String(hookName)} hook failed`, cause)
1333
+ )
1334
+ ),
1335
+ { discard: true }
1336
+ );
1337
+ var runParseHooks = (extensions, source, document) => runExtensionHooks(extensions, "onParse", (ext) => ext.onParse(source, document));
1338
+ var runValidateHooks = (extensions, document, errors) => runExtensionHooks(extensions, "onValidate", (ext) => ext.onValidate(document, errors));
1339
+ var runExecuteStartHooks = (extensions, args) => runExtensionHooks(extensions, "onExecuteStart", (ext) => ext.onExecuteStart(args));
1340
+ var runExecuteEndHooks = (extensions, result) => runExtensionHooks(extensions, "onExecuteEnd", (ext) => ext.onExecuteEnd(result));
1341
+
1342
+ // src/builder/execute.ts
1343
+ var execute = (schema, layer, extensions = [], fieldComplexities = /* @__PURE__ */ new Map()) => (source, variableValues, operationName) => effect.Effect.gen(function* () {
1344
+ const extensionsService = yield* makeExtensionsService();
1345
+ const runtime = yield* effect.Effect.runtime();
1346
+ let document;
1347
+ try {
1348
+ document = graphql.parse(source);
1349
+ } catch (parseError) {
1350
+ const extensionData2 = yield* extensionsService.get();
1351
+ return {
1352
+ errors: [
1353
+ parseError instanceof graphql.GraphQLError ? parseError : new graphql.GraphQLError(String(parseError))
1354
+ ],
1355
+ extensions: Object.keys(extensionData2).length > 0 ? extensionData2 : void 0
1356
+ };
1357
+ }
1358
+ yield* runParseHooks(extensions, source, document).pipe(
1359
+ effect.Effect.provideService(ExtensionsService, extensionsService)
1360
+ );
1361
+ const validationErrors = graphql.validate(schema, document);
1362
+ yield* runValidateHooks(extensions, document, validationErrors).pipe(
1363
+ effect.Effect.provideService(ExtensionsService, extensionsService)
1364
+ );
1365
+ if (validationErrors.length > 0) {
1366
+ const extensionData2 = yield* extensionsService.get();
1367
+ return {
1368
+ errors: validationErrors,
1369
+ extensions: Object.keys(extensionData2).length > 0 ? extensionData2 : void 0
1370
+ };
1371
+ }
1372
+ const executionArgs = {
1373
+ source,
1374
+ document,
1375
+ variableValues,
1376
+ operationName,
1377
+ schema,
1378
+ fieldComplexities
1379
+ };
1380
+ yield* runExecuteStartHooks(extensions, executionArgs).pipe(
1381
+ effect.Effect.provideService(ExtensionsService, extensionsService)
1382
+ );
1383
+ const executeResult = yield* effect.Effect.try({
1384
+ try: () => graphql.execute({
1385
+ schema,
1386
+ document,
1387
+ variableValues,
1388
+ operationName,
1389
+ contextValue: { runtime }
1390
+ }),
1391
+ catch: (error) => new Error(String(error))
1392
+ });
1393
+ const resolvedResult = executeResult && typeof executeResult === "object" && "then" in executeResult ? yield* effect.Effect.promise(() => executeResult) : executeResult;
1394
+ yield* runExecuteEndHooks(extensions, resolvedResult).pipe(
1395
+ effect.Effect.provideService(ExtensionsService, extensionsService)
1396
+ );
1397
+ const extensionData = yield* extensionsService.get();
1398
+ if (Object.keys(extensionData).length > 0) {
1399
+ return {
1400
+ ...resolvedResult,
1401
+ extensions: {
1402
+ ...resolvedResult.extensions,
1403
+ ...extensionData
1404
+ }
1405
+ };
1406
+ }
1407
+ return resolvedResult;
1408
+ }).pipe(effect.Effect.provide(layer));
1409
+
1410
+ Object.defineProperty(exports, "DirectiveLocation", {
1411
+ enumerable: true,
1412
+ get: function () { return graphql.DirectiveLocation; }
1413
+ });
1414
+ exports.GraphQLSchemaBuilder = GraphQLSchemaBuilder;
1415
+ exports.compose = compose;
1416
+ exports.directive = directive;
1417
+ exports.enumType = enumType;
1418
+ exports.execute = execute;
1419
+ exports.extension = extension;
1420
+ exports.field = field;
1421
+ exports.getSchemaName = getSchemaName;
1422
+ exports.inputType = inputType;
1423
+ exports.interfaceType = interfaceType;
1424
+ exports.middleware = middleware;
1425
+ exports.mutation = mutation;
1426
+ exports.objectType = objectType;
1427
+ exports.query = query;
1428
+ exports.subscription = subscription;
1429
+ exports.unionType = unionType;
1430
+ //# sourceMappingURL=index.cjs.map
1431
+ //# sourceMappingURL=index.cjs.map