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 projectsQuery = generateSubscriptionQuery('projects');
// const todosQuery = generateSubscriptionQuery('todos');
const handleSubscription = (typeName) => {
const MUTATED = `${typeName.toUpperCase()}_MUTATED`;
const type = findTypeDefinition(typeName);
const fields = extractFields(type);
const query = generateSubscriptionQuery(typeName);
client.subscribe(
{
query: projectsQuery,
query: query,
},
{
next: data => {
if (data && data.data && data.data.projects_mutated) {
pubsub.publish(PROJECTS_MUTATED, { projects_mutated: data.data.projects_mutated });
if (data && data.data && data.data[`${typeName}_mutated`]) {
pubsub.publish(MUTATED, { [`${typeName}_mutated`]: data.data[`${typeName}_mutated`] });
}
},
error: error => console.error('error:', error),
}
);
module.exports = {
Subscription: {
projects_mutated: {
subscribe: () => pubsub.asyncIterator([PROJECTS_MUTATED]),
resolve: payload => payload.projects_mutated,
},
return {
[`${typeName}_mutated`]: {
subscribe: () => pubsub.asyncIterator([MUTATED]),
resolve: payload => payload[`${typeName}_mutated`],
},
};
};
module.exports = {
Subscription: {
...handleSubscription('projects'),
...handleSubscription('todos'),
},
};