@effect-gql/core 1.3.4 → 1.4.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.
package/builder/index.cjs CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  var graphql = require('graphql');
4
4
  var effect = require('effect');
5
- var S2 = require('effect/Schema');
5
+ var S = require('effect/Schema');
6
6
  var AST = require('effect/SchemaAST');
7
7
 
8
8
  function _interopNamespace(e) {
@@ -23,7 +23,7 @@ function _interopNamespace(e) {
23
23
  return Object.freeze(n);
24
24
  }
25
25
 
26
- var S2__namespace = /*#__PURE__*/_interopNamespace(S2);
26
+ var S__namespace = /*#__PURE__*/_interopNamespace(S);
27
27
  var AST__namespace = /*#__PURE__*/_interopNamespace(AST);
28
28
 
29
29
  // src/builder/index.ts
@@ -41,6 +41,34 @@ var isIntegerType = (ast) => {
41
41
  }
42
42
  return false;
43
43
  };
44
+ var isOptionDeclaration = (ast) => {
45
+ if (ast._tag === "Declaration") {
46
+ const annotations = ast.annotations;
47
+ if (annotations) {
48
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
49
+ const typeConstructor = annotations[TypeConstructorSymbol];
50
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
51
+ return true;
52
+ }
53
+ }
54
+ }
55
+ return false;
56
+ };
57
+ var isOptionTransformation = (ast) => {
58
+ if (ast._tag === "Transformation") {
59
+ return isOptionDeclaration(ast.to);
60
+ }
61
+ return false;
62
+ };
63
+ var getOptionInnerType = (ast) => {
64
+ if (ast._tag === "Declaration") {
65
+ const typeParams = ast.typeParameters;
66
+ if (typeParams && typeParams.length > 0) {
67
+ return typeParams[0];
68
+ }
69
+ }
70
+ return void 0;
71
+ };
44
72
  var toGraphQLType = (schema) => {
45
73
  const ast = schema.ast;
46
74
  if (ast._tag === "StringKeyword") return graphql.GraphQLString;
@@ -50,7 +78,7 @@ var toGraphQLType = (schema) => {
50
78
  if (isIntegerType(ast)) {
51
79
  return graphql.GraphQLInt;
52
80
  }
53
- return toGraphQLType(S2__namespace.make(ast.from));
81
+ return toGraphQLType(S__namespace.make(ast.from));
54
82
  }
55
83
  if (ast._tag === "Literal") {
56
84
  if (typeof ast.literal === "string") return graphql.GraphQLString;
@@ -62,7 +90,7 @@ var toGraphQLType = (schema) => {
62
90
  if (ast._tag === "TupleType") {
63
91
  const elements = ast.elements;
64
92
  if (elements.length > 0) {
65
- const elementSchema = S2__namespace.make(elements[0].type);
93
+ const elementSchema = S__namespace.make(elements[0].type);
66
94
  return new graphql.GraphQLList(toGraphQLType(elementSchema));
67
95
  }
68
96
  }
@@ -70,7 +98,7 @@ var toGraphQLType = (schema) => {
70
98
  const fields = {};
71
99
  for (const field2 of ast.propertySignatures) {
72
100
  const fieldName = String(field2.name);
73
- const fieldSchema = S2__namespace.make(field2.type);
101
+ const fieldSchema = S__namespace.make(field2.type);
74
102
  let fieldType = toGraphQLType(fieldSchema);
75
103
  if (!field2.isOptional) {
76
104
  fieldType = new graphql.GraphQLNonNull(fieldType);
@@ -84,17 +112,35 @@ var toGraphQLType = (schema) => {
84
112
  });
85
113
  }
86
114
  if (ast._tag === "Transformation") {
87
- return toGraphQLType(S2__namespace.make(ast.to));
115
+ if (isOptionTransformation(ast)) {
116
+ const innerType = getOptionInnerType(ast.to);
117
+ if (innerType) {
118
+ return toGraphQLType(S__namespace.make(innerType));
119
+ }
120
+ }
121
+ return toGraphQLType(S__namespace.make(ast.to));
122
+ }
123
+ if (ast._tag === "Declaration") {
124
+ if (isOptionDeclaration(ast)) {
125
+ const innerType = getOptionInnerType(ast);
126
+ if (innerType) {
127
+ return toGraphQLType(S__namespace.make(innerType));
128
+ }
129
+ }
130
+ const typeParams = ast.typeParameters;
131
+ if (typeParams && typeParams.length > 0) {
132
+ return toGraphQLType(S__namespace.make(typeParams[0]));
133
+ }
88
134
  }
89
135
  if (ast._tag === "Union") {
90
136
  const types = ast.types;
91
137
  if (types.length > 0) {
92
- return toGraphQLType(S2__namespace.make(types[0]));
138
+ return toGraphQLType(S__namespace.make(types[0]));
93
139
  }
94
140
  }
95
141
  if (ast._tag === "Suspend") {
96
142
  const innerAst = ast.f();
97
- return toGraphQLType(S2__namespace.make(innerAst));
143
+ return toGraphQLType(S__namespace.make(innerAst));
98
144
  }
99
145
  return graphql.GraphQLString;
100
146
  };
@@ -107,7 +153,7 @@ var toGraphQLInputType = (schema) => {
107
153
  if (isIntegerType(ast)) {
108
154
  return graphql.GraphQLInt;
109
155
  }
110
- return toGraphQLInputType(S2__namespace.make(ast.from));
156
+ return toGraphQLInputType(S__namespace.make(ast.from));
111
157
  }
112
158
  if (ast._tag === "Literal") {
113
159
  if (typeof ast.literal === "string") return graphql.GraphQLString;
@@ -119,7 +165,7 @@ var toGraphQLInputType = (schema) => {
119
165
  if (ast._tag === "TupleType") {
120
166
  const elements = ast.elements;
121
167
  if (elements.length > 0) {
122
- const elementSchema = S2__namespace.make(elements[0].type);
168
+ const elementSchema = S__namespace.make(elements[0].type);
123
169
  return new graphql.GraphQLList(toGraphQLInputType(elementSchema));
124
170
  }
125
171
  }
@@ -127,7 +173,7 @@ var toGraphQLInputType = (schema) => {
127
173
  const fields = {};
128
174
  for (const field2 of ast.propertySignatures) {
129
175
  const fieldName = String(field2.name);
130
- const fieldSchema = S2__namespace.make(field2.type);
176
+ const fieldSchema = S__namespace.make(field2.type);
131
177
  let fieldType = toGraphQLInputType(fieldSchema);
132
178
  if (!field2.isOptional) {
133
179
  fieldType = new graphql.GraphQLNonNull(fieldType);
@@ -141,17 +187,33 @@ var toGraphQLInputType = (schema) => {
141
187
  });
142
188
  }
143
189
  if (ast._tag === "Transformation") {
144
- return toGraphQLInputType(S2__namespace.make(ast.from));
190
+ return toGraphQLInputType(S__namespace.make(ast.from));
191
+ }
192
+ if (ast._tag === "Declaration") {
193
+ if (isOptionDeclaration(ast)) {
194
+ const innerType = getOptionInnerType(ast);
195
+ if (innerType) {
196
+ return toGraphQLInputType(S__namespace.make(innerType));
197
+ }
198
+ }
199
+ const typeParams = ast.typeParameters;
200
+ if (typeParams && typeParams.length > 0) {
201
+ return toGraphQLInputType(S__namespace.make(typeParams[0]));
202
+ }
145
203
  }
146
204
  if (ast._tag === "Union") {
147
205
  const types = ast.types;
206
+ const nonNullTypes = types.filter((t) => t._tag !== "Literal" || t.literal !== null).filter((t) => t._tag !== "UndefinedKeyword");
207
+ if (nonNullTypes.length > 0) {
208
+ return toGraphQLInputType(S__namespace.make(nonNullTypes[0]));
209
+ }
148
210
  if (types.length > 0) {
149
- return toGraphQLInputType(S2__namespace.make(types[0]));
211
+ return toGraphQLInputType(S__namespace.make(types[0]));
150
212
  }
151
213
  }
152
214
  if (ast._tag === "Suspend") {
153
215
  const innerAst = ast.f();
154
- return toGraphQLInputType(S2__namespace.make(innerAst));
216
+ return toGraphQLInputType(S__namespace.make(innerAst));
155
217
  }
156
218
  return graphql.GraphQLString;
157
219
  };
@@ -161,7 +223,7 @@ var toGraphQLArgs = (schema) => {
161
223
  const args = {};
162
224
  for (const field2 of ast.propertySignatures) {
163
225
  const fieldName = String(field2.name);
164
- const fieldSchema = S2__namespace.make(field2.type);
226
+ const fieldSchema = S__namespace.make(field2.type);
165
227
  let fieldType = toGraphQLInputType(fieldSchema);
166
228
  if (!field2.isOptional) {
167
229
  fieldType = new graphql.GraphQLNonNull(fieldType);
@@ -261,9 +323,21 @@ function toGraphQLTypeWithRegistry(schema, ctx) {
261
323
  if (ast._tag === "TupleType") {
262
324
  return handleTupleTypeAST(ast, ctx);
263
325
  }
326
+ if (ast._tag === "Declaration") {
327
+ if (isOptionDeclaration2(ast)) {
328
+ const innerType = getOptionInnerType2(ast);
329
+ if (innerType) {
330
+ return toGraphQLTypeWithRegistry(S__namespace.make(innerType), ctx);
331
+ }
332
+ }
333
+ const typeParams = ast.typeParameters;
334
+ if (typeParams && typeParams.length > 0) {
335
+ return toGraphQLTypeWithRegistry(S__namespace.make(typeParams[0]), ctx);
336
+ }
337
+ }
264
338
  if (ast._tag === "Suspend") {
265
339
  const innerAst = ast.f();
266
- return toGraphQLTypeWithRegistry(S2__namespace.make(innerAst), ctx);
340
+ return toGraphQLTypeWithRegistry(S__namespace.make(innerAst), ctx);
267
341
  }
268
342
  return toGraphQLType(schema);
269
343
  }
@@ -281,20 +355,48 @@ function findRegisteredInterface(schema, ast, ctx) {
281
355
  }
282
356
  return void 0;
283
357
  }
358
+ function isOptionDeclaration2(ast) {
359
+ if (ast._tag === "Declaration") {
360
+ const annotations = ast.annotations;
361
+ if (annotations) {
362
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
363
+ const typeConstructor = annotations[TypeConstructorSymbol];
364
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
365
+ return true;
366
+ }
367
+ }
368
+ }
369
+ return false;
370
+ }
371
+ function getOptionInnerType2(ast) {
372
+ if (ast._tag === "Declaration") {
373
+ const typeParams = ast.typeParameters;
374
+ if (typeParams && typeParams.length > 0) {
375
+ return typeParams[0];
376
+ }
377
+ }
378
+ return void 0;
379
+ }
284
380
  function handleTransformationAST(ast, ctx) {
285
381
  const toAst = ast.to;
382
+ if (isOptionDeclaration2(toAst)) {
383
+ const innerType = getOptionInnerType2(toAst);
384
+ if (innerType) {
385
+ return toGraphQLTypeWithRegistry(S__namespace.make(innerType), ctx);
386
+ }
387
+ }
286
388
  if (toAst._tag === "TupleType") {
287
389
  if (toAst.rest && toAst.rest.length > 0) {
288
- const elementSchema = S2__namespace.make(toAst.rest[0].type);
390
+ const elementSchema = S__namespace.make(toAst.rest[0].type);
289
391
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
290
392
  return new graphql.GraphQLList(elementType);
291
393
  } else if (toAst.elements.length > 0) {
292
- const elementSchema = S2__namespace.make(toAst.elements[0].type);
394
+ const elementSchema = S__namespace.make(toAst.elements[0].type);
293
395
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
294
396
  return new graphql.GraphQLList(elementType);
295
397
  }
296
398
  }
297
- return toGraphQLTypeWithRegistry(S2__namespace.make(ast.to), ctx);
399
+ return toGraphQLTypeWithRegistry(S__namespace.make(ast.to), ctx);
298
400
  }
299
401
  function handleUnionAST(ast, ctx) {
300
402
  const allLiterals = ast.types.every((t) => t._tag === "Literal");
@@ -306,9 +408,9 @@ function handleUnionAST(ast, ctx) {
306
408
  if (unionType2) return unionType2;
307
409
  }
308
410
  if (ast.types.length > 0) {
309
- return toGraphQLTypeWithRegistry(S2__namespace.make(ast.types[0]), ctx);
411
+ return toGraphQLTypeWithRegistry(S__namespace.make(ast.types[0]), ctx);
310
412
  }
311
- return toGraphQLType(S2__namespace.make(ast));
413
+ return toGraphQLType(S__namespace.make(ast));
312
414
  }
313
415
  function findEnumForLiteralUnion(types, ctx) {
314
416
  const literalValues = types.map((t) => String(t.literal)).sort();
@@ -351,15 +453,15 @@ function findEnumForLiteral(ast, ctx) {
351
453
  }
352
454
  function handleTupleTypeAST(ast, ctx) {
353
455
  if (ast.rest && ast.rest.length > 0) {
354
- const elementSchema = S2__namespace.make(ast.rest[0].type);
456
+ const elementSchema = S__namespace.make(ast.rest[0].type);
355
457
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
356
458
  return new graphql.GraphQLList(elementType);
357
459
  } else if (ast.elements && ast.elements.length > 0) {
358
- const elementSchema = S2__namespace.make(ast.elements[0].type);
460
+ const elementSchema = S__namespace.make(ast.elements[0].type);
359
461
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
360
462
  return new graphql.GraphQLList(elementType);
361
463
  }
362
- return toGraphQLType(S2__namespace.make(ast));
464
+ return toGraphQLType(S__namespace.make(ast));
363
465
  }
364
466
  function schemaToFields(schema, ctx) {
365
467
  let ast = schema.ast;
@@ -376,7 +478,7 @@ function schemaToFields(schema, ctx) {
376
478
  const fields = {};
377
479
  for (const field2 of ast.propertySignatures) {
378
480
  const fieldName = String(field2.name);
379
- const fieldSchema = S2__namespace.make(field2.type);
481
+ const fieldSchema = S__namespace.make(field2.type);
380
482
  let fieldType = toGraphQLTypeWithRegistry(fieldSchema, ctx);
381
483
  if (!field2.isOptional) {
382
484
  fieldType = getNonNull(fieldType);
@@ -393,7 +495,7 @@ function schemaToInputFields(schema, enumRegistry, inputRegistry, inputs, enums,
393
495
  const fields = {};
394
496
  for (const field2 of ast.propertySignatures) {
395
497
  const fieldName = String(field2.name);
396
- const fieldSchema = S2__namespace.make(field2.type);
498
+ const fieldSchema = S__namespace.make(field2.type);
397
499
  let fieldType = toGraphQLInputTypeWithRegistry(
398
500
  fieldSchema,
399
501
  enumRegistry,
@@ -435,7 +537,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
435
537
  if (ast._tag === "Transformation") {
436
538
  const toAst = ast.to;
437
539
  return toGraphQLInputTypeWithRegistry(
438
- S2__namespace.make(toAst),
540
+ S__namespace.make(toAst),
439
541
  enumRegistry,
440
542
  inputRegistry,
441
543
  inputs,
@@ -462,7 +564,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
462
564
  const nonUndefinedTypes = unionAst.types.filter((t) => t._tag !== "UndefinedKeyword");
463
565
  if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "Union") {
464
566
  return toGraphQLInputTypeWithRegistry(
465
- S2__namespace.make(nonUndefinedTypes[0]),
567
+ S__namespace.make(nonUndefinedTypes[0]),
466
568
  enumRegistry,
467
569
  inputRegistry,
468
570
  inputs,
@@ -472,7 +574,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
472
574
  }
473
575
  if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "TypeLiteral") {
474
576
  return toGraphQLInputTypeWithRegistry(
475
- S2__namespace.make(nonUndefinedTypes[0]),
577
+ S__namespace.make(nonUndefinedTypes[0]),
476
578
  enumRegistry,
477
579
  inputRegistry,
478
580
  inputs,
@@ -512,7 +614,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
512
614
  if (ast._tag === "Suspend") {
513
615
  const innerAst = ast.f();
514
616
  return toGraphQLInputTypeWithRegistry(
515
- S2__namespace.make(innerAst),
617
+ S__namespace.make(innerAst),
516
618
  enumRegistry,
517
619
  inputRegistry,
518
620
  inputs,
@@ -528,7 +630,7 @@ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs,
528
630
  const args = {};
529
631
  for (const field2 of ast.propertySignatures) {
530
632
  const fieldName = String(field2.name);
531
- const fieldSchema = S2__namespace.make(field2.type);
633
+ const fieldSchema = S__namespace.make(field2.type);
532
634
  let fieldType = toGraphQLInputTypeWithRegistry(
533
635
  fieldSchema,
534
636
  enumRegistry,
@@ -546,6 +648,29 @@ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs,
546
648
  }
547
649
  return toGraphQLArgs(schema);
548
650
  }
651
+ function isOptionSchema(schema) {
652
+ const ast = schema.ast;
653
+ if (ast._tag === "Transformation") {
654
+ const toAst = ast.to;
655
+ if (toAst._tag === "Declaration") {
656
+ const annotations = toAst.annotations;
657
+ if (annotations) {
658
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
659
+ const typeConstructor = annotations[TypeConstructorSymbol];
660
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
661
+ return true;
662
+ }
663
+ }
664
+ }
665
+ }
666
+ return false;
667
+ }
668
+ function encodeResolverOutput(schema, value) {
669
+ if (isOptionSchema(schema)) {
670
+ return effect.Effect.orDie(S__namespace.encode(schema)(value));
671
+ }
672
+ return effect.Effect.succeed(value);
673
+ }
549
674
  function applyDirectives(effect, directives, directiveRegistrations) {
550
675
  if (!directives) return effect;
551
676
  let wrapped = effect;
@@ -580,7 +705,8 @@ function buildField(config, ctx) {
580
705
  );
581
706
  const middlewareContext = { parent: _parent, args, info };
582
707
  effect$1 = applyMiddleware(effect$1, middlewareContext, ctx.middlewares);
583
- return await effect.Runtime.runPromise(context.runtime)(effect$1);
708
+ const result = await effect.Runtime.runPromise(context.runtime)(effect$1);
709
+ return await effect.Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, result));
584
710
  }
585
711
  };
586
712
  if (config.args) {
@@ -609,7 +735,8 @@ function buildObjectField(config, ctx) {
609
735
  );
610
736
  const middlewareContext = { parent, args, info };
611
737
  effect$1 = applyMiddleware(effect$1, middlewareContext, ctx.middlewares);
612
- return await effect.Runtime.runPromise(context.runtime)(effect$1);
738
+ const result = await effect.Runtime.runPromise(context.runtime)(effect$1);
739
+ return await effect.Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, result));
613
740
  }
614
741
  };
615
742
  if (config.args) {
@@ -644,13 +771,18 @@ function buildSubscriptionField(config, ctx) {
644
771
  return streamToAsyncIterator(stream, context.runtime);
645
772
  },
646
773
  // The resolve function transforms each yielded value
647
- // If no custom resolve is provided, return the payload directly
774
+ // If no custom resolve is provided, encode and return the payload directly
648
775
  resolve: config.resolve ? async (value, args, context, info) => {
649
776
  let effect$1 = config.resolve(value, args);
650
777
  const middlewareContext = { parent: value, args, info };
651
778
  effect$1 = applyMiddleware(effect$1, middlewareContext, ctx.middlewares);
652
- return await effect.Runtime.runPromise(context.runtime)(effect$1);
653
- } : (value) => value
779
+ const result = await effect.Runtime.runPromise(context.runtime)(effect$1);
780
+ return await effect.Runtime.runPromise(context.runtime)(
781
+ encodeResolverOutput(config.type, result)
782
+ );
783
+ } : async (value, _args, context) => {
784
+ return await effect.Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, value));
785
+ }
654
786
  };
655
787
  if (config.args) {
656
788
  fieldConfig.args = toGraphQLArgsWithRegistry(