Aws-mobile-appsync-sdk-js: Dealing with simple subscriptions (without .watchQuery)

Created on 27 Jul 2018  路  2Comments  路  Source: awslabs/aws-mobile-appsync-sdk-js

Is there a chance to subscribe to a GraphQLsubscription without prior .watchQuery?
I haven't found (or maybe missed?) any suitable function within the hydrated()
All subscriptions I saw work in pair with queries, like this:

  this.appsync.hydrated().then(client => {
    const obs = client.watchQuery({
      query: query,
      otherOptions: ...
    });
    obs.subscribe(dosome);
    obs.subscribeToMore({
      document: subscription, <-- need only this
      otherOptions: ...
    });
  });

but I have no query...
Would be appreciated for any help/hints.

question

Most helpful comment

Hi @GeorgiyZhuravlev

There is, you can try something like this:

import AWSAppSyncClient, { AUTH_TYPE } from "aws-appsync";
import gql from "graphql-tag";

const client = new AWSAppSyncClient({
  region: "us-east-1",
  url:
    "https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql",
  auth: {
    type: AUTH_TYPE.API_KEY,
    apiKey: "da2-xxxxxx"
  }
});

client
  .subscribe({
    query: gql`subscription S {
  subscribeToEventComments(
    eventId: "ba31613f-5a86-4b96-9d25-c2647ccd654c"
  ) {
    eventId
    commentId
    content
    createdAt
  }
}`
  })
  .subscribe({
    next: console.log
  });

All 2 comments

Hi @GeorgiyZhuravlev

There is, you can try something like this:

import AWSAppSyncClient, { AUTH_TYPE } from "aws-appsync";
import gql from "graphql-tag";

const client = new AWSAppSyncClient({
  region: "us-east-1",
  url:
    "https://xxxxxx.appsync-api.us-east-1.amazonaws.com/graphql",
  auth: {
    type: AUTH_TYPE.API_KEY,
    apiKey: "da2-xxxxxx"
  }
});

client
  .subscribe({
    query: gql`subscription S {
  subscribeToEventComments(
    eventId: "ba31613f-5a86-4b96-9d25-c2647ccd654c"
  ) {
    eventId
    commentId
    content
    createdAt
  }
}`
  })
  .subscribe({
    next: console.log
  });

I am unable to get the subscription to fire when an item is created.

Any advice for an onCreate subscription.

Was this page helpful?
0 / 5 - 0 ratings