@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.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,7 @@ 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
+ const fieldSchema = S.make(field2.type);
52
80
  let fieldType = toGraphQLType(fieldSchema);
53
81
  if (!field2.isOptional) {
54
82
  fieldType = new GraphQLNonNull(fieldType);
@@ -62,17 +90,35 @@ var toGraphQLType = (schema) => {
62
90
  });
63
91
  }
64
92
  if (ast._tag === "Transformation") {
65
- return toGraphQLType(S2.make(ast.to));
93
+ if (isOptionTransformation(ast)) {
94
+ const innerType = getOptionInnerType(ast.to);
95
+ if (innerType) {
96
+ return toGraphQLType(S.make(innerType));
97
+ }
98
+ }
99
+ return toGraphQLType(S.make(ast.to));
100
+ }
101
+ if (ast._tag === "Declaration") {
102
+ if (isOptionDeclaration(ast)) {
103
+ const innerType = getOptionInnerType(ast);
104
+ if (innerType) {
105
+ return toGraphQLType(S.make(innerType));
106
+ }
107
+ }
108
+ const typeParams = ast.typeParameters;
109
+ if (typeParams && typeParams.length > 0) {
110
+ return toGraphQLType(S.make(typeParams[0]));
111
+ }
66
112
  }
67
113
  if (ast._tag === "Union") {
68
114
  const types = ast.types;
69
115
  if (types.length > 0) {
70
- return toGraphQLType(S2.make(types[0]));
116
+ return toGraphQLType(S.make(types[0]));
71
117
  }
72
118
  }
73
119
  if (ast._tag === "Suspend") {
74
120
  const innerAst = ast.f();
75
- return toGraphQLType(S2.make(innerAst));
121
+ return toGraphQLType(S.make(innerAst));
76
122
  }
77
123
  return GraphQLString;
78
124
  };
@@ -85,7 +131,7 @@ var toGraphQLInputType = (schema) => {
85
131
  if (isIntegerType(ast)) {
86
132
  return GraphQLInt;
87
133
  }
88
- return toGraphQLInputType(S2.make(ast.from));
134
+ return toGraphQLInputType(S.make(ast.from));
89
135
  }
90
136
  if (ast._tag === "Literal") {
91
137
  if (typeof ast.literal === "string") return GraphQLString;
@@ -97,7 +143,7 @@ var toGraphQLInputType = (schema) => {
97
143
  if (ast._tag === "TupleType") {
98
144
  const elements = ast.elements;
99
145
  if (elements.length > 0) {
100
- const elementSchema = S2.make(elements[0].type);
146
+ const elementSchema = S.make(elements[0].type);
101
147
  return new GraphQLList(toGraphQLInputType(elementSchema));
102
148
  }
103
149
  }
@@ -105,7 +151,7 @@ var toGraphQLInputType = (schema) => {
105
151
  const fields = {};
106
152
  for (const field2 of ast.propertySignatures) {
107
153
  const fieldName = String(field2.name);
108
- const fieldSchema = S2.make(field2.type);
154
+ const fieldSchema = S.make(field2.type);
109
155
  let fieldType = toGraphQLInputType(fieldSchema);
110
156
  if (!field2.isOptional) {
111
157
  fieldType = new GraphQLNonNull(fieldType);
@@ -119,17 +165,33 @@ var toGraphQLInputType = (schema) => {
119
165
  });
120
166
  }
121
167
  if (ast._tag === "Transformation") {
122
- return toGraphQLInputType(S2.make(ast.from));
168
+ return toGraphQLInputType(S.make(ast.from));
169
+ }
170
+ if (ast._tag === "Declaration") {
171
+ if (isOptionDeclaration(ast)) {
172
+ const innerType = getOptionInnerType(ast);
173
+ if (innerType) {
174
+ return toGraphQLInputType(S.make(innerType));
175
+ }
176
+ }
177
+ const typeParams = ast.typeParameters;
178
+ if (typeParams && typeParams.length > 0) {
179
+ return toGraphQLInputType(S.make(typeParams[0]));
180
+ }
123
181
  }
124
182
  if (ast._tag === "Union") {
125
183
  const types = ast.types;
184
+ const nonNullTypes = types.filter((t) => t._tag !== "Literal" || t.literal !== null).filter((t) => t._tag !== "UndefinedKeyword");
185
+ if (nonNullTypes.length > 0) {
186
+ return toGraphQLInputType(S.make(nonNullTypes[0]));
187
+ }
126
188
  if (types.length > 0) {
127
- return toGraphQLInputType(S2.make(types[0]));
189
+ return toGraphQLInputType(S.make(types[0]));
128
190
  }
129
191
  }
130
192
  if (ast._tag === "Suspend") {
131
193
  const innerAst = ast.f();
132
- return toGraphQLInputType(S2.make(innerAst));
194
+ return toGraphQLInputType(S.make(innerAst));
133
195
  }
134
196
  return GraphQLString;
135
197
  };
@@ -139,7 +201,7 @@ var toGraphQLArgs = (schema) => {
139
201
  const args = {};
140
202
  for (const field2 of ast.propertySignatures) {
141
203
  const fieldName = String(field2.name);
142
- const fieldSchema = S2.make(field2.type);
204
+ const fieldSchema = S.make(field2.type);
143
205
  let fieldType = toGraphQLInputType(fieldSchema);
144
206
  if (!field2.isOptional) {
145
207
  fieldType = new GraphQLNonNull(fieldType);
@@ -239,9 +301,21 @@ function toGraphQLTypeWithRegistry(schema, ctx) {
239
301
  if (ast._tag === "TupleType") {
240
302
  return handleTupleTypeAST(ast, ctx);
241
303
  }
304
+ if (ast._tag === "Declaration") {
305
+ if (isOptionDeclaration2(ast)) {
306
+ const innerType = getOptionInnerType2(ast);
307
+ if (innerType) {
308
+ return toGraphQLTypeWithRegistry(S.make(innerType), ctx);
309
+ }
310
+ }
311
+ const typeParams = ast.typeParameters;
312
+ if (typeParams && typeParams.length > 0) {
313
+ return toGraphQLTypeWithRegistry(S.make(typeParams[0]), ctx);
314
+ }
315
+ }
242
316
  if (ast._tag === "Suspend") {
243
317
  const innerAst = ast.f();
244
- return toGraphQLTypeWithRegistry(S2.make(innerAst), ctx);
318
+ return toGraphQLTypeWithRegistry(S.make(innerAst), ctx);
245
319
  }
246
320
  return toGraphQLType(schema);
247
321
  }
@@ -259,20 +333,48 @@ function findRegisteredInterface(schema, ast, ctx) {
259
333
  }
260
334
  return void 0;
261
335
  }
336
+ function isOptionDeclaration2(ast) {
337
+ if (ast._tag === "Declaration") {
338
+ const annotations = ast.annotations;
339
+ if (annotations) {
340
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
341
+ const typeConstructor = annotations[TypeConstructorSymbol];
342
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
343
+ return true;
344
+ }
345
+ }
346
+ }
347
+ return false;
348
+ }
349
+ function getOptionInnerType2(ast) {
350
+ if (ast._tag === "Declaration") {
351
+ const typeParams = ast.typeParameters;
352
+ if (typeParams && typeParams.length > 0) {
353
+ return typeParams[0];
354
+ }
355
+ }
356
+ return void 0;
357
+ }
262
358
  function handleTransformationAST(ast, ctx) {
263
359
  const toAst = ast.to;
360
+ if (isOptionDeclaration2(toAst)) {
361
+ const innerType = getOptionInnerType2(toAst);
362
+ if (innerType) {
363
+ return toGraphQLTypeWithRegistry(S.make(innerType), ctx);
364
+ }
365
+ }
264
366
  if (toAst._tag === "TupleType") {
265
367
  if (toAst.rest && toAst.rest.length > 0) {
266
- const elementSchema = S2.make(toAst.rest[0].type);
368
+ const elementSchema = S.make(toAst.rest[0].type);
267
369
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
268
370
  return new GraphQLList(elementType);
269
371
  } else if (toAst.elements.length > 0) {
270
- const elementSchema = S2.make(toAst.elements[0].type);
372
+ const elementSchema = S.make(toAst.elements[0].type);
271
373
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
272
374
  return new GraphQLList(elementType);
273
375
  }
274
376
  }
275
- return toGraphQLTypeWithRegistry(S2.make(ast.to), ctx);
377
+ return toGraphQLTypeWithRegistry(S.make(ast.to), ctx);
276
378
  }
277
379
  function handleUnionAST(ast, ctx) {
278
380
  const allLiterals = ast.types.every((t) => t._tag === "Literal");
@@ -284,9 +386,9 @@ function handleUnionAST(ast, ctx) {
284
386
  if (unionType2) return unionType2;
285
387
  }
286
388
  if (ast.types.length > 0) {
287
- return toGraphQLTypeWithRegistry(S2.make(ast.types[0]), ctx);
389
+ return toGraphQLTypeWithRegistry(S.make(ast.types[0]), ctx);
288
390
  }
289
- return toGraphQLType(S2.make(ast));
391
+ return toGraphQLType(S.make(ast));
290
392
  }
291
393
  function findEnumForLiteralUnion(types, ctx) {
292
394
  const literalValues = types.map((t) => String(t.literal)).sort();
@@ -329,15 +431,15 @@ function findEnumForLiteral(ast, ctx) {
329
431
  }
330
432
  function handleTupleTypeAST(ast, ctx) {
331
433
  if (ast.rest && ast.rest.length > 0) {
332
- const elementSchema = S2.make(ast.rest[0].type);
434
+ const elementSchema = S.make(ast.rest[0].type);
333
435
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
334
436
  return new GraphQLList(elementType);
335
437
  } else if (ast.elements && ast.elements.length > 0) {
336
- const elementSchema = S2.make(ast.elements[0].type);
438
+ const elementSchema = S.make(ast.elements[0].type);
337
439
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
338
440
  return new GraphQLList(elementType);
339
441
  }
340
- return toGraphQLType(S2.make(ast));
442
+ return toGraphQLType(S.make(ast));
341
443
  }
342
444
  function schemaToFields(schema, ctx) {
343
445
  let ast = schema.ast;
@@ -354,7 +456,7 @@ function schemaToFields(schema, ctx) {
354
456
  const fields = {};
355
457
  for (const field2 of ast.propertySignatures) {
356
458
  const fieldName = String(field2.name);
357
- const fieldSchema = S2.make(field2.type);
459
+ const fieldSchema = S.make(field2.type);
358
460
  let fieldType = toGraphQLTypeWithRegistry(fieldSchema, ctx);
359
461
  if (!field2.isOptional) {
360
462
  fieldType = getNonNull(fieldType);
@@ -371,7 +473,7 @@ function schemaToInputFields(schema, enumRegistry, inputRegistry, inputs, enums,
371
473
  const fields = {};
372
474
  for (const field2 of ast.propertySignatures) {
373
475
  const fieldName = String(field2.name);
374
- const fieldSchema = S2.make(field2.type);
476
+ const fieldSchema = S.make(field2.type);
375
477
  let fieldType = toGraphQLInputTypeWithRegistry(
376
478
  fieldSchema,
377
479
  enumRegistry,
@@ -413,7 +515,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
413
515
  if (ast._tag === "Transformation") {
414
516
  const toAst = ast.to;
415
517
  return toGraphQLInputTypeWithRegistry(
416
- S2.make(toAst),
518
+ S.make(toAst),
417
519
  enumRegistry,
418
520
  inputRegistry,
419
521
  inputs,
@@ -440,7 +542,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
440
542
  const nonUndefinedTypes = unionAst.types.filter((t) => t._tag !== "UndefinedKeyword");
441
543
  if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "Union") {
442
544
  return toGraphQLInputTypeWithRegistry(
443
- S2.make(nonUndefinedTypes[0]),
545
+ S.make(nonUndefinedTypes[0]),
444
546
  enumRegistry,
445
547
  inputRegistry,
446
548
  inputs,
@@ -450,7 +552,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
450
552
  }
451
553
  if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "TypeLiteral") {
452
554
  return toGraphQLInputTypeWithRegistry(
453
- S2.make(nonUndefinedTypes[0]),
555
+ S.make(nonUndefinedTypes[0]),
454
556
  enumRegistry,
455
557
  inputRegistry,
456
558
  inputs,
@@ -490,7 +592,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
490
592
  if (ast._tag === "Suspend") {
491
593
  const innerAst = ast.f();
492
594
  return toGraphQLInputTypeWithRegistry(
493
- S2.make(innerAst),
595
+ S.make(innerAst),
494
596
  enumRegistry,
495
597
  inputRegistry,
496
598
  inputs,
@@ -506,7 +608,7 @@ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs,
506
608
  const args = {};
507
609
  for (const field2 of ast.propertySignatures) {
508
610
  const fieldName = String(field2.name);
509
- const fieldSchema = S2.make(field2.type);
611
+ const fieldSchema = S.make(field2.type);
510
612
  let fieldType = toGraphQLInputTypeWithRegistry(
511
613
  fieldSchema,
512
614
  enumRegistry,
@@ -524,6 +626,29 @@ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs,
524
626
  }
525
627
  return toGraphQLArgs(schema);
526
628
  }
629
+ function isOptionSchema(schema) {
630
+ const ast = schema.ast;
631
+ if (ast._tag === "Transformation") {
632
+ const toAst = ast.to;
633
+ if (toAst._tag === "Declaration") {
634
+ const annotations = toAst.annotations;
635
+ if (annotations) {
636
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
637
+ const typeConstructor = annotations[TypeConstructorSymbol];
638
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
639
+ return true;
640
+ }
641
+ }
642
+ }
643
+ }
644
+ return false;
645
+ }
646
+ function encodeResolverOutput(schema, value) {
647
+ if (isOptionSchema(schema)) {
648
+ return Effect.orDie(S.encode(schema)(value));
649
+ }
650
+ return Effect.succeed(value);
651
+ }
527
652
  function applyDirectives(effect, directives, directiveRegistrations) {
528
653
  if (!directives) return effect;
529
654
  let wrapped = effect;
@@ -558,7 +683,8 @@ function buildField(config, ctx) {
558
683
  );
559
684
  const middlewareContext = { parent: _parent, args, info };
560
685
  effect = applyMiddleware(effect, middlewareContext, ctx.middlewares);
561
- return await Runtime.runPromise(context.runtime)(effect);
686
+ const result = await Runtime.runPromise(context.runtime)(effect);
687
+ return await Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, result));
562
688
  }
563
689
  };
564
690
  if (config.args) {
@@ -587,7 +713,8 @@ function buildObjectField(config, ctx) {
587
713
  );
588
714
  const middlewareContext = { parent, args, info };
589
715
  effect = applyMiddleware(effect, middlewareContext, ctx.middlewares);
590
- return await Runtime.runPromise(context.runtime)(effect);
716
+ const result = await Runtime.runPromise(context.runtime)(effect);
717
+ return await Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, result));
591
718
  }
592
719
  };
593
720
  if (config.args) {
@@ -622,13 +749,18 @@ function buildSubscriptionField(config, ctx) {
622
749
  return streamToAsyncIterator(stream, context.runtime);
623
750
  },
624
751
  // The resolve function transforms each yielded value
625
- // If no custom resolve is provided, return the payload directly
752
+ // If no custom resolve is provided, encode and return the payload directly
626
753
  resolve: config.resolve ? async (value, args, context, info) => {
627
754
  let effect = config.resolve(value, args);
628
755
  const middlewareContext = { parent: value, args, info };
629
756
  effect = applyMiddleware(effect, middlewareContext, ctx.middlewares);
630
- return await Runtime.runPromise(context.runtime)(effect);
631
- } : (value) => value
757
+ const result = await Runtime.runPromise(context.runtime)(effect);
758
+ return await Runtime.runPromise(context.runtime)(
759
+ encodeResolverOutput(config.type, result)
760
+ );
761
+ } : async (value, _args, context) => {
762
+ return await Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, value));
763
+ }
632
764
  };
633
765
  if (config.args) {
634
766
  fieldConfig.args = toGraphQLArgsWithRegistry(