介紹
服務(wù)器端訂閱與正常的GraphQL訂閱相當(dāng)。這意味著它們具有相同的API,例如允許提供相同的過濾器,以便僅通知您感興趣的事件。
當(dāng)設(shè)置服務(wù)器端訂閱時(shí),Prisma將監(jiān)視數(shù)據(jù)更改并在適用時(shí)執(zhí)行關(guān)聯(lián)的查詢,就像正常的GraphQL訂閱一樣。不同的是交付機(jī)制。
服務(wù)器端訂閱旨在與現(xiàn)代無服務(wù)器基礎(chǔ)架構(gòu)配合使用。目前,Prisma支持通過webhook傳遞事件,并且將來我們將增加對(duì)直接AWS Lambda調(diào)用的支持以及不同的隊(duì)列實(shí)現(xiàn)。
配置
您可以通過在您的服務(wù)的prisma.yml文件中添加subscriptions屬性來配置服務(wù)器端訂閱。
prisma.yml
service: my-service
stage: ${env:PRISMA_STAGE}
secret: ${env:PRISMA_SECRET}
cluster: ${env:PRISMA_CLUSTER}
datamodel: database/datamodel.graphql
subscriptions:
userChangedEmail:
webhook:
url: http://example.org/sendSlackMessage
headers:
Content-Type: application/json
Authorization: Bearer cha2eiheiphesash3shoofo7eceexaequeebuyaequ1reishiujuu6weisao7ohc
query: |
subscription {
user(where: {
mutation_in: [UPDATED]
}) {
node {
name
email
}
}
}
示例
上面配置的userChangedEmail訂閱會(huì)被類似這樣的變化觸發(fā):
mutation {
updateUser(
data: { email: "new@email.com" },
where: { id: "cjcgo976g5twb018740bzyy4q" }
) {
id
}
}