dynamically pulling and passing subscriptions from directus to mesh

This commit is contained in:
Samuel Andert 2023-08-23 12:44:05 +02:00
parent b9ee596d56
commit 35956ce32c
2 changed files with 16 additions and 9 deletions

View File

@ -10,7 +10,7 @@ sources:
operationHeaders: operationHeaders:
Authorization: Bearer {env.DIRECTUS_API} Authorization: Bearer {env.DIRECTUS_API}
additionalResolvers: additionalResolvers:
- ./resolvers/projects.js - ./resolvers/directus_subscriptions.js
# - name: Cloudron # - name: Cloudron
# handler: # handler:

View File

@ -5,13 +5,13 @@ const { parse } = require('graphql');
const fs = require('fs'); const fs = require('fs');
const pubsub = new PubSub(); const pubsub = new PubSub();
const PROJECTS_MUTATED = 'PROJECTS_MUTATED';
// Read the schema file // Read the schema file
const schema = fs.readFileSync('./schemas/directus.graphql', 'utf8'); const schema = fs.readFileSync('./schemas/directus.graphql', 'utf8');
// Parse the schema // Parse the schema
const document = parse(schema); const document = parse(schema);
// Function to find a type definition by name // Function to find a type definition by name
const findTypeDefinition = (typeName) => { const findTypeDefinition = (typeName) => {
return document.definitions.find( return document.definitions.find(
@ -36,9 +36,6 @@ const extractFields = (type, depth = 0) => {
.join('\n'); .join('\n');
}; };
const projectsType = findTypeDefinition('projects');
const projectsFields = extractFields(projectsType);
const client = createClient({ const client = createClient({
url: 'wss://directus.andert.me/graphql', url: 'wss://directus.andert.me/graphql',
keepAlive: 30000, keepAlive: 30000,
@ -95,9 +92,19 @@ const handleSubscription = (typeName) => {
}; };
}; };
// Find the Subscription type definition
const subscriptionType = findTypeDefinition('Subscription');
// Extract the subscription type names, excluding the ones starting with 'directus_'
const subscriptionTypeNames = subscriptionType.fields
.map(field => field.name.value.replace('_mutated', ''))
.filter(name => !name.startsWith('directus_'));
// Generate subscriptions for each type
const subscriptions = subscriptionTypeNames.reduce((acc, typeName) => {
return { ...acc, ...handleSubscription(typeName) };
}, {});
module.exports = { module.exports = {
Subscription: { Subscription: subscriptions,
...handleSubscription('projects'),
...handleSubscription('todos'),
},
}; };