Skip to content

Instantly share code, notes, and snippets.

@hyochan
Last active February 1, 2020 15:10
Show Gist options
  • Save hyochan/8044fecdea64c0f8f46909f5d422b4e7 to your computer and use it in GitHub Desktop.
Save hyochan/8044fecdea64c0f8f46909f5d422b4e7 to your computer and use it in GitHub Desktop.
Example for using apollo server
import models, { ModelType } from './models';
import { ApolloServer } from 'apollo-server-express';
import { Http2Server } from 'http2';
import { JwtUser } from './utils/auth';
import { PubSub } from 'graphql-subscriptions';
import { User } from './models/User';
import { allResolvers } from './resolvers';
import { createApp } from './app';
import { createServer as createHttpServer } from 'http';
import express from 'express';
import { importSchema } from 'graphql-import';
import jwt from 'jsonwebtoken';
require('dotenv').config();
const { PORT = 4000, JWT_SECRET = 'undefined' } = process.env;
const pubsub = new PubSub();
export const verifyUser = (token: string): JwtUser => {
return jwt.verify(token, JWT_SECRET) as JwtUser;
};
// eslint-disable-next-line
const getToken = (req: Express.Request & any): string => {
const authHeader = req.get('Authorization');
if (!authHeader) {
return null;
}
return authHeader.replace('Bearer ', '');
};
const createApolloServer = (): ApolloServer => new ApolloServer({
typeDefs: importSchema('schemas/schema.graphql'),
context: ({ req }): {
getUser: () => Promise<User>;
models: ModelType;
pubsub: PubSub;
appSecret: string;
} => ({
getUser: (): Promise<User> => {
const { User: userModel } = models;
const token = getToken(req);
if (!token) {
return null;
}
const user = verifyUser(token);
const { userId } = user;
return userModel.findOne({
where: {
id: userId,
},
raw: true,
});
},
models,
pubsub,
appSecret: JWT_SECRET,
}),
introspection: process.env.NODE_ENV !== 'production',
playground: process.env.NODE_ENV !== 'production',
resolvers: allResolvers,
subscriptions: {
onConnect: (): void => {
process.stdout.write('Connected to websocket\n');
},
},
});
const initializeApolloServer = (apollo: ApolloServer, app: express.Application): () => void => {
apollo.applyMiddleware({ app });
return (): void => {
process.stdout.write(
`🚀 Server ready at http://localhost:${PORT}${apollo.graphqlPath}\n`,
);
};
};
export const startServer = async (app: express.Application): Promise<Http2Server> => {
const httpServer = createHttpServer(app);
const apollo = createApolloServer();
apollo.installSubscriptionHandlers(httpServer);
const handleApolloServerInitilized = initializeApolloServer(apollo, app);
return httpServer.listen({ port: PORT }, () => {
handleApolloServerInitilized();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment