fixing subscription resolver

This commit is contained in:
Samuel Andert 2023-08-23 08:53:31 +02:00
parent ef66f2790d
commit 3e2ee1ee0f

View File

@ -1,5 +1,9 @@
const { createClient } = require('graphql-ws');
const WebSocket = require('ws');
const { PubSub } = require('graphql-subscriptions');
const pubsub = new PubSub();
const PROJECTS_MUTATED = 'PROJECTS_MUTATED';
const client = createClient({
url: 'wss://directus.andert.me/graphql',
@ -8,11 +12,6 @@ const client = createClient({
connectionParams: async () => {
return { access_token: 'Mb3NMqKwiyesxDeJRTD72VZZ6f4t3mBr' };
},
on: {
opened: () => console.log('Connection opened'),
closed: () => console.log('Connection closed'),
error: (error) => console.log('Connection error:', error),
},
});
client.subscribe(
@ -20,22 +19,35 @@ client.subscribe(
query: `
subscription {
projects_mutated {
key
event
data {
id
text
date_updated
user_updated {
first_name
}
}
event
key
}
}
`,
},
{
next: data => console.log('data:', data),
next: data => {
if (data && data.data && data.data.projects_mutated) {
pubsub.publish(PROJECTS_MUTATED, { projects_mutated: data.data.projects_mutated });
}
},
error: error => console.error('error:', error),
complete: () => console.log('complete'),
}
);
module.exports = {
Subscription: {
projects_mutated: {
subscribe: () => pubsub.asyncIterator([PROJECTS_MUTATED]),
resolve: payload => payload.projects_mutated,
},
},
};