gql_websocket_link 2.0.1 copy "gql_websocket_link: ^2.0.1" to clipboard
gql_websocket_link: ^2.0.1 copied to clipboard

GQL Websocket Link

MIT License PRs Welcome Watch on GitHub Star on GitHub Watch on GitHub Discord

GQL WebSocket link to execute subscriptions as well as mutations & queries against a GraphQL backend. This link support autoReconnect and will resubscribe after reconnecting.

This package supports both the deprecated graphql-ws and the graphql-transport-ws protocol via the WebSocketLink and TransportWebSocketLink classes respectively.

Usage #

import "package:gql_link/gql_link.dart";
import "package:gql_websocket_link/gql_websocket_link.dart";

void main () {
  final link = Link.from([
    // SomeLink(),
    WebSocketLink("ws://<GRAPHQL_SERVER_ENDPOINT>/graphql"),
  ]);
}

import "package:gql_link/gql_link.dart";
import "package:gql_websocket_link/gql_websocket_link.dart";

void main () {
  final link = Link.from([
    // SomeLink(),
    TransportWebSocketLink(
      TransportWsClientOptions(
          socketMaker: WebSocketMaker.url(() => "ws://<GRAPHQL_SERVER_ENDPOINT>/graphql")
      ),
    ),
  ]);
}

Websocket Sub-Protocols And Their Dart Implementations #

There are two websocket sub-protocols available for GraphQL supported by this package:

  • graphql-ws (also known as subscriptions-transport-ws, which is the name of the npm package which implements it, spec])
  • graphql-transport-ws (confusingly, also known as graphql-ws, which is the name of the npm package which implements it spec)

In this document, we will refer to the protocols not by their npm package names, but by the name of the sub-protocol.

See also the Apollo docs.

Generally, you should use graphql-transport-ws if your server supports it.

In order to use graphql-transport-ws, you need to use the TransportWebSocketLink class. For graphql-ws, use the WebSocketLink class.

The WebSocketLink class has some known issues, see:

Features and bugs #

Please file feature requests and bugs at the GitHub.

Attribution #

This code was adapted with minor changed from the graphql-flutter repo