问题可能是由于 AppSync onUpdate 订阅规则中缺少正确的返回类型所导致的。在查询中添加返回值并在代码中使用正确的类型即可解决该问题。
以下是一个简单的代码示例,用于向 AWS AppSync 的 GraphQL API 发送 POST 请求并使用正确的类型进行解析:
import requests
import json
# POST request to AppSync endpoint
url = 'https://.appsync-api..amazonaws.com/graphql'
payload = {'query': 'mutation { updateMyType(input: {id: "xyz", name: "new-name"}) { id name } }'}
headers = {'Content-Type': 'application/graphql',
'x-api-key': ''}
response = requests.post(url, data=json.dumps(payload), headers=headers)
# Parse response
response_json = response.json()
if 'data' in response_json and 'updateMyType' in response_json['data']:
updated_type = response_json['data']['updateMyType']
print(f"Updated type ID: {updated_type['id']}, Name: {updated_type['name']}")
else:
print("Error occurred: ", response_json)
请注意,上面的示例代码中的
,
和 API_KEY
需要替换为正确的值。另外,"mutation" 应替换为正确的操作类型。
使用正确的类型和相应的返回值应该能够解决 AppSync onUpdate 订阅未被触发的问题。
上一篇:AppSync模式设计