This repository has been archived on 2023-09-04. You can view files and clone it, but cannot push or open issues or pull requests.
ARCHIVED-graphql/resolvers/projects.js

93 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-08-23 05:25:32 +00:00
const { createClient } = require('graphql-ws');
const WebSocket = require('ws');
2023-08-23 06:53:31 +00:00
const { PubSub } = require('graphql-subscriptions');
const { parse } = require('graphql');
const fs = require('fs');
2023-08-23 06:53:31 +00:00
const pubsub = new PubSub();
const PROJECTS_MUTATED = 'PROJECTS_MUTATED';
2023-08-23 05:25:32 +00:00
// Read the schema file
const schema = fs.readFileSync('./schemas/directus.graphql', 'utf8');
// Parse the schema
const document = parse(schema);
// Function to find a type definition by name
const findTypeDefinition = (typeName) => {
return document.definitions.find(
def => def.kind === 'ObjectTypeDefinition' && def.name.value === typeName
);
};
// Extract the fields excluding the ones ending with '_func', of object type, and specific fields
const extractFields = (type, depth = 0) => {
return type.fields
.filter(field => !field.name.value.endsWith('_func') && !['avatar', 'role'].includes(field.name.value))
.map(field => {
if (field.type.kind === 'NamedType' && depth < 1) {
// If the field is of object type and we are not too deep, recursively extract its fields
const nestedType = findTypeDefinition(field.type.name.value);
if (nestedType) {
return `${field.name.value} {\n${extractFields(nestedType, depth + 1)}\n}`;
}
}
return field.name.value;
})
.join('\n');
};
const projectsType = findTypeDefinition('projects');
const projectsFields = extractFields(projectsType);
2023-08-23 05:25:32 +00:00
const client = createClient({
url: 'wss://directus.andert.me/graphql',
keepAlive: 30000,
webSocketImpl: WebSocket,
connectionParams: async () => {
2023-08-23 07:30:08 +00:00
return { access_token: process.env.DIRECTUS_API };
2023-08-23 05:25:32 +00:00
},
});
2023-08-23 10:23:50 +00:00
const generateSubscriptionQuery = (typeName) => {
const typeDefinition = findTypeDefinition(typeName);
const fields = extractFields(typeDefinition);
return `
subscription {
${typeName}_mutated {
key
event
data {
${fields}
}
}
}
2023-08-23 10:23:50 +00:00
`;
};
2023-08-23 10:23:50 +00:00
// Generate subscription query for 'projects'
const projectsQuery = generateSubscriptionQuery('projects');
// const todosQuery = generateSubscriptionQuery('todos');
2023-08-23 05:25:32 +00:00
client.subscribe(
{
2023-08-23 10:23:50 +00:00
query: projectsQuery,
2023-08-23 05:25:32 +00:00
},
{
2023-08-23 06:53:31 +00:00
next: data => {
if (data && data.data && data.data.projects_mutated) {
pubsub.publish(PROJECTS_MUTATED, { projects_mutated: data.data.projects_mutated });
}
},
2023-08-23 05:25:32 +00:00
error: error => console.error('error:', error),
}
2023-08-23 06:53:31 +00:00
);
module.exports = {
Subscription: {
projects_mutated: {
subscribe: () => pubsub.asyncIterator([PROJECTS_MUTATED]),
resolve: payload => payload.projects_mutated,
},
},
};