2

I am building a simple React Native app. to test AppSync APIs. I am able to do queries, mutations ; but subscriptions don't seem to work. I am trying this out on an Android Emulator.

Here's how i am building my client and creating a subscription.

    const client = new AWSAppSyncClient({
      url: awsconfig.aws_appsync_graphqlEndpoint,
      region: awsconfig.aws_appsync_region,
      auth: {
        type: AUTH_TYPE.API_KEY, // or type: awsconfig.aws_appsync_authenticationType,
        apiKey: awsconfig.aws_appsync_apiKey,
      }
    });

    subscription = client.subscribe({ query: gql(onCreateBook) }).subscribe({
        next: data => {
          console.log("got a book--->");
        },
        error: error => {
          console.warn("errror getting book");
        }
      });

Here is my schema(relevant parts) & subscriptions gql(auto generated by codeGen)

Schema

type Book {
    title: String!
    description: String
}

type Mutation {
    createBook(input: CreateBookInput!): Book
    updateBook(input: UpdateBookInput!): Book
    deleteBook(input: DeleteBookInput!): Book
}

type Query {
    getBook(title: String!): Book
    listBooks(filter: TableBookFilterInput, limit: Int, nextToken: String): BookConnection
}

type Subscription {
    onCreateBook(title: String, description: String): Book
        @aws_subscribe(mutations: ["createBook"])
    onUpdateBook(title: String, description: String): Book
        @aws_subscribe(mutations: ["updateBook"])
    onDeleteBook(title: String, description: String): Book
        @aws_subscribe(mutations: ["deleteBook"])
}

subscriptions gql

// eslint-disable
// this is an auto generated file. This will be overwritten

export const onCreateBook = `subscription OnCreateBook($title: String, $description: String) {
  onCreateBook(title: $title, description: $description) {
    title
    description
  }
}
`;
export const onUpdateBook = `subscription OnUpdateBook($title: String, $description: String) {
  onUpdateBook(title: $title, description: $description) {
    title
    description
  }
}
`;
export const onDeleteBook = `subscription OnDeleteBook($title: String, $description: String) {
  onDeleteBook(title: $title, description: $description) {
    title
    description
  }
}
`;

Note : I have verified that subscriptions are working fine on firing a mutation in AWS Console, but i cant see any errors in ReactNative app.

nikel
  • 3,402
  • 11
  • 45
  • 71

0 Answers0