Skip to content
This repository has been archived by the owner on May 7, 2023. It is now read-only.

Commit

Permalink
Wrap use server
Browse files Browse the repository at this point in the history
  • Loading branch information
hyochan committed Jun 2, 2022
1 parent be0b59a commit 6b58552
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import {createContext, runSubscriptionServer} from './context';

import {ApolloServer} from 'apollo-server-express';
import {ApolloServerPluginDrainHttpServer} from 'apollo-server-core';
import {Disposable} from 'graphql-ws';
import SendGridMail from '@sendgrid/mail';
import WebSocket from 'ws';
import {applyMiddleware} from 'graphql-middleware';
import {assert} from './utils/assert';
import {createApp} from './app';
import express from 'express';
import {permissions} from './permissions';
import {schema} from './schema';
import {useServer} from 'graphql-ws/lib/use/ws';

const {PORT = 4000, NODE_ENV, SENDGRID_API_KEY} = process.env;

Expand All @@ -20,7 +21,7 @@ export const schemaWithMiddleware =
assert(SENDGRID_API_KEY, 'Missing SENDGRID_API_KEY environment variable.');
SendGridMail.setApiKey(SENDGRID_API_KEY);

let subscriptionServer: WebSocket.Server<WebSocket.WebSocket>;
let serverCleanup: Disposable;

const createApolloServer = (httpServer: Server): ApolloServer =>
new ApolloServer({
Expand All @@ -34,9 +35,7 @@ const createApolloServer = (httpServer: Server): ApolloServer =>
async serverWillStart() {
return {
async drainServer() {
if (subscriptionServer) {
subscriptionServer.close();
}
serverCleanup?.dispose();
},
};
},
Expand All @@ -51,7 +50,9 @@ const initializeApolloServer = (
): (() => void) => {
apollo.applyMiddleware({app});

subscriptionServer = runSubscriptionServer(httpServer);
const subscriptionServer = runSubscriptionServer(httpServer);
// eslint-disable-next-line react-hooks/rules-of-hooks
serverCleanup = useServer({schema: schemaWithMiddleware}, subscriptionServer);

return (): void => {
process.stdout.write(
Expand Down

0 comments on commit 6b58552

Please sign in to comment.