Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example usage #1

Open
tukruic opened this issue Sep 2, 2021 · 1 comment
Open

Example usage #1

tukruic opened this issue Sep 2, 2021 · 1 comment

Comments

@tukruic
Copy link

tukruic commented Sep 2, 2021

For people looking for full working example
call initGraphQLChannel() from initstate

void initGraphQLChannel() async {
    // your absinthe plug api location
    final HttpLink phoenixHttpLink = HttpLink(
      'http://192.168.1.23:4000/api',
    );

    // your websocket location
    String websocketUri = "ws://192.168.1.23:4000/socket/websocket";

    // creation of phoenixChannel for use in PhoenixLink
    final phoenixChannel =
        await PhoenixLink.createChannel(websocketUri: websocketUri);

    // creation of PhoenixLink
    final phoenixLink = PhoenixLink(
      channel: phoenixChannel,
    );

    // Split like in flutter graphql documentation between websocket and http requests
    var _link = Link.split(
        (request) => request.isSubscription, phoenixLink, phoenixHttpLink);
    final GraphQLClient client = GraphQLClient(
      link: _link,
      cache: GraphQLCache(),
    );

    // your subscription query
    final subscriptionDocument = gql(
      r'''
    subscription{
  postAdded{
    id
    name
    description
  }
}
  ''',
    );

    // subscribe and listen
    Stream<QueryResult> subscription = client.subscribe(
      SubscriptionOptions(document: subscriptionDocument),
    );
    subscription.listen(reactToPostCreated);
  }

  void reactToPostCreated(QueryResult event) {
    print("Received event =>");
    print(event);
  }

  // example event received structure
  // Received event =>
  // I/flutter (15033): QueryResult(source: QueryResultSource.network, data: {postAdded: {__typename: Post, id: 32, name: graphqllink post, description: something something}}, context: Context({ResponseExtensions: Instance of 'ResponseExtensions'}), exception: null, timestamp: 2021-09-02 20:15:40.365330)
@knaeckeKami
Copy link
Owner

Thanks! I'll add that as example code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants