本文屬使用Prisma構(gòu)建GraphQL服務(wù)系列钧惧。
介紹
服務(wù)器端訂閱與正常的GraphQL訂閱相當(dāng)击狮。這意味著它們具有相同的API,例如允許提供相同的過濾器,以便僅通知您感興趣的事件湾盒。
當(dāng)設(shè)置服務(wù)器端訂閱時哲泊,Prisma將監(jiān)視數(shù)據(jù)更改并在適用時執(zhí)行關(guān)聯(lián)的查詢疤祭,就像正常的GraphQL訂閱一樣湾揽。不同的是交付機制。
服務(wù)器端訂閱旨在與現(xiàn)代無服務(wù)器基礎(chǔ)架構(gòu)配合使用踢关。目前伞鲫,Prisma支持通過webhook傳遞事件,并且將來我們將增加對直接AWS Lambda調(diào)用的支持以及不同的隊列實現(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
訂閱會被類似這樣的變化觸發(fā):
mutation {
updateUser(
data: { email: "new@email.com" },
where: { id: "cjcgo976g5twb018740bzyy4q" }
) {
id
}
}