@effect-gql/core 1.1.0 → 1.1.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/server/index.js CHANGED
@@ -1,7 +1,6 @@
1
- import { Config, Option, Data, Context, Schema, Effect, Queue, Deferred, Stream, Ref, Runtime, Fiber } from 'effect';
1
+ import { Config, Option, Data, Context, Schema, Effect, Queue, Deferred, Stream, Ref, Fiber, Runtime } from 'effect';
2
2
  import { HttpIncomingMessage, HttpServerResponse, HttpServerRequest, HttpRouter } from '@effect/platform';
3
- import { Kind, GraphQLObjectType, parse, specifiedRules, NoSchemaIntrospectionCustomRule, validate, subscribe, GraphQLError, GraphQLNonNull, GraphQLList, GraphQLSchema, GraphQLScalarType, GraphQLEnumType, execute } from 'graphql';
4
- import { makeServer } from 'graphql-ws';
3
+ import { Kind, GraphQLObjectType, parse, specifiedRules, NoSchemaIntrospectionCustomRule, validate, GraphQLError, subscribe, GraphQLNonNull, GraphQLList, GraphQLSchema, GraphQLScalarType, GraphQLEnumType, execute } from 'graphql';
5
4
 
6
5
  // src/server/config.ts
7
6
  var defaultConfig = {
@@ -1226,27 +1225,45 @@ var runConnectionLifecycle = (socket, wsServer, extra) => Effect.gen(function* (
1226
1225
  Effect.catchAllCause(() => Effect.void),
1227
1226
  Effect.scoped
1228
1227
  );
1228
+ var importGraphqlWs = Effect.tryPromise({
1229
+ try: () => import('graphql-ws'),
1230
+ catch: () => new Error(
1231
+ "graphql-ws is required for WebSocket subscriptions. Install it with: npm install graphql-ws"
1232
+ )
1233
+ });
1229
1234
  var makeGraphQLWSHandler = (schema, layer, options) => {
1230
1235
  const complexityConfig = options?.complexity;
1231
1236
  const fieldComplexities = options?.fieldComplexities ?? /* @__PURE__ */ new Map();
1232
- const serverOptions = {
1233
- schema,
1234
- context: async (ctx) => {
1235
- const extra = ctx.extra;
1236
- return {
1237
- runtime: extra.runtime,
1238
- ...extra.connectionParams
1239
- };
1240
- },
1241
- subscribe: async (args) => subscribe(args),
1242
- onConnect: makeOnConnectHandler(options),
1243
- onDisconnect: makeOnDisconnectHandler(options),
1244
- onSubscribe: makeOnSubscribeHandler(options, schema, complexityConfig, fieldComplexities),
1245
- onComplete: makeOnCompleteHandler(options),
1246
- onError: makeOnErrorHandler(options)
1237
+ let wsServerPromise = null;
1238
+ const getOrCreateServer = async () => {
1239
+ if (!wsServerPromise) {
1240
+ wsServerPromise = Effect.runPromise(importGraphqlWs).then(({ makeServer }) => {
1241
+ const serverOptions = {
1242
+ schema,
1243
+ context: async (ctx) => {
1244
+ const extra = ctx.extra;
1245
+ return {
1246
+ runtime: extra.runtime,
1247
+ ...extra.connectionParams
1248
+ };
1249
+ },
1250
+ subscribe: async (args) => subscribe(args),
1251
+ onConnect: makeOnConnectHandler(options),
1252
+ onDisconnect: makeOnDisconnectHandler(options),
1253
+ onSubscribe: makeOnSubscribeHandler(options, schema, complexityConfig, fieldComplexities),
1254
+ onComplete: makeOnCompleteHandler(options),
1255
+ onError: makeOnErrorHandler(options)
1256
+ };
1257
+ return makeServer(serverOptions);
1258
+ });
1259
+ }
1260
+ return wsServerPromise;
1247
1261
  };
1248
- const wsServer = makeServer(serverOptions);
1249
1262
  return (socket) => Effect.gen(function* () {
1263
+ const wsServer = yield* Effect.tryPromise({
1264
+ try: () => getOrCreateServer(),
1265
+ catch: (error) => error
1266
+ });
1250
1267
  const runtime = yield* Effect.provide(Effect.runtime(), layer);
1251
1268
  const extra = {
1252
1269
  socket,