@effect-gql/core 1.3.3 → 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/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
  var DataLoader = require('dataloader');
8
8
  var platform = require('@effect/platform');
@@ -27,7 +27,7 @@ function _interopNamespace(e) {
27
27
  return Object.freeze(n);
28
28
  }
29
29
 
30
- var S2__namespace = /*#__PURE__*/_interopNamespace(S2);
30
+ var S__namespace = /*#__PURE__*/_interopNamespace(S);
31
31
  var AST__namespace = /*#__PURE__*/_interopNamespace(AST);
32
32
  var DataLoader__default = /*#__PURE__*/_interopDefault(DataLoader);
33
33
 
@@ -46,6 +46,34 @@ var isIntegerType = (ast) => {
46
46
  }
47
47
  return false;
48
48
  };
49
+ var isOptionDeclaration = (ast) => {
50
+ if (ast._tag === "Declaration") {
51
+ const annotations = ast.annotations;
52
+ if (annotations) {
53
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
54
+ const typeConstructor = annotations[TypeConstructorSymbol];
55
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
56
+ return true;
57
+ }
58
+ }
59
+ }
60
+ return false;
61
+ };
62
+ var isOptionTransformation = (ast) => {
63
+ if (ast._tag === "Transformation") {
64
+ return isOptionDeclaration(ast.to);
65
+ }
66
+ return false;
67
+ };
68
+ var getOptionInnerType = (ast) => {
69
+ if (ast._tag === "Declaration") {
70
+ const typeParams = ast.typeParameters;
71
+ if (typeParams && typeParams.length > 0) {
72
+ return typeParams[0];
73
+ }
74
+ }
75
+ return void 0;
76
+ };
49
77
  var toGraphQLType = (schema) => {
50
78
  const ast = schema.ast;
51
79
  if (ast._tag === "StringKeyword") return graphql.GraphQLString;
@@ -55,7 +83,7 @@ var toGraphQLType = (schema) => {
55
83
  if (isIntegerType(ast)) {
56
84
  return graphql.GraphQLInt;
57
85
  }
58
- return toGraphQLType(S2__namespace.make(ast.from));
86
+ return toGraphQLType(S__namespace.make(ast.from));
59
87
  }
60
88
  if (ast._tag === "Literal") {
61
89
  if (typeof ast.literal === "string") return graphql.GraphQLString;
@@ -67,7 +95,7 @@ var toGraphQLType = (schema) => {
67
95
  if (ast._tag === "TupleType") {
68
96
  const elements = ast.elements;
69
97
  if (elements.length > 0) {
70
- const elementSchema = S2__namespace.make(elements[0].type);
98
+ const elementSchema = S__namespace.make(elements[0].type);
71
99
  return new graphql.GraphQLList(toGraphQLType(elementSchema));
72
100
  }
73
101
  }
@@ -75,7 +103,7 @@ var toGraphQLType = (schema) => {
75
103
  const fields = {};
76
104
  for (const field2 of ast.propertySignatures) {
77
105
  const fieldName = String(field2.name);
78
- const fieldSchema = S2__namespace.make(field2.type);
106
+ const fieldSchema = S__namespace.make(field2.type);
79
107
  let fieldType = toGraphQLType(fieldSchema);
80
108
  if (!field2.isOptional) {
81
109
  fieldType = new graphql.GraphQLNonNull(fieldType);
@@ -89,17 +117,35 @@ var toGraphQLType = (schema) => {
89
117
  });
90
118
  }
91
119
  if (ast._tag === "Transformation") {
92
- return toGraphQLType(S2__namespace.make(ast.to));
120
+ if (isOptionTransformation(ast)) {
121
+ const innerType = getOptionInnerType(ast.to);
122
+ if (innerType) {
123
+ return toGraphQLType(S__namespace.make(innerType));
124
+ }
125
+ }
126
+ return toGraphQLType(S__namespace.make(ast.to));
127
+ }
128
+ if (ast._tag === "Declaration") {
129
+ if (isOptionDeclaration(ast)) {
130
+ const innerType = getOptionInnerType(ast);
131
+ if (innerType) {
132
+ return toGraphQLType(S__namespace.make(innerType));
133
+ }
134
+ }
135
+ const typeParams = ast.typeParameters;
136
+ if (typeParams && typeParams.length > 0) {
137
+ return toGraphQLType(S__namespace.make(typeParams[0]));
138
+ }
93
139
  }
94
140
  if (ast._tag === "Union") {
95
141
  const types = ast.types;
96
142
  if (types.length > 0) {
97
- return toGraphQLType(S2__namespace.make(types[0]));
143
+ return toGraphQLType(S__namespace.make(types[0]));
98
144
  }
99
145
  }
100
146
  if (ast._tag === "Suspend") {
101
147
  const innerAst = ast.f();
102
- return toGraphQLType(S2__namespace.make(innerAst));
148
+ return toGraphQLType(S__namespace.make(innerAst));
103
149
  }
104
150
  return graphql.GraphQLString;
105
151
  };
@@ -112,7 +158,7 @@ var toGraphQLInputType = (schema) => {
112
158
  if (isIntegerType(ast)) {
113
159
  return graphql.GraphQLInt;
114
160
  }
115
- return toGraphQLInputType(S2__namespace.make(ast.from));
161
+ return toGraphQLInputType(S__namespace.make(ast.from));
116
162
  }
117
163
  if (ast._tag === "Literal") {
118
164
  if (typeof ast.literal === "string") return graphql.GraphQLString;
@@ -124,7 +170,7 @@ var toGraphQLInputType = (schema) => {
124
170
  if (ast._tag === "TupleType") {
125
171
  const elements = ast.elements;
126
172
  if (elements.length > 0) {
127
- const elementSchema = S2__namespace.make(elements[0].type);
173
+ const elementSchema = S__namespace.make(elements[0].type);
128
174
  return new graphql.GraphQLList(toGraphQLInputType(elementSchema));
129
175
  }
130
176
  }
@@ -132,7 +178,7 @@ var toGraphQLInputType = (schema) => {
132
178
  const fields = {};
133
179
  for (const field2 of ast.propertySignatures) {
134
180
  const fieldName = String(field2.name);
135
- const fieldSchema = S2__namespace.make(field2.type);
181
+ const fieldSchema = S__namespace.make(field2.type);
136
182
  let fieldType = toGraphQLInputType(fieldSchema);
137
183
  if (!field2.isOptional) {
138
184
  fieldType = new graphql.GraphQLNonNull(fieldType);
@@ -146,27 +192,55 @@ var toGraphQLInputType = (schema) => {
146
192
  });
147
193
  }
148
194
  if (ast._tag === "Transformation") {
149
- return toGraphQLInputType(S2__namespace.make(ast.from));
195
+ return toGraphQLInputType(S__namespace.make(ast.from));
196
+ }
197
+ if (ast._tag === "Declaration") {
198
+ if (isOptionDeclaration(ast)) {
199
+ const innerType = getOptionInnerType(ast);
200
+ if (innerType) {
201
+ return toGraphQLInputType(S__namespace.make(innerType));
202
+ }
203
+ }
204
+ const typeParams = ast.typeParameters;
205
+ if (typeParams && typeParams.length > 0) {
206
+ return toGraphQLInputType(S__namespace.make(typeParams[0]));
207
+ }
150
208
  }
151
209
  if (ast._tag === "Union") {
152
210
  const types = ast.types;
211
+ const nonNullTypes = types.filter((t) => t._tag !== "Literal" || t.literal !== null).filter((t) => t._tag !== "UndefinedKeyword");
212
+ if (nonNullTypes.length > 0) {
213
+ return toGraphQLInputType(S__namespace.make(nonNullTypes[0]));
214
+ }
153
215
  if (types.length > 0) {
154
- return toGraphQLInputType(S2__namespace.make(types[0]));
216
+ return toGraphQLInputType(S__namespace.make(types[0]));
155
217
  }
156
218
  }
157
219
  if (ast._tag === "Suspend") {
158
220
  const innerAst = ast.f();
159
- return toGraphQLInputType(S2__namespace.make(innerAst));
221
+ return toGraphQLInputType(S__namespace.make(innerAst));
160
222
  }
161
223
  return graphql.GraphQLString;
162
224
  };
163
225
  var toGraphQLObjectType = (name, schema, additionalFields) => {
164
- const ast = schema.ast;
226
+ let ast = schema.ast;
227
+ while (ast._tag === "Transformation") {
228
+ ast = ast.to;
229
+ }
230
+ if (ast._tag === "Declaration") {
231
+ const typeParams = ast.typeParameters;
232
+ if (typeParams && typeParams.length > 0) {
233
+ ast = typeParams[0];
234
+ while (ast._tag === "Transformation") {
235
+ ast = ast.to;
236
+ }
237
+ }
238
+ }
165
239
  if (ast._tag === "TypeLiteral") {
166
240
  const fields = {};
167
241
  for (const field2 of ast.propertySignatures) {
168
242
  const fieldName = String(field2.name);
169
- const fieldSchema = S2__namespace.make(field2.type);
243
+ const fieldSchema = S__namespace.make(field2.type);
170
244
  let fieldType = toGraphQLType(fieldSchema);
171
245
  if (!field2.isOptional) {
172
246
  fieldType = new graphql.GraphQLNonNull(fieldType);
@@ -197,7 +271,7 @@ var toGraphQLArgs = (schema) => {
197
271
  const args = {};
198
272
  for (const field2 of ast.propertySignatures) {
199
273
  const fieldName = String(field2.name);
200
- const fieldSchema = S2__namespace.make(field2.type);
274
+ const fieldSchema = S__namespace.make(field2.type);
201
275
  let fieldType = toGraphQLInputType(fieldSchema);
202
276
  if (!field2.isOptional) {
203
277
  fieldType = new graphql.GraphQLNonNull(fieldType);
@@ -297,9 +371,21 @@ function toGraphQLTypeWithRegistry(schema, ctx) {
297
371
  if (ast._tag === "TupleType") {
298
372
  return handleTupleTypeAST(ast, ctx);
299
373
  }
374
+ if (ast._tag === "Declaration") {
375
+ if (isOptionDeclaration2(ast)) {
376
+ const innerType = getOptionInnerType2(ast);
377
+ if (innerType) {
378
+ return toGraphQLTypeWithRegistry(S__namespace.make(innerType), ctx);
379
+ }
380
+ }
381
+ const typeParams = ast.typeParameters;
382
+ if (typeParams && typeParams.length > 0) {
383
+ return toGraphQLTypeWithRegistry(S__namespace.make(typeParams[0]), ctx);
384
+ }
385
+ }
300
386
  if (ast._tag === "Suspend") {
301
387
  const innerAst = ast.f();
302
- return toGraphQLTypeWithRegistry(S2__namespace.make(innerAst), ctx);
388
+ return toGraphQLTypeWithRegistry(S__namespace.make(innerAst), ctx);
303
389
  }
304
390
  return toGraphQLType(schema);
305
391
  }
@@ -317,20 +403,48 @@ function findRegisteredInterface(schema, ast, ctx) {
317
403
  }
318
404
  return void 0;
319
405
  }
406
+ function isOptionDeclaration2(ast) {
407
+ if (ast._tag === "Declaration") {
408
+ const annotations = ast.annotations;
409
+ if (annotations) {
410
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
411
+ const typeConstructor = annotations[TypeConstructorSymbol];
412
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
413
+ return true;
414
+ }
415
+ }
416
+ }
417
+ return false;
418
+ }
419
+ function getOptionInnerType2(ast) {
420
+ if (ast._tag === "Declaration") {
421
+ const typeParams = ast.typeParameters;
422
+ if (typeParams && typeParams.length > 0) {
423
+ return typeParams[0];
424
+ }
425
+ }
426
+ return void 0;
427
+ }
320
428
  function handleTransformationAST(ast, ctx) {
321
429
  const toAst = ast.to;
430
+ if (isOptionDeclaration2(toAst)) {
431
+ const innerType = getOptionInnerType2(toAst);
432
+ if (innerType) {
433
+ return toGraphQLTypeWithRegistry(S__namespace.make(innerType), ctx);
434
+ }
435
+ }
322
436
  if (toAst._tag === "TupleType") {
323
437
  if (toAst.rest && toAst.rest.length > 0) {
324
- const elementSchema = S2__namespace.make(toAst.rest[0].type);
438
+ const elementSchema = S__namespace.make(toAst.rest[0].type);
325
439
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
326
440
  return new graphql.GraphQLList(elementType);
327
441
  } else if (toAst.elements.length > 0) {
328
- const elementSchema = S2__namespace.make(toAst.elements[0].type);
442
+ const elementSchema = S__namespace.make(toAst.elements[0].type);
329
443
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
330
444
  return new graphql.GraphQLList(elementType);
331
445
  }
332
446
  }
333
- return toGraphQLTypeWithRegistry(S2__namespace.make(ast.to), ctx);
447
+ return toGraphQLTypeWithRegistry(S__namespace.make(ast.to), ctx);
334
448
  }
335
449
  function handleUnionAST(ast, ctx) {
336
450
  const allLiterals = ast.types.every((t) => t._tag === "Literal");
@@ -342,9 +456,9 @@ function handleUnionAST(ast, ctx) {
342
456
  if (unionType2) return unionType2;
343
457
  }
344
458
  if (ast.types.length > 0) {
345
- return toGraphQLTypeWithRegistry(S2__namespace.make(ast.types[0]), ctx);
459
+ return toGraphQLTypeWithRegistry(S__namespace.make(ast.types[0]), ctx);
346
460
  }
347
- return toGraphQLType(S2__namespace.make(ast));
461
+ return toGraphQLType(S__namespace.make(ast));
348
462
  }
349
463
  function findEnumForLiteralUnion(types, ctx) {
350
464
  const literalValues = types.map((t) => String(t.literal)).sort();
@@ -387,15 +501,15 @@ function findEnumForLiteral(ast, ctx) {
387
501
  }
388
502
  function handleTupleTypeAST(ast, ctx) {
389
503
  if (ast.rest && ast.rest.length > 0) {
390
- const elementSchema = S2__namespace.make(ast.rest[0].type);
504
+ const elementSchema = S__namespace.make(ast.rest[0].type);
391
505
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
392
506
  return new graphql.GraphQLList(elementType);
393
507
  } else if (ast.elements && ast.elements.length > 0) {
394
- const elementSchema = S2__namespace.make(ast.elements[0].type);
508
+ const elementSchema = S__namespace.make(ast.elements[0].type);
395
509
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
396
510
  return new graphql.GraphQLList(elementType);
397
511
  }
398
- return toGraphQLType(S2__namespace.make(ast));
512
+ return toGraphQLType(S__namespace.make(ast));
399
513
  }
400
514
  function schemaToFields(schema, ctx) {
401
515
  let ast = schema.ast;
@@ -412,7 +526,7 @@ function schemaToFields(schema, ctx) {
412
526
  const fields = {};
413
527
  for (const field2 of ast.propertySignatures) {
414
528
  const fieldName = String(field2.name);
415
- const fieldSchema = S2__namespace.make(field2.type);
529
+ const fieldSchema = S__namespace.make(field2.type);
416
530
  let fieldType = toGraphQLTypeWithRegistry(fieldSchema, ctx);
417
531
  if (!field2.isOptional) {
418
532
  fieldType = getNonNull(fieldType);
@@ -429,7 +543,7 @@ function schemaToInputFields(schema, enumRegistry, inputRegistry, inputs, enums,
429
543
  const fields = {};
430
544
  for (const field2 of ast.propertySignatures) {
431
545
  const fieldName = String(field2.name);
432
- const fieldSchema = S2__namespace.make(field2.type);
546
+ const fieldSchema = S__namespace.make(field2.type);
433
547
  let fieldType = toGraphQLInputTypeWithRegistry(
434
548
  fieldSchema,
435
549
  enumRegistry,
@@ -471,7 +585,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
471
585
  if (ast._tag === "Transformation") {
472
586
  const toAst = ast.to;
473
587
  return toGraphQLInputTypeWithRegistry(
474
- S2__namespace.make(toAst),
588
+ S__namespace.make(toAst),
475
589
  enumRegistry,
476
590
  inputRegistry,
477
591
  inputs,
@@ -498,7 +612,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
498
612
  const nonUndefinedTypes = unionAst.types.filter((t) => t._tag !== "UndefinedKeyword");
499
613
  if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "Union") {
500
614
  return toGraphQLInputTypeWithRegistry(
501
- S2__namespace.make(nonUndefinedTypes[0]),
615
+ S__namespace.make(nonUndefinedTypes[0]),
502
616
  enumRegistry,
503
617
  inputRegistry,
504
618
  inputs,
@@ -508,7 +622,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
508
622
  }
509
623
  if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "TypeLiteral") {
510
624
  return toGraphQLInputTypeWithRegistry(
511
- S2__namespace.make(nonUndefinedTypes[0]),
625
+ S__namespace.make(nonUndefinedTypes[0]),
512
626
  enumRegistry,
513
627
  inputRegistry,
514
628
  inputs,
@@ -548,7 +662,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
548
662
  if (ast._tag === "Suspend") {
549
663
  const innerAst = ast.f();
550
664
  return toGraphQLInputTypeWithRegistry(
551
- S2__namespace.make(innerAst),
665
+ S__namespace.make(innerAst),
552
666
  enumRegistry,
553
667
  inputRegistry,
554
668
  inputs,
@@ -564,7 +678,7 @@ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs,
564
678
  const args = {};
565
679
  for (const field2 of ast.propertySignatures) {
566
680
  const fieldName = String(field2.name);
567
- const fieldSchema = S2__namespace.make(field2.type);
681
+ const fieldSchema = S__namespace.make(field2.type);
568
682
  let fieldType = toGraphQLInputTypeWithRegistry(
569
683
  fieldSchema,
570
684
  enumRegistry,
@@ -582,6 +696,29 @@ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs,
582
696
  }
583
697
  return toGraphQLArgs(schema);
584
698
  }
699
+ function isOptionSchema(schema) {
700
+ const ast = schema.ast;
701
+ if (ast._tag === "Transformation") {
702
+ const toAst = ast.to;
703
+ if (toAst._tag === "Declaration") {
704
+ const annotations = toAst.annotations;
705
+ if (annotations) {
706
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
707
+ const typeConstructor = annotations[TypeConstructorSymbol];
708
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
709
+ return true;
710
+ }
711
+ }
712
+ }
713
+ }
714
+ return false;
715
+ }
716
+ function encodeResolverOutput(schema, value) {
717
+ if (isOptionSchema(schema)) {
718
+ return effect.Effect.orDie(S__namespace.encode(schema)(value));
719
+ }
720
+ return effect.Effect.succeed(value);
721
+ }
585
722
  function applyDirectives(effect, directives, directiveRegistrations) {
586
723
  if (!directives) return effect;
587
724
  let wrapped = effect;
@@ -616,7 +753,8 @@ function buildField(config, ctx) {
616
753
  );
617
754
  const middlewareContext = { parent: _parent, args, info };
618
755
  effect$1 = applyMiddleware(effect$1, middlewareContext, ctx.middlewares);
619
- return await effect.Runtime.runPromise(context.runtime)(effect$1);
756
+ const result = await effect.Runtime.runPromise(context.runtime)(effect$1);
757
+ return await effect.Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, result));
620
758
  }
621
759
  };
622
760
  if (config.args) {
@@ -645,7 +783,8 @@ function buildObjectField(config, ctx) {
645
783
  );
646
784
  const middlewareContext = { parent, args, info };
647
785
  effect$1 = applyMiddleware(effect$1, middlewareContext, ctx.middlewares);
648
- return await effect.Runtime.runPromise(context.runtime)(effect$1);
786
+ const result = await effect.Runtime.runPromise(context.runtime)(effect$1);
787
+ return await effect.Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, result));
649
788
  }
650
789
  };
651
790
  if (config.args) {
@@ -680,13 +819,18 @@ function buildSubscriptionField(config, ctx) {
680
819
  return streamToAsyncIterator(stream, context.runtime);
681
820
  },
682
821
  // The resolve function transforms each yielded value
683
- // If no custom resolve is provided, return the payload directly
822
+ // If no custom resolve is provided, encode and return the payload directly
684
823
  resolve: config.resolve ? async (value, args, context, info) => {
685
824
  let effect$1 = config.resolve(value, args);
686
825
  const middlewareContext = { parent: value, args, info };
687
826
  effect$1 = applyMiddleware(effect$1, middlewareContext, ctx.middlewares);
688
- return await effect.Runtime.runPromise(context.runtime)(effect$1);
689
- } : (value) => value
827
+ const result = await effect.Runtime.runPromise(context.runtime)(effect$1);
828
+ return await effect.Runtime.runPromise(context.runtime)(
829
+ encodeResolverOutput(config.type, result)
830
+ );
831
+ } : async (value, _args, context) => {
832
+ return await effect.Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, value));
833
+ }
690
834
  };
691
835
  if (config.args) {
692
836
  fieldConfig.args = toGraphQLArgsWithRegistry(