Enghreifftiau Tanysgrifio
Defnyddiwch danysgrifiadau GraphQL ar gyfer diweddariadau amser real defnyddwyr a grwpiau.
Am y model digwyddiad a chynllun y pecyn, gweler:
- Cysylltu â Thanysgrifiadau AppSync
- Fformat Digwyddiad Tanysgrifio
- Cynllun Digwyddiad Tanysgrifio
- Defnyddio Tanysgrifiadau
- Dilysu Oes Dogfen a Derbynnydd gyda Thanysgrifiadau
Tanysgrifiad Ffrydiau Defnyddiwr
subscription SubscribeUserFeed {
subscribeUserFeed {
userId
data
}
}
Tanysgrifiad Ffrydiau Grŵp
subscription SubscribeGroupFeed($groupId: String!) {
subscribeGroupFeed(groupId: $groupId) {
groupId
data
}
}
Enghraifft JavaScript
import { createClient } from 'graphql-ws';
import WebSocket from 'ws';
const client = createClient({
url: 'wss://graphql.uk.legalesign.com/graphql',
webSocketImpl: WebSocket,
connectionParams: {
Authorization: `Bearer ${accessToken}`,
},
});
const parseEnvelope = (payload) => {
const raw = payload?.data;
if (!raw) return null;
return typeof raw === 'string' ? JSON.parse(raw) : raw;
};
client.subscribe(
{
query: `
subscription SubscribeUserFeed {
subscribeUserFeed {
userId
data
}
}
`,
},
{
next: ({ data }) => {
const wrapper = data?.subscribeUserFeed;
const event = parseEnvelope(wrapper);
console.log('user event', {
deliveryUserId: wrapper?.userId,
eventId: event?.eventId,
category: event?.category,
event: event?.event,
payload: event?.data,
});
},
error: console.error,
complete: () => console.log('user feed closed'),
}
);
client.subscribe(
{
query: `
subscription SubscribeGroupFeed($groupId: String!) {
subscribeGroupFeed(groupId: $groupId) {
groupId
data
}
}
`,
variables: { groupId: 'your-group-id' },
},
{
next: ({ data }) => {
const wrapper = data?.subscribeGroupFeed;
const event = parseEnvelope(wrapper);
console.log('group event', {
deliveryGroupId: wrapper?.groupId,
eventId: event?.eventId,
category: event?.category,
event: event?.event,
payload: event?.data,
});
},
error: console.error,
complete: () => console.log('group feed closed'),
}
);