graphQLWS function

RequestHandler graphQLWS(
  1. GraphQL graphQL, {
  2. Duration? keepAliveInterval,
})

A RequestHandler that serves a spec-compliant GraphQL backend, over WebSockets. This endpoint only supports WebSockets, and can be used to deliver subscription events.

graphQLWS uses the Apollo WebSocket protocol, for the sake of compatibility with existing tooling.

See:

Implementation

RequestHandler graphQLWS(GraphQL graphQL, {Duration? keepAliveInterval}) {
  return (req, res) async {
    if (req is HttpRequestContext) {
      if (WebSocketTransformer.isUpgradeRequest(req.rawRequest!)) {
        await res.detach();
        var socket = await WebSocketTransformer.upgrade(req.rawRequest!,
            protocolSelector: (protocols) {
          if (protocols.contains('graphql-ws')) {
            return 'graphql-ws';
          } else {
            throw AngelHttpException.badRequest(
                message: 'Only the "graphql-ws" protocol is allowed.');
          }
        });
        var channel = IOWebSocketChannel(socket);
        var client = stw.RemoteClient(channel.cast<String>());
        var server =
            _GraphQLWSServer(client, graphQL, req, res, keepAliveInterval);
        await server.done;
      } else {
        throw AngelHttpException.badRequest(
            message: 'The `graphQLWS` endpoint only accepts WebSockets.');
      }
    } else {
      throw AngelHttpException.badRequest(
          message: 'The `graphQLWS` endpoint only accepts HTTP/1.1 requests.');
    }
  };
}