1

我正在构建一个简单的 React Native 应用程序。测试 AppSync API。我能够进行查询、突变;但订阅似乎不起作用。我正在 Android 模拟器上尝试这个。

以下是我构建客户端和创建订阅的方式。

    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");
        }
      });

这是我的架构(相关部分)和订阅 gql(由 codeGen 自动生成)

架构

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"])
}

订阅 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
  }
}
`;

注意:我已经验证订阅在 AWS 控制台中触发突变时工作正常,但我在 ReactNative 应用程序中看不到任何错误。

4

0 回答 0