Graphql-flutter: Subscription using https://pub.dev/packages/graphql

Created on 23 Oct 2020  路  3Comments  路  Source: zino-app/graphql-flutter

Hi, are there examples on how to setup and subscribe to graphql subscription using https://pub.dev/packages/graphql? Thanks!

Most helpful comment

Just found out myself how to do this. Use WebSocketLink instead of HttpLink:

GraphQLClient _client = GraphQLClient(
  cache: InMemoryCache(),
  link: WebSocketLink(url: "ws://foo.bar"),
);

final QueryOptions options = QueryOptions(
  documentNode: gql(<insert subcription query here>),
  variables: <insert optional variables here>,
);

final Operation operation = Operation.fromOptions(options);

final Stream<FetchResult> result = _client.subscribe(operation);

Hope this helps you in the right direction.

All 3 comments

Just found out myself how to do this. Use WebSocketLink instead of HttpLink:

GraphQLClient _client = GraphQLClient(
  cache: InMemoryCache(),
  link: WebSocketLink(url: "ws://foo.bar"),
);

final QueryOptions options = QueryOptions(
  documentNode: gql(<insert subcription query here>),
  variables: <insert optional variables here>,
);

final Operation operation = Operation.fromOptions(options);

final Stream<FetchResult> result = _client.subscribe(operation);

Hope this helps you in the right direction.

@phijma thanks!

Also note that on v4 it is important to split links by type: link = Link.split((request) => request.isSubscription, websocketLink, link);
see: https://github.com/zino-app/graphql-flutter/tree/beta/packages/graphql#composing-links

Was this page helpful?
0 / 5 - 0 ratings