@effect-gql/core 1.3.4 → 1.4.1

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.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { GraphQLDirective, GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLObjectType, GraphQLUnionType, GraphQLSchema, GraphQLNonNull, GraphQLString, GraphQLFloat, GraphQLBoolean, GraphQLInt, GraphQLList, parse, GraphQLError, validate, execute as execute$1 } from 'graphql';
2
2
  export { DirectiveLocation } from 'graphql';
3
- import { Pipeable, Context, Runtime, Queue, Option, Effect, Stream, Fiber, Ref } from 'effect';
4
- import * as S2 from 'effect/Schema';
3
+ import { Pipeable, Context, Runtime, Effect, Queue, Option, Stream, Fiber, Ref } from 'effect';
4
+ import * as S from 'effect/Schema';
5
5
  import * as AST from 'effect/SchemaAST';
6
6
 
7
7
  // src/builder/index.ts
@@ -19,6 +19,34 @@ var isIntegerType = (ast) => {
19
19
  }
20
20
  return false;
21
21
  };
22
+ var isOptionDeclaration = (ast) => {
23
+ if (ast._tag === "Declaration") {
24
+ const annotations = ast.annotations;
25
+ if (annotations) {
26
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
27
+ const typeConstructor = annotations[TypeConstructorSymbol];
28
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
29
+ return true;
30
+ }
31
+ }
32
+ }
33
+ return false;
34
+ };
35
+ var isOptionTransformation = (ast) => {
36
+ if (ast._tag === "Transformation") {
37
+ return isOptionDeclaration(ast.to);
38
+ }
39
+ return false;
40
+ };
41
+ var getOptionInnerType = (ast) => {
42
+ if (ast._tag === "Declaration") {
43
+ const typeParams = ast.typeParameters;
44
+ if (typeParams && typeParams.length > 0) {
45
+ return typeParams[0];
46
+ }
47
+ }
48
+ return void 0;
49
+ };
22
50
  var toGraphQLType = (schema) => {
23
51
  const ast = schema.ast;
24
52
  if (ast._tag === "StringKeyword") return GraphQLString;
@@ -28,7 +56,7 @@ var toGraphQLType = (schema) => {
28
56
  if (isIntegerType(ast)) {
29
57
  return GraphQLInt;
30
58
  }
31
- return toGraphQLType(S2.make(ast.from));
59
+ return toGraphQLType(S.make(ast.from));
32
60
  }
33
61
  if (ast._tag === "Literal") {
34
62
  if (typeof ast.literal === "string") return GraphQLString;
@@ -40,7 +68,7 @@ var toGraphQLType = (schema) => {
40
68
  if (ast._tag === "TupleType") {
41
69
  const elements = ast.elements;
42
70
  if (elements.length > 0) {
43
- const elementSchema = S2.make(elements[0].type);
71
+ const elementSchema = S.make(elements[0].type);
44
72
  return new GraphQLList(toGraphQLType(elementSchema));
45
73
  }
46
74
  }
@@ -48,7 +76,8 @@ var toGraphQLType = (schema) => {
48
76
  const fields = {};
49
77
  for (const field2 of ast.propertySignatures) {
50
78
  const fieldName = String(field2.name);
51
- const fieldSchema = S2.make(field2.type);
79
+ if (fieldName === "_tag") continue;
80
+ const fieldSchema = S.make(field2.type);
52
81
  let fieldType = toGraphQLType(fieldSchema);
53
82
  if (!field2.isOptional) {
54
83
  fieldType = new GraphQLNonNull(fieldType);
@@ -62,17 +91,35 @@ var toGraphQLType = (schema) => {
62
91
  });
63
92
  }
64
93
  if (ast._tag === "Transformation") {
65
- return toGraphQLType(S2.make(ast.to));
94
+ if (isOptionTransformation(ast)) {
95
+ const innerType = getOptionInnerType(ast.to);
96
+ if (innerType) {
97
+ return toGraphQLType(S.make(innerType));
98
+ }
99
+ }
100
+ return toGraphQLType(S.make(ast.to));
101
+ }
102
+ if (ast._tag === "Declaration") {
103
+ if (isOptionDeclaration(ast)) {
104
+ const innerType = getOptionInnerType(ast);
105
+ if (innerType) {
106
+ return toGraphQLType(S.make(innerType));
107
+ }
108
+ }
109
+ const typeParams = ast.typeParameters;
110
+ if (typeParams && typeParams.length > 0) {
111
+ return toGraphQLType(S.make(typeParams[0]));
112
+ }
66
113
  }
67
114
  if (ast._tag === "Union") {
68
115
  const types = ast.types;
69
116
  if (types.length > 0) {
70
- return toGraphQLType(S2.make(types[0]));
117
+ return toGraphQLType(S.make(types[0]));
71
118
  }
72
119
  }
73
120
  if (ast._tag === "Suspend") {
74
121
  const innerAst = ast.f();
75
- return toGraphQLType(S2.make(innerAst));
122
+ return toGraphQLType(S.make(innerAst));
76
123
  }
77
124
  return GraphQLString;
78
125
  };
@@ -85,7 +132,7 @@ var toGraphQLInputType = (schema) => {
85
132
  if (isIntegerType(ast)) {
86
133
  return GraphQLInt;
87
134
  }
88
- return toGraphQLInputType(S2.make(ast.from));
135
+ return toGraphQLInputType(S.make(ast.from));
89
136
  }
90
137
  if (ast._tag === "Literal") {
91
138
  if (typeof ast.literal === "string") return GraphQLString;
@@ -97,7 +144,7 @@ var toGraphQLInputType = (schema) => {
97
144
  if (ast._tag === "TupleType") {
98
145
  const elements = ast.elements;
99
146
  if (elements.length > 0) {
100
- const elementSchema = S2.make(elements[0].type);
147
+ const elementSchema = S.make(elements[0].type);
101
148
  return new GraphQLList(toGraphQLInputType(elementSchema));
102
149
  }
103
150
  }
@@ -105,7 +152,8 @@ var toGraphQLInputType = (schema) => {
105
152
  const fields = {};
106
153
  for (const field2 of ast.propertySignatures) {
107
154
  const fieldName = String(field2.name);
108
- const fieldSchema = S2.make(field2.type);
155
+ if (fieldName === "_tag") continue;
156
+ const fieldSchema = S.make(field2.type);
109
157
  let fieldType = toGraphQLInputType(fieldSchema);
110
158
  if (!field2.isOptional) {
111
159
  fieldType = new GraphQLNonNull(fieldType);
@@ -119,17 +167,33 @@ var toGraphQLInputType = (schema) => {
119
167
  });
120
168
  }
121
169
  if (ast._tag === "Transformation") {
122
- return toGraphQLInputType(S2.make(ast.from));
170
+ return toGraphQLInputType(S.make(ast.from));
171
+ }
172
+ if (ast._tag === "Declaration") {
173
+ if (isOptionDeclaration(ast)) {
174
+ const innerType = getOptionInnerType(ast);
175
+ if (innerType) {
176
+ return toGraphQLInputType(S.make(innerType));
177
+ }
178
+ }
179
+ const typeParams = ast.typeParameters;
180
+ if (typeParams && typeParams.length > 0) {
181
+ return toGraphQLInputType(S.make(typeParams[0]));
182
+ }
123
183
  }
124
184
  if (ast._tag === "Union") {
125
185
  const types = ast.types;
186
+ const nonNullTypes = types.filter((t) => t._tag !== "Literal" || t.literal !== null).filter((t) => t._tag !== "UndefinedKeyword");
187
+ if (nonNullTypes.length > 0) {
188
+ return toGraphQLInputType(S.make(nonNullTypes[0]));
189
+ }
126
190
  if (types.length > 0) {
127
- return toGraphQLInputType(S2.make(types[0]));
191
+ return toGraphQLInputType(S.make(types[0]));
128
192
  }
129
193
  }
130
194
  if (ast._tag === "Suspend") {
131
195
  const innerAst = ast.f();
132
- return toGraphQLInputType(S2.make(innerAst));
196
+ return toGraphQLInputType(S.make(innerAst));
133
197
  }
134
198
  return GraphQLString;
135
199
  };
@@ -139,7 +203,8 @@ var toGraphQLArgs = (schema) => {
139
203
  const args = {};
140
204
  for (const field2 of ast.propertySignatures) {
141
205
  const fieldName = String(field2.name);
142
- const fieldSchema = S2.make(field2.type);
206
+ if (fieldName === "_tag") continue;
207
+ const fieldSchema = S.make(field2.type);
143
208
  let fieldType = toGraphQLInputType(fieldSchema);
144
209
  if (!field2.isOptional) {
145
210
  fieldType = new GraphQLNonNull(fieldType);
@@ -239,9 +304,21 @@ function toGraphQLTypeWithRegistry(schema, ctx) {
239
304
  if (ast._tag === "TupleType") {
240
305
  return handleTupleTypeAST(ast, ctx);
241
306
  }
307
+ if (ast._tag === "Declaration") {
308
+ if (isOptionDeclaration2(ast)) {
309
+ const innerType = getOptionInnerType2(ast);
310
+ if (innerType) {
311
+ return toGraphQLTypeWithRegistry(S.make(innerType), ctx);
312
+ }
313
+ }
314
+ const typeParams = ast.typeParameters;
315
+ if (typeParams && typeParams.length > 0) {
316
+ return toGraphQLTypeWithRegistry(S.make(typeParams[0]), ctx);
317
+ }
318
+ }
242
319
  if (ast._tag === "Suspend") {
243
320
  const innerAst = ast.f();
244
- return toGraphQLTypeWithRegistry(S2.make(innerAst), ctx);
321
+ return toGraphQLTypeWithRegistry(S.make(innerAst), ctx);
245
322
  }
246
323
  return toGraphQLType(schema);
247
324
  }
@@ -259,20 +336,48 @@ function findRegisteredInterface(schema, ast, ctx) {
259
336
  }
260
337
  return void 0;
261
338
  }
339
+ function isOptionDeclaration2(ast) {
340
+ if (ast._tag === "Declaration") {
341
+ const annotations = ast.annotations;
342
+ if (annotations) {
343
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
344
+ const typeConstructor = annotations[TypeConstructorSymbol];
345
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
346
+ return true;
347
+ }
348
+ }
349
+ }
350
+ return false;
351
+ }
352
+ function getOptionInnerType2(ast) {
353
+ if (ast._tag === "Declaration") {
354
+ const typeParams = ast.typeParameters;
355
+ if (typeParams && typeParams.length > 0) {
356
+ return typeParams[0];
357
+ }
358
+ }
359
+ return void 0;
360
+ }
262
361
  function handleTransformationAST(ast, ctx) {
263
362
  const toAst = ast.to;
363
+ if (isOptionDeclaration2(toAst)) {
364
+ const innerType = getOptionInnerType2(toAst);
365
+ if (innerType) {
366
+ return toGraphQLTypeWithRegistry(S.make(innerType), ctx);
367
+ }
368
+ }
264
369
  if (toAst._tag === "TupleType") {
265
370
  if (toAst.rest && toAst.rest.length > 0) {
266
- const elementSchema = S2.make(toAst.rest[0].type);
371
+ const elementSchema = S.make(toAst.rest[0].type);
267
372
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
268
373
  return new GraphQLList(elementType);
269
374
  } else if (toAst.elements.length > 0) {
270
- const elementSchema = S2.make(toAst.elements[0].type);
375
+ const elementSchema = S.make(toAst.elements[0].type);
271
376
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
272
377
  return new GraphQLList(elementType);
273
378
  }
274
379
  }
275
- return toGraphQLTypeWithRegistry(S2.make(ast.to), ctx);
380
+ return toGraphQLTypeWithRegistry(S.make(ast.to), ctx);
276
381
  }
277
382
  function handleUnionAST(ast, ctx) {
278
383
  const allLiterals = ast.types.every((t) => t._tag === "Literal");
@@ -284,9 +389,9 @@ function handleUnionAST(ast, ctx) {
284
389
  if (unionType2) return unionType2;
285
390
  }
286
391
  if (ast.types.length > 0) {
287
- return toGraphQLTypeWithRegistry(S2.make(ast.types[0]), ctx);
392
+ return toGraphQLTypeWithRegistry(S.make(ast.types[0]), ctx);
288
393
  }
289
- return toGraphQLType(S2.make(ast));
394
+ return toGraphQLType(S.make(ast));
290
395
  }
291
396
  function findEnumForLiteralUnion(types, ctx) {
292
397
  const literalValues = types.map((t) => String(t.literal)).sort();
@@ -329,15 +434,15 @@ function findEnumForLiteral(ast, ctx) {
329
434
  }
330
435
  function handleTupleTypeAST(ast, ctx) {
331
436
  if (ast.rest && ast.rest.length > 0) {
332
- const elementSchema = S2.make(ast.rest[0].type);
437
+ const elementSchema = S.make(ast.rest[0].type);
333
438
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
334
439
  return new GraphQLList(elementType);
335
440
  } else if (ast.elements && ast.elements.length > 0) {
336
- const elementSchema = S2.make(ast.elements[0].type);
441
+ const elementSchema = S.make(ast.elements[0].type);
337
442
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
338
443
  return new GraphQLList(elementType);
339
444
  }
340
- return toGraphQLType(S2.make(ast));
445
+ return toGraphQLType(S.make(ast));
341
446
  }
342
447
  function schemaToFields(schema, ctx) {
343
448
  let ast = schema.ast;
@@ -346,15 +451,22 @@ function schemaToFields(schema, ctx) {
346
451
  }
347
452
  if (ast._tag === "Declaration") {
348
453
  const typeParams = ast.typeParameters;
349
- if (typeParams && typeParams.length > 0 && typeParams[0]._tag === "TypeLiteral") {
350
- ast = typeParams[0];
454
+ if (typeParams && typeParams.length > 0) {
455
+ let innerAst = typeParams[0];
456
+ while (innerAst._tag === "Transformation") {
457
+ innerAst = innerAst.to;
458
+ }
459
+ if (innerAst._tag === "TypeLiteral") {
460
+ ast = innerAst;
461
+ }
351
462
  }
352
463
  }
353
464
  if (ast._tag === "TypeLiteral") {
354
465
  const fields = {};
355
466
  for (const field2 of ast.propertySignatures) {
356
467
  const fieldName = String(field2.name);
357
- const fieldSchema = S2.make(field2.type);
468
+ if (fieldName === "_tag") continue;
469
+ const fieldSchema = S.make(field2.type);
358
470
  let fieldType = toGraphQLTypeWithRegistry(fieldSchema, ctx);
359
471
  if (!field2.isOptional) {
360
472
  fieldType = getNonNull(fieldType);
@@ -366,12 +478,16 @@ function schemaToFields(schema, ctx) {
366
478
  return {};
367
479
  }
368
480
  function schemaToInputFields(schema, enumRegistry, inputRegistry, inputs, enums, cache) {
369
- const ast = schema.ast;
481
+ let ast = schema.ast;
482
+ while (ast._tag === "Transformation") {
483
+ ast = ast.to;
484
+ }
370
485
  if (ast._tag === "TypeLiteral") {
371
486
  const fields = {};
372
487
  for (const field2 of ast.propertySignatures) {
373
488
  const fieldName = String(field2.name);
374
- const fieldSchema = S2.make(field2.type);
489
+ if (fieldName === "_tag") continue;
490
+ const fieldSchema = S.make(field2.type);
375
491
  let fieldType = toGraphQLInputTypeWithRegistry(
376
492
  fieldSchema,
377
493
  enumRegistry,
@@ -410,17 +526,6 @@ function buildInputTypeLookupCache(inputs, enums) {
410
526
  }
411
527
  function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inputs, enums, cache) {
412
528
  const ast = schema.ast;
413
- if (ast._tag === "Transformation") {
414
- const toAst = ast.to;
415
- return toGraphQLInputTypeWithRegistry(
416
- S2.make(toAst),
417
- enumRegistry,
418
- inputRegistry,
419
- inputs,
420
- enums,
421
- cache
422
- );
423
- }
424
529
  if (cache?.schemaToInputName || cache?.astToInputName) {
425
530
  const inputName = cache.schemaToInputName?.get(schema) ?? cache.astToInputName?.get(ast);
426
531
  if (inputName) {
@@ -435,12 +540,23 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
435
540
  }
436
541
  }
437
542
  }
543
+ if (ast._tag === "Transformation") {
544
+ const toAst = ast.to;
545
+ return toGraphQLInputTypeWithRegistry(
546
+ S.make(toAst),
547
+ enumRegistry,
548
+ inputRegistry,
549
+ inputs,
550
+ enums,
551
+ cache
552
+ );
553
+ }
438
554
  if (ast._tag === "Union") {
439
555
  const unionAst = ast;
440
556
  const nonUndefinedTypes = unionAst.types.filter((t) => t._tag !== "UndefinedKeyword");
441
557
  if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "Union") {
442
558
  return toGraphQLInputTypeWithRegistry(
443
- S2.make(nonUndefinedTypes[0]),
559
+ S.make(nonUndefinedTypes[0]),
444
560
  enumRegistry,
445
561
  inputRegistry,
446
562
  inputs,
@@ -450,7 +566,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
450
566
  }
451
567
  if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "TypeLiteral") {
452
568
  return toGraphQLInputTypeWithRegistry(
453
- S2.make(nonUndefinedTypes[0]),
569
+ S.make(nonUndefinedTypes[0]),
454
570
  enumRegistry,
455
571
  inputRegistry,
456
572
  inputs,
@@ -490,7 +606,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
490
606
  if (ast._tag === "Suspend") {
491
607
  const innerAst = ast.f();
492
608
  return toGraphQLInputTypeWithRegistry(
493
- S2.make(innerAst),
609
+ S.make(innerAst),
494
610
  enumRegistry,
495
611
  inputRegistry,
496
612
  inputs,
@@ -506,7 +622,8 @@ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs,
506
622
  const args = {};
507
623
  for (const field2 of ast.propertySignatures) {
508
624
  const fieldName = String(field2.name);
509
- const fieldSchema = S2.make(field2.type);
625
+ if (fieldName === "_tag") continue;
626
+ const fieldSchema = S.make(field2.type);
510
627
  let fieldType = toGraphQLInputTypeWithRegistry(
511
628
  fieldSchema,
512
629
  enumRegistry,
@@ -524,6 +641,29 @@ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs,
524
641
  }
525
642
  return toGraphQLArgs(schema);
526
643
  }
644
+ function isOptionSchema(schema) {
645
+ const ast = schema.ast;
646
+ if (ast._tag === "Transformation") {
647
+ const toAst = ast.to;
648
+ if (toAst._tag === "Declaration") {
649
+ const annotations = toAst.annotations;
650
+ if (annotations) {
651
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
652
+ const typeConstructor = annotations[TypeConstructorSymbol];
653
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
654
+ return true;
655
+ }
656
+ }
657
+ }
658
+ }
659
+ return false;
660
+ }
661
+ function encodeResolverOutput(schema, value) {
662
+ if (isOptionSchema(schema)) {
663
+ return Effect.orDie(S.encode(schema)(value));
664
+ }
665
+ return Effect.succeed(value);
666
+ }
527
667
  function applyDirectives(effect, directives, directiveRegistrations) {
528
668
  if (!directives) return effect;
529
669
  let wrapped = effect;
@@ -558,7 +698,8 @@ function buildField(config, ctx) {
558
698
  );
559
699
  const middlewareContext = { parent: _parent, args, info };
560
700
  effect = applyMiddleware(effect, middlewareContext, ctx.middlewares);
561
- return await Runtime.runPromise(context.runtime)(effect);
701
+ const result = await Runtime.runPromise(context.runtime)(effect);
702
+ return await Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, result));
562
703
  }
563
704
  };
564
705
  if (config.args) {
@@ -587,7 +728,8 @@ function buildObjectField(config, ctx) {
587
728
  );
588
729
  const middlewareContext = { parent, args, info };
589
730
  effect = applyMiddleware(effect, middlewareContext, ctx.middlewares);
590
- return await Runtime.runPromise(context.runtime)(effect);
731
+ const result = await Runtime.runPromise(context.runtime)(effect);
732
+ return await Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, result));
591
733
  }
592
734
  };
593
735
  if (config.args) {
@@ -622,13 +764,18 @@ function buildSubscriptionField(config, ctx) {
622
764
  return streamToAsyncIterator(stream, context.runtime);
623
765
  },
624
766
  // The resolve function transforms each yielded value
625
- // If no custom resolve is provided, return the payload directly
767
+ // If no custom resolve is provided, encode and return the payload directly
626
768
  resolve: config.resolve ? async (value, args, context, info) => {
627
769
  let effect = config.resolve(value, args);
628
770
  const middlewareContext = { parent: value, args, info };
629
771
  effect = applyMiddleware(effect, middlewareContext, ctx.middlewares);
630
- return await Runtime.runPromise(context.runtime)(effect);
631
- } : (value) => value
772
+ const result = await Runtime.runPromise(context.runtime)(effect);
773
+ return await Runtime.runPromise(context.runtime)(
774
+ encodeResolverOutput(config.type, result)
775
+ );
776
+ } : async (value, _args, context) => {
777
+ return await Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, value));
778
+ }
632
779
  };
633
780
  if (config.args) {
634
781
  fieldConfig.args = toGraphQLArgsWithRegistry(