Skip to content

Instantly share code, notes, and snippets.

@daniele-zurico
Created June 8, 2018 15:45
Show Gist options
  • Select an option

  • Save daniele-zurico/5965a7effdf056436199e078f5ee7894 to your computer and use it in GitHub Desktop.

Select an option

Save daniele-zurico/5965a7effdf056436199e078f5ee7894 to your computer and use it in GitHub Desktop.
import { ApolloServer, AuthenticationError } from "apollo-server";
import express from "express";
import mongoose from "mongoose";
import { GraphQLSchema } from "graphql";
import { mergeSchemas } from "graphql-tools";
const { OAuth2Client } = require("google-auth-library");
import schemas from "./schemas/schema";
import resolvers from "./resolvers/resolvers";
import { userController } from "./controllers/controllers";
const MONGO_PORT = 27017;
const MONGO_URL = "localhost";
const dbName = "graphExample";
export const CLIENT_ID ="YOUR_CLIENT_ID";
export const client = new OAuth2Client(CLIENT_ID);
// help to debug mongoose
mongoose.set("debug", true);
mongoose.connect(`mongodb://${MONGO_URL}:${MONGO_PORT}/${dbName}`);
const app = express();
const schema: GraphQLSchema = mergeSchemas({
schemas,
resolvers
});
// GraphQL
const server = new ApolloServer({
schema,
context: async ({ req }: any) => {
const token = req.headers.authorization || "";
const checkToken = await userController.findOrCreateUser(token);
if (!checkToken.hasOwnProperty('authorized')) {
return {user: checkToken, authorized: true};
}
return checkToken;
},
tracing: true
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment