Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

graphql-subscriptions #22

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
"@fastify/cors": "^8.4.1",
"@fastify/websocket": "^8.3.0",
"@graphql-tools/merge": "^9.0.1",
"@graphql-tools/schema": "^10.0.2",
"@hocuspocus/extension-database": "^2.8.1",
"@hocuspocus/server": "^2.8.1",
"axios": "^1.6.0",
"fastify": "^4.24.3",
"fastify-graceful-shutdown": "^3.5.1",
"fastify-socket.io": "^5.0.0",
"graphql": "^16.8.1",
"graphql-postgres-subscriptions": "^1.0.5",
"graphql-tag": "^2.12.6",
"graphql-upload": "^16.0.2",
"graphql-ws": "^5.14.3",
"lodash-es": "^4.17.21",
"minio": "^7.1.3",
"pg": "^8.11.3",
Expand All @@ -46,6 +49,7 @@
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/ws": "^8.5.10",
"chai": "^4.3.10",
"mocha": "^10.2.0",
"prettier": "^3.0.3",
Expand Down
107 changes: 105 additions & 2 deletions backend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions backend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { ApolloServer } from '@apollo/server';
import { fastifyApolloDrainPlugin, fastifyApolloHandler } from '@as-integrations/fastify';
import * as cors from '@fastify/cors';
import { fastifyWebsocket } from '@fastify/websocket';
import { makeExecutableSchema } from '@graphql-tools/schema';
import axios from 'axios';
import Fastify from 'fastify';
import fastifyIO from 'fastify-socket.io';
import processRequest from 'graphql-upload/processRequest.mjs';
import { makeHandler } from 'graphql-ws/lib/use/@fastify/websocket';
import { GraphQLError } from 'graphql/error/index.js';

import { db } from './db/index.js';
Expand All @@ -25,6 +27,8 @@ import { minioClient } from './services/minio-client.js';
import { socketInit } from './socket/index.js';
import typeDefs from './type-defs.js';

const schema = makeExecutableSchema({ typeDefs, resolvers });

const fastify = Fastify({
logger: ENABLE_FASTIFY_LOGGING,
keepAliveTimeout: 61 * 1000,
Expand Down Expand Up @@ -114,8 +118,13 @@ const apollo = new ApolloServer({
await apollo.start();

const myContextFunction = async (request) => {
let token = request?.headers?.authorization;

if (request?.connectionParams?.headers?.Authorization) {
token = request?.connectionParams?.headers?.Authorization;
}

// get the user token from the headers
const token = request.headers.authorization;
let user = null;

// Allow if introspection query only
Expand All @@ -131,7 +140,7 @@ const myContextFunction = async (request) => {
try {
// TODO: maybe we just call the url of the caller origin
const { data } = await axios.get(`${FRONTEND_HOSTNAME}/api/verify-jwt`, {
headers: { Authorization: request.headers.authorization },
headers: { Authorization: token },
});

// TODO: We can inject from DB here the whitelist domains and emails in addition to ENV vars
Expand Down Expand Up @@ -208,6 +217,10 @@ const myContextFunction = async (request) => {
};
};

fastify.register(async (fastify) => {
fastify.get('/graphql', { websocket: true }, makeHandler({ schema, context: myContextFunction }));
});

fastify.post(
'/graphql',
fastifyApolloHandler(apollo, {
Expand Down
11 changes: 11 additions & 0 deletions backend/src/resolvers/Board/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import pubsub from '../../services/apollo-pg-pubsub.js';

const resolvers = {
Subscription: {
boardUpdated: {
subscribe: () => pubsub.asyncIterator(['BOARD_UPDATED']),
},
},
Query: {
boards: (parent, args, { db }) => {
// TODO: should we require a project id to show boards?
Expand Down Expand Up @@ -27,6 +34,10 @@ const resolvers = {

await board.save();

pubsub.publish('BOARD_UPDATED', {
boardUpdated: board,
});

// TODO: fix
// emitBoardUpdatedEvent(io, board.toJSON());

Expand Down
15 changes: 14 additions & 1 deletion backend/src/resolvers/Issue/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Op } from 'sequelize';

import pubsub from '../../services/apollo-pg-pubsub.js';

const resolvers = {
Subscription: {
issueCreated: {
subscribe: () => pubsub.asyncIterator(['ISSUE_CREATED']),
},
},
Query: {
issues: (parent, { input: { projectId, id, search, searchOperator } }, { db }) => {
let whereOr = [];
Expand Down Expand Up @@ -175,7 +182,13 @@ const resolvers = {

const issueStatus = await db.sequelize.models.IssueStatuses.findByPk(issueStatusId);

return { ...issue.toJSON(), status: issueStatus.toJSON() };
const returnData = { ...issue.toJSON(), status: issueStatus.toJSON() };

pubsub.publish('ISSUE_CREATED', {
issueCreated: returnData,
});

return returnData;
},
deleteIssue: async (parent, { input }, { db }) => {
const { id } = input;
Expand Down
14 changes: 14 additions & 0 deletions backend/src/services/apollo-pg-pubsub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PostgresPubSub } from 'graphql-postgres-subscriptions';
import pkg from 'pg';

import { SQL_URI } from './config.js';

const { Client } = pkg;

const client = new Client({
connectionString: SQL_URI,
});
await client.connect();
const pubsub = new PostgresPubSub({ client });

export default pubsub;
6 changes: 6 additions & 0 deletions backend/src/type-defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ const typeDefs = gql`
issues(input: QueryIssueInput): [Issue]
issue(input: QueryIssueInput): Issue
}

# SUBSCRIPTIONS
type Subscription {
issueCreated: Issue
boardUpdated: Board
}
`;

export default typeDefs;
Loading