@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.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, Kind, specifiedRules, NoSchemaIntrospectionCustomRule, subscribe, GraphQLScalarType } from 'graphql';
2
2
  export { DirectiveLocation, GraphQLBoolean, GraphQLEnumType, GraphQLFloat, GraphQLID, GraphQLInputObjectType, GraphQLInt, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLScalarType, GraphQLSchema, GraphQLString, GraphQLUnionType, Kind, graphql, lexicographicSortSchema, printSchema } from 'graphql';
3
3
  import { Pipeable, Context, Data, Layer, Effect, Ref, HashMap, Config, Option, Schema, Runtime, Queue, Stream, Fiber, Deferred } from 'effect';
4
- import * as S2 from 'effect/Schema';
4
+ import * as S from 'effect/Schema';
5
5
  import * as AST from 'effect/SchemaAST';
6
6
  import DataLoader from 'dataloader';
7
7
  import { HttpIncomingMessage, HttpServerResponse, HttpServerRequest, HttpRouter } from '@effect/platform';
@@ -21,6 +21,34 @@ var isIntegerType = (ast) => {
21
21
  }
22
22
  return false;
23
23
  };
24
+ var isOptionDeclaration = (ast) => {
25
+ if (ast._tag === "Declaration") {
26
+ const annotations = ast.annotations;
27
+ if (annotations) {
28
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
29
+ const typeConstructor = annotations[TypeConstructorSymbol];
30
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
31
+ return true;
32
+ }
33
+ }
34
+ }
35
+ return false;
36
+ };
37
+ var isOptionTransformation = (ast) => {
38
+ if (ast._tag === "Transformation") {
39
+ return isOptionDeclaration(ast.to);
40
+ }
41
+ return false;
42
+ };
43
+ var getOptionInnerType = (ast) => {
44
+ if (ast._tag === "Declaration") {
45
+ const typeParams = ast.typeParameters;
46
+ if (typeParams && typeParams.length > 0) {
47
+ return typeParams[0];
48
+ }
49
+ }
50
+ return void 0;
51
+ };
24
52
  var toGraphQLType = (schema) => {
25
53
  const ast = schema.ast;
26
54
  if (ast._tag === "StringKeyword") return GraphQLString;
@@ -30,7 +58,7 @@ var toGraphQLType = (schema) => {
30
58
  if (isIntegerType(ast)) {
31
59
  return GraphQLInt;
32
60
  }
33
- return toGraphQLType(S2.make(ast.from));
61
+ return toGraphQLType(S.make(ast.from));
34
62
  }
35
63
  if (ast._tag === "Literal") {
36
64
  if (typeof ast.literal === "string") return GraphQLString;
@@ -42,7 +70,7 @@ var toGraphQLType = (schema) => {
42
70
  if (ast._tag === "TupleType") {
43
71
  const elements = ast.elements;
44
72
  if (elements.length > 0) {
45
- const elementSchema = S2.make(elements[0].type);
73
+ const elementSchema = S.make(elements[0].type);
46
74
  return new GraphQLList(toGraphQLType(elementSchema));
47
75
  }
48
76
  }
@@ -50,7 +78,7 @@ var toGraphQLType = (schema) => {
50
78
  const fields = {};
51
79
  for (const field2 of ast.propertySignatures) {
52
80
  const fieldName = String(field2.name);
53
- const fieldSchema = S2.make(field2.type);
81
+ const fieldSchema = S.make(field2.type);
54
82
  let fieldType = toGraphQLType(fieldSchema);
55
83
  if (!field2.isOptional) {
56
84
  fieldType = new GraphQLNonNull(fieldType);
@@ -64,17 +92,35 @@ var toGraphQLType = (schema) => {
64
92
  });
65
93
  }
66
94
  if (ast._tag === "Transformation") {
67
- return toGraphQLType(S2.make(ast.to));
95
+ if (isOptionTransformation(ast)) {
96
+ const innerType = getOptionInnerType(ast.to);
97
+ if (innerType) {
98
+ return toGraphQLType(S.make(innerType));
99
+ }
100
+ }
101
+ return toGraphQLType(S.make(ast.to));
102
+ }
103
+ if (ast._tag === "Declaration") {
104
+ if (isOptionDeclaration(ast)) {
105
+ const innerType = getOptionInnerType(ast);
106
+ if (innerType) {
107
+ return toGraphQLType(S.make(innerType));
108
+ }
109
+ }
110
+ const typeParams = ast.typeParameters;
111
+ if (typeParams && typeParams.length > 0) {
112
+ return toGraphQLType(S.make(typeParams[0]));
113
+ }
68
114
  }
69
115
  if (ast._tag === "Union") {
70
116
  const types = ast.types;
71
117
  if (types.length > 0) {
72
- return toGraphQLType(S2.make(types[0]));
118
+ return toGraphQLType(S.make(types[0]));
73
119
  }
74
120
  }
75
121
  if (ast._tag === "Suspend") {
76
122
  const innerAst = ast.f();
77
- return toGraphQLType(S2.make(innerAst));
123
+ return toGraphQLType(S.make(innerAst));
78
124
  }
79
125
  return GraphQLString;
80
126
  };
@@ -87,7 +133,7 @@ var toGraphQLInputType = (schema) => {
87
133
  if (isIntegerType(ast)) {
88
134
  return GraphQLInt;
89
135
  }
90
- return toGraphQLInputType(S2.make(ast.from));
136
+ return toGraphQLInputType(S.make(ast.from));
91
137
  }
92
138
  if (ast._tag === "Literal") {
93
139
  if (typeof ast.literal === "string") return GraphQLString;
@@ -99,7 +145,7 @@ var toGraphQLInputType = (schema) => {
99
145
  if (ast._tag === "TupleType") {
100
146
  const elements = ast.elements;
101
147
  if (elements.length > 0) {
102
- const elementSchema = S2.make(elements[0].type);
148
+ const elementSchema = S.make(elements[0].type);
103
149
  return new GraphQLList(toGraphQLInputType(elementSchema));
104
150
  }
105
151
  }
@@ -107,7 +153,7 @@ var toGraphQLInputType = (schema) => {
107
153
  const fields = {};
108
154
  for (const field2 of ast.propertySignatures) {
109
155
  const fieldName = String(field2.name);
110
- const fieldSchema = S2.make(field2.type);
156
+ const fieldSchema = S.make(field2.type);
111
157
  let fieldType = toGraphQLInputType(fieldSchema);
112
158
  if (!field2.isOptional) {
113
159
  fieldType = new GraphQLNonNull(fieldType);
@@ -121,27 +167,55 @@ var toGraphQLInputType = (schema) => {
121
167
  });
122
168
  }
123
169
  if (ast._tag === "Transformation") {
124
- 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
+ }
125
183
  }
126
184
  if (ast._tag === "Union") {
127
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
+ }
128
190
  if (types.length > 0) {
129
- return toGraphQLInputType(S2.make(types[0]));
191
+ return toGraphQLInputType(S.make(types[0]));
130
192
  }
131
193
  }
132
194
  if (ast._tag === "Suspend") {
133
195
  const innerAst = ast.f();
134
- return toGraphQLInputType(S2.make(innerAst));
196
+ return toGraphQLInputType(S.make(innerAst));
135
197
  }
136
198
  return GraphQLString;
137
199
  };
138
200
  var toGraphQLObjectType = (name, schema, additionalFields) => {
139
- const ast = schema.ast;
201
+ let ast = schema.ast;
202
+ while (ast._tag === "Transformation") {
203
+ ast = ast.to;
204
+ }
205
+ if (ast._tag === "Declaration") {
206
+ const typeParams = ast.typeParameters;
207
+ if (typeParams && typeParams.length > 0) {
208
+ ast = typeParams[0];
209
+ while (ast._tag === "Transformation") {
210
+ ast = ast.to;
211
+ }
212
+ }
213
+ }
140
214
  if (ast._tag === "TypeLiteral") {
141
215
  const fields = {};
142
216
  for (const field2 of ast.propertySignatures) {
143
217
  const fieldName = String(field2.name);
144
- const fieldSchema = S2.make(field2.type);
218
+ const fieldSchema = S.make(field2.type);
145
219
  let fieldType = toGraphQLType(fieldSchema);
146
220
  if (!field2.isOptional) {
147
221
  fieldType = new GraphQLNonNull(fieldType);
@@ -172,7 +246,7 @@ var toGraphQLArgs = (schema) => {
172
246
  const args = {};
173
247
  for (const field2 of ast.propertySignatures) {
174
248
  const fieldName = String(field2.name);
175
- const fieldSchema = S2.make(field2.type);
249
+ const fieldSchema = S.make(field2.type);
176
250
  let fieldType = toGraphQLInputType(fieldSchema);
177
251
  if (!field2.isOptional) {
178
252
  fieldType = new GraphQLNonNull(fieldType);
@@ -272,9 +346,21 @@ function toGraphQLTypeWithRegistry(schema, ctx) {
272
346
  if (ast._tag === "TupleType") {
273
347
  return handleTupleTypeAST(ast, ctx);
274
348
  }
349
+ if (ast._tag === "Declaration") {
350
+ if (isOptionDeclaration2(ast)) {
351
+ const innerType = getOptionInnerType2(ast);
352
+ if (innerType) {
353
+ return toGraphQLTypeWithRegistry(S.make(innerType), ctx);
354
+ }
355
+ }
356
+ const typeParams = ast.typeParameters;
357
+ if (typeParams && typeParams.length > 0) {
358
+ return toGraphQLTypeWithRegistry(S.make(typeParams[0]), ctx);
359
+ }
360
+ }
275
361
  if (ast._tag === "Suspend") {
276
362
  const innerAst = ast.f();
277
- return toGraphQLTypeWithRegistry(S2.make(innerAst), ctx);
363
+ return toGraphQLTypeWithRegistry(S.make(innerAst), ctx);
278
364
  }
279
365
  return toGraphQLType(schema);
280
366
  }
@@ -292,20 +378,48 @@ function findRegisteredInterface(schema, ast, ctx) {
292
378
  }
293
379
  return void 0;
294
380
  }
381
+ function isOptionDeclaration2(ast) {
382
+ if (ast._tag === "Declaration") {
383
+ const annotations = ast.annotations;
384
+ if (annotations) {
385
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
386
+ const typeConstructor = annotations[TypeConstructorSymbol];
387
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
388
+ return true;
389
+ }
390
+ }
391
+ }
392
+ return false;
393
+ }
394
+ function getOptionInnerType2(ast) {
395
+ if (ast._tag === "Declaration") {
396
+ const typeParams = ast.typeParameters;
397
+ if (typeParams && typeParams.length > 0) {
398
+ return typeParams[0];
399
+ }
400
+ }
401
+ return void 0;
402
+ }
295
403
  function handleTransformationAST(ast, ctx) {
296
404
  const toAst = ast.to;
405
+ if (isOptionDeclaration2(toAst)) {
406
+ const innerType = getOptionInnerType2(toAst);
407
+ if (innerType) {
408
+ return toGraphQLTypeWithRegistry(S.make(innerType), ctx);
409
+ }
410
+ }
297
411
  if (toAst._tag === "TupleType") {
298
412
  if (toAst.rest && toAst.rest.length > 0) {
299
- const elementSchema = S2.make(toAst.rest[0].type);
413
+ const elementSchema = S.make(toAst.rest[0].type);
300
414
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
301
415
  return new GraphQLList(elementType);
302
416
  } else if (toAst.elements.length > 0) {
303
- const elementSchema = S2.make(toAst.elements[0].type);
417
+ const elementSchema = S.make(toAst.elements[0].type);
304
418
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
305
419
  return new GraphQLList(elementType);
306
420
  }
307
421
  }
308
- return toGraphQLTypeWithRegistry(S2.make(ast.to), ctx);
422
+ return toGraphQLTypeWithRegistry(S.make(ast.to), ctx);
309
423
  }
310
424
  function handleUnionAST(ast, ctx) {
311
425
  const allLiterals = ast.types.every((t) => t._tag === "Literal");
@@ -317,9 +431,9 @@ function handleUnionAST(ast, ctx) {
317
431
  if (unionType2) return unionType2;
318
432
  }
319
433
  if (ast.types.length > 0) {
320
- return toGraphQLTypeWithRegistry(S2.make(ast.types[0]), ctx);
434
+ return toGraphQLTypeWithRegistry(S.make(ast.types[0]), ctx);
321
435
  }
322
- return toGraphQLType(S2.make(ast));
436
+ return toGraphQLType(S.make(ast));
323
437
  }
324
438
  function findEnumForLiteralUnion(types, ctx) {
325
439
  const literalValues = types.map((t) => String(t.literal)).sort();
@@ -362,15 +476,15 @@ function findEnumForLiteral(ast, ctx) {
362
476
  }
363
477
  function handleTupleTypeAST(ast, ctx) {
364
478
  if (ast.rest && ast.rest.length > 0) {
365
- const elementSchema = S2.make(ast.rest[0].type);
479
+ const elementSchema = S.make(ast.rest[0].type);
366
480
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
367
481
  return new GraphQLList(elementType);
368
482
  } else if (ast.elements && ast.elements.length > 0) {
369
- const elementSchema = S2.make(ast.elements[0].type);
483
+ const elementSchema = S.make(ast.elements[0].type);
370
484
  const elementType = toGraphQLTypeWithRegistry(elementSchema, ctx);
371
485
  return new GraphQLList(elementType);
372
486
  }
373
- return toGraphQLType(S2.make(ast));
487
+ return toGraphQLType(S.make(ast));
374
488
  }
375
489
  function schemaToFields(schema, ctx) {
376
490
  let ast = schema.ast;
@@ -387,7 +501,7 @@ function schemaToFields(schema, ctx) {
387
501
  const fields = {};
388
502
  for (const field2 of ast.propertySignatures) {
389
503
  const fieldName = String(field2.name);
390
- const fieldSchema = S2.make(field2.type);
504
+ const fieldSchema = S.make(field2.type);
391
505
  let fieldType = toGraphQLTypeWithRegistry(fieldSchema, ctx);
392
506
  if (!field2.isOptional) {
393
507
  fieldType = getNonNull(fieldType);
@@ -404,7 +518,7 @@ function schemaToInputFields(schema, enumRegistry, inputRegistry, inputs, enums,
404
518
  const fields = {};
405
519
  for (const field2 of ast.propertySignatures) {
406
520
  const fieldName = String(field2.name);
407
- const fieldSchema = S2.make(field2.type);
521
+ const fieldSchema = S.make(field2.type);
408
522
  let fieldType = toGraphQLInputTypeWithRegistry(
409
523
  fieldSchema,
410
524
  enumRegistry,
@@ -446,7 +560,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
446
560
  if (ast._tag === "Transformation") {
447
561
  const toAst = ast.to;
448
562
  return toGraphQLInputTypeWithRegistry(
449
- S2.make(toAst),
563
+ S.make(toAst),
450
564
  enumRegistry,
451
565
  inputRegistry,
452
566
  inputs,
@@ -473,7 +587,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
473
587
  const nonUndefinedTypes = unionAst.types.filter((t) => t._tag !== "UndefinedKeyword");
474
588
  if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "Union") {
475
589
  return toGraphQLInputTypeWithRegistry(
476
- S2.make(nonUndefinedTypes[0]),
590
+ S.make(nonUndefinedTypes[0]),
477
591
  enumRegistry,
478
592
  inputRegistry,
479
593
  inputs,
@@ -483,7 +597,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
483
597
  }
484
598
  if (nonUndefinedTypes.length === 1 && nonUndefinedTypes[0]._tag === "TypeLiteral") {
485
599
  return toGraphQLInputTypeWithRegistry(
486
- S2.make(nonUndefinedTypes[0]),
600
+ S.make(nonUndefinedTypes[0]),
487
601
  enumRegistry,
488
602
  inputRegistry,
489
603
  inputs,
@@ -523,7 +637,7 @@ function toGraphQLInputTypeWithRegistry(schema, enumRegistry, inputRegistry, inp
523
637
  if (ast._tag === "Suspend") {
524
638
  const innerAst = ast.f();
525
639
  return toGraphQLInputTypeWithRegistry(
526
- S2.make(innerAst),
640
+ S.make(innerAst),
527
641
  enumRegistry,
528
642
  inputRegistry,
529
643
  inputs,
@@ -539,7 +653,7 @@ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs,
539
653
  const args = {};
540
654
  for (const field2 of ast.propertySignatures) {
541
655
  const fieldName = String(field2.name);
542
- const fieldSchema = S2.make(field2.type);
656
+ const fieldSchema = S.make(field2.type);
543
657
  let fieldType = toGraphQLInputTypeWithRegistry(
544
658
  fieldSchema,
545
659
  enumRegistry,
@@ -557,6 +671,29 @@ function toGraphQLArgsWithRegistry(schema, enumRegistry, inputRegistry, inputs,
557
671
  }
558
672
  return toGraphQLArgs(schema);
559
673
  }
674
+ function isOptionSchema(schema) {
675
+ const ast = schema.ast;
676
+ if (ast._tag === "Transformation") {
677
+ const toAst = ast.to;
678
+ if (toAst._tag === "Declaration") {
679
+ const annotations = toAst.annotations;
680
+ if (annotations) {
681
+ const TypeConstructorSymbol = /* @__PURE__ */ Symbol.for("effect/annotation/TypeConstructor");
682
+ const typeConstructor = annotations[TypeConstructorSymbol];
683
+ if (typeConstructor && typeConstructor._tag === "effect/Option") {
684
+ return true;
685
+ }
686
+ }
687
+ }
688
+ }
689
+ return false;
690
+ }
691
+ function encodeResolverOutput(schema, value) {
692
+ if (isOptionSchema(schema)) {
693
+ return Effect.orDie(S.encode(schema)(value));
694
+ }
695
+ return Effect.succeed(value);
696
+ }
560
697
  function applyDirectives(effect, directives, directiveRegistrations) {
561
698
  if (!directives) return effect;
562
699
  let wrapped = effect;
@@ -591,7 +728,8 @@ function buildField(config, ctx) {
591
728
  );
592
729
  const middlewareContext = { parent: _parent, args, info };
593
730
  effect = applyMiddleware(effect, middlewareContext, ctx.middlewares);
594
- 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));
595
733
  }
596
734
  };
597
735
  if (config.args) {
@@ -620,7 +758,8 @@ function buildObjectField(config, ctx) {
620
758
  );
621
759
  const middlewareContext = { parent, args, info };
622
760
  effect = applyMiddleware(effect, middlewareContext, ctx.middlewares);
623
- return await Runtime.runPromise(context.runtime)(effect);
761
+ const result = await Runtime.runPromise(context.runtime)(effect);
762
+ return await Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, result));
624
763
  }
625
764
  };
626
765
  if (config.args) {
@@ -655,13 +794,18 @@ function buildSubscriptionField(config, ctx) {
655
794
  return streamToAsyncIterator(stream, context.runtime);
656
795
  },
657
796
  // The resolve function transforms each yielded value
658
- // If no custom resolve is provided, return the payload directly
797
+ // If no custom resolve is provided, encode and return the payload directly
659
798
  resolve: config.resolve ? async (value, args, context, info) => {
660
799
  let effect = config.resolve(value, args);
661
800
  const middlewareContext = { parent: value, args, info };
662
801
  effect = applyMiddleware(effect, middlewareContext, ctx.middlewares);
663
- return await Runtime.runPromise(context.runtime)(effect);
664
- } : (value) => value
802
+ const result = await Runtime.runPromise(context.runtime)(effect);
803
+ return await Runtime.runPromise(context.runtime)(
804
+ encodeResolverOutput(config.type, result)
805
+ );
806
+ } : async (value, _args, context) => {
807
+ return await Runtime.runPromise(context.runtime)(encodeResolverOutput(config.type, value));
808
+ }
665
809
  };
666
810
  if (config.args) {
667
811
  fieldConfig.args = toGraphQLArgsWithRegistry(