Leum gu prìomh susbaint

Ceangail ri AppSync Subscriptions

Tha an leabhar-làimhe seo a’ sealltainn mar a cheanglas tu ris an dàrna ceann websocket Legalesign AppSync agus tòisich a’ cleachdadh GraphQL subscriptions.

Cleachd an duilleag seo airson an rèiteachadh ceangail.

Cleachd na duilleagan seo airson làimhseachadh payload:

Mus Tòisich Thu

Feumaidh tu:

  • token faighinn ruigsinneachd dligheach
  • ceann GraphQL Legalesign
  • neach-dèiligidh GraphQL comasach air websocket

Faigh token faighinn ruigsinneachd an toiseach:

Ceannan

Ceann HTTP GraphQL:

https://graphql.uk.legalesign.com/graphql

Ceann websocket:

wss://graphql.uk.legalesign.com/graphql

Dearbhadh

Cuir seachad do token faighinn nuair a bhios tu a’ fosgladh ceangal websocket.

Paramadairean ceangail àbhaisteach:

{
"Authorization": "Bearer <access-token>"
}

Gnìomhan Subscription

Fiadh Cleachdaiche

subscription SubscribeUserFeed {
subscribeUserFeed {
userId
data
}
}

Fiadh Buidheann

subscription SubscribeGroupFeed($groupId: String!) {
subscribeGroupFeed(groupId: $groupId) {
groupId
data
}
}

Eisimpleir JavaScript

Tha an eisimpleir seo a’ cleachdadh 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();

Mìnich a’ Phayload a-staigh

Tha an raon subscription a’ tilgeil freagairt a tha na chòmhdach a bharrachd air sreang JSON ann an data.

Mìnich a’ phayload a-staigh mus dèan thu rathad air category no event:

const parseEnvelope = (payload) => {
const raw = payload?.data;
if (!raw) return null;
return typeof raw === 'string' ? JSON.parse(raw) : raw;
};

Eisimpleir:

next: ({ data }) => {
const wrapper = data?.subscribeGroupFeed;
const event = parseEnvelope(wrapper);

console.log(event.category, event.event, event.data);
}

Ath-cheangal agus Ath-nuadhachadh Token

Bu chòir do luchd-dèiligidh riochdachaidh:

  • ath-cheangal gu fèin-obrachail ma thèid an websocket a chall
  • ath-nuadhachadh tokens fàillte
  • fosgladh suas subscriptions a-rithist às dèidh ath-cheangal

Ma thòisicheas subscriptions a’ stad ag fhaighinn ùrachaidhean às dèidh seisean fad-ùine, ’s e a’ chiad rud ri sgrùdadh cò às a thàinig fàilligeadh an token.