added generic subscribing to directus wss by name

This commit is contained in:
Samuel Andert 2023-08-23 12:37:30 +02:00
parent 39e1f2afbc
commit b9ee596d56

View File

@ -65,29 +65,39 @@ const generateSubscriptionQuery = (typeName) => {
`; `;
}; };
// Generate subscription query for 'projects' const handleSubscription = (typeName) => {
const projectsQuery = generateSubscriptionQuery('projects'); const MUTATED = `${typeName.toUpperCase()}_MUTATED`;
// const todosQuery = generateSubscriptionQuery('todos');
client.subscribe( const type = findTypeDefinition(typeName);
const fields = extractFields(type);
const query = generateSubscriptionQuery(typeName);
client.subscribe(
{ {
query: projectsQuery, query: query,
}, },
{ {
next: data => { next: data => {
if (data && data.data && data.data.projects_mutated) { if (data && data.data && data.data[`${typeName}_mutated`]) {
pubsub.publish(PROJECTS_MUTATED, { projects_mutated: data.data.projects_mutated }); pubsub.publish(MUTATED, { [`${typeName}_mutated`]: data.data[`${typeName}_mutated`] });
} }
}, },
error: error => console.error('error:', error), error: error => console.error('error:', error),
} }
); );
return {
[`${typeName}_mutated`]: {
subscribe: () => pubsub.asyncIterator([MUTATED]),
resolve: payload => payload[`${typeName}_mutated`],
},
};
};
module.exports = { module.exports = {
Subscription: { Subscription: {
projects_mutated: { ...handleSubscription('projects'),
subscribe: () => pubsub.asyncIterator([PROJECTS_MUTATED]), ...handleSubscription('todos'),
resolve: payload => payload.projects_mutated,
},
}, },
}; };