我正在构建一个简单的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 Console中的突变,但是我看不到ReactNative应用程序中的任何错误。