Cysylltu â Thanysgrifio AppSync
Dangosir y canllaw hwn sut i gysylltu â phwynt diwedd websocket Legalesign AppSync a dechrau defnyddio tanysgrifio GraphQL.
Defnyddiwch y dudalen hon ar gyfer gosod cysylltiad.
Defnyddiwch y tudalennau hyn ar gyfer trin y llwyth:
Cyn i Chi Ddechrau
Mae angen ichi:
- tocyn mynediad dilys
- pwynt diwedd GraphQL Legalesign
- cleient GraphQL sy'n gallu defnyddio websocket
Cael tocyn mynediad yn gyntaf:
Pwyntiau Diwedd
Pwynt diwedd HTTP GraphQL:
https://graphql.uk.legalesign.com/graphql
Pwynt diwedd websocket:
wss://graphql.uk.legalesign.com/graphql
Awduryddu
Pasio eich tocyn mynediad wrth agor y cysylltiad websocket.
Paramedrau cysylltiad arferol:
{
"Authorization": "Bearer <access-token>"
}
Gweithrediadau Tanysgrifio
Llif Defnyddiwr
subscription SubscribeUserFeed {
subscribeUserFeed {
userId
data
}
}
Llif Grŵp
subscription SubscribeGroupFeed($groupId: String!) {
subscribeGroupFeed(groupId: $groupId) {
groupId
data
}
}
Enghraifft JavaScript
Mae’r enghraifft hon yn defnyddio graphql-ws:
import { createClient } from 'graphql-ws';
import WebSocket from 'ws';
const accessToken = '<access-token>';
const client = createClient({
url: 'wss://graphql.uk.legalesign.com/graphql',
webSocketImpl: WebSocket,
connectionParams: {
Authorization: `Bearer ${accessToken}`,
},
});
const unsubscribeUser = client.subscribe(
{
query: `
subscription SubscribeUserFeed {
subscribeUserFeed {
userId
data
}
}
`,
},
{
next: ({ data }) => {
console.log('user feed message', data);
},
error: (err) => {
console.error('user feed error', err);
},
complete: () => {
console.log('user feed closed');
},
}
);
const unsubscribeGroup = client.subscribe(
{
query: `
subscription SubscribeGroupFeed($groupId: String!) {
subscribeGroupFeed(groupId: $groupId) {
groupId
data
}
}
`,
variables: {
groupId: '<your-group-id>',
},
},
{
next: ({ data }) => {
console.log('group feed message', data);
},
error: (err) => {
console.error('group feed error', err);
},
complete: () => {
console.log('group feed closed');
},
}
);
// Later:
// unsubscribeUser();
// unsubscribeGroup();
Dadansoddi’r Llwyth Mewnol
Mae’r maes tanysgrifio yn dychwelyd capsiwn ynghyd â chyfrinair JSON yn data.
Dadansoddwch y llwyth mewnol cyn ei ddosbarthu ar category neu event:
const parseEnvelope = (payload) => {
const raw = payload?.data;
if (!raw) return null;
return typeof raw === 'string' ? JSON.parse(raw) : raw;
};
Enghraifft:
next: ({ data }) => {
const wrapper = data?.subscribeGroupFeed;
const event = parseEnvelope(wrapper);
console.log(event.category, event.event, event.data);
}
Ailgysylltu a Diweddaru Tocynnau
Dylai cleientiaid cynhyrchiad:
- ailgysylltu’n awtomatig os bydd y websocket yn disgyn
- diweddaru tocynnau mynediad sy wedi dod i ben
- ailagor tanysgrifiadau ar ôl ailgysylltu
Os yw tanysgrifiadau’n stopio derbyn diweddariadau ar ôl sesiwn hir, yr hyn cyntaf i’w wirio yw dod i ben y tocyn.