Skip to content

Instantly share code, notes, and snippets.

@sebastianrothbucher
Last active July 21, 2024 10:18
Show Gist options
  • Save sebastianrothbucher/54a820e758aead838aab4836d88e60e0 to your computer and use it in GitHub Desktop.
Save sebastianrothbucher/54a820e758aead838aab4836d88e60e0 to your computer and use it in GitHub Desktop.
Base Apollo server (including subscriptions - can leave out ws part) - round trip
import {ApolloServer} from '@apollo/server';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import {expressMiddleware} from '@apollo/server/express4';
import {makeExecutableSchema} from '@graphql-tools/schema';
import express from 'express';
import cors from 'cors';
import morgan from 'morgan';
import fs from 'fs';
import {createServer} from 'http';
import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import { PubSub } from 'graphql-subscriptions';
const fakeDb = {};
const pubsub = new PubSub();
(async () => {
const app = express();
app.use(cors());
app.use(morgan("common"));
const httpServer = createServer(app);
const schema = makeExecutableSchema({
typeDefs: fs.readFileSync('test.graphqls', 'utf-8'),
resolvers: {
Query: {
next: async () => {
const value = 10 + Math.round(2 * Math.random());
pubsub.publish('NEW_VALUE', {
next: {value}, // full message
});
return {
value,
};
},
},
Mutation: {
next: async (_, { val }) => {
pubsub.publish('NEW_VALUE', {
next: val, // full message
});
return val;
},
},
Subscription: {
next: {
subscribe: () => pubsub.asyncIterator(['NEW_VALUE']),
},
},
},
});
const server = new ApolloServer({
schema,
plugins: [
// Proper shutdown for the HTTP server.
ApolloServerPluginDrainHttpServer({ httpServer }),
// Proper shutdown for the WebSocket server.
{
async serverWillStart() {
return {
async drainServer() {
await serverCleanup.dispose();
},
};
},
},
],
});
const wsServer = new WebSocketServer({
server: httpServer,
path: '/',
});
const serverCleanup = useServer({ schema }, wsServer);
await server.start();
app.use("/", express.json(), expressMiddleware(server));
httpServer.listen({
port: 4444,
host: "127.0.0.1",
ipv6Only: false,
});
console.log(`🚀 Server ready at: http://localhost:4444`);
})();
{
"name": "tests",
"scripts": {},
"dependencies": {
"@apollo/server": "^4.10.4",
"@graphql-tools/schema": "^10.0.4",
"cors": "^2.8.5",
"express": "^4.19.2",
"graphql": "^16.9.0",
"graphql-subscriptions": "^2.0.0",
"graphql-ws": "^5.16.0",
"morgan": "^1.10.0",
"ws": "^8.18.0"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/morgan": "^1.9.9",
"@types/ws": "^8.5.10",
"ts-node": "^10.9.2"
}
}
type NextValue {
value: Int
}
type Query {
next: NextValue
}
input NextValueInput {
value: Int
}
type Mutation {
next(val: NextValueInput): NextValue
}
type Subscription {
next: NextValue
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment