combinedLinkForNhostAuth function

Link combinedLinkForNhostAuth(
  1. String nhostGqlEndpointUrl,
  2. HasuraAuthClient nhostAuth, {
  3. Map<String, String>? defaultHeaders,
  4. Client? httpClientOverride,
})

Creates a link that that configures automatically based on nhostAuth's authentication state. The returned link will select HTTP or Web Socket transport as appropriate based on the GQL operation type.

nhostGqlEndpointUrl can be found at NhostClient.gqlEndpointUrl.

defaultHeaders (optional) A set of headers that will be provided with all requests passing through the link.

httpClientOverride (optional) can be provided to customize the network request, such as to configure proxies, introduce interceptors, etc.

Implementation

Link combinedLinkForNhostAuth(
  String nhostGqlEndpointUrl,
  HasuraAuthClient nhostAuth, {
  Map<String, String>? defaultHeaders,
  http.Client? httpClientOverride,
}) {
  return Link.split(
    (Request request) {
      final document = request.operation.document;
      final operationDefs =
          document.definitions.whereType<OperationDefinitionNode>().toList();
      final operationTypes = operationDefs.map((def) => def.type).toSet();

      log.finest(
          () => 'Issuing request, operations=${operationDefs.toLogString()}');

      // If any of the operations in the request are subscriptions, we forward
      // the entire request along to the websocket
      return operationTypes.contains(OperationType.subscription);
    },
    webSocketLinkForNhost(
      nhostGqlEndpointUrl,
      nhostAuth,
      defaultHeaders: defaultHeaders,
    ),
    httpLinkForNhost(
      nhostGqlEndpointUrl,
      nhostAuth,
      defaultHeaders: defaultHeaders,
      httpClientOverride: httpClientOverride,
    ),
  );
}