const { ApolloServer } = require('apollo-server-express')
const { ApolloServerPluginDrainHttpServer } = require('apollo-server-core')
const express = require('express')
const http = require('http')
const { makeExecutableSchema } = require('@graphql-tools/schema')
const { upperDirectiveTransformer } = require('./directives/uppercase')
const { WebSocketServer } = require ('ws');
const { useServer } = require('graphql-ws/lib/use/ws');
const { typeDefs } = require('./typeDefs')
const { resolvers } = require('./resolvers')
const jwt = require('jsonwebtoken');
const JWT_SECRET = 'f1BtnWgD3VKY';
const users = [
{
"id":1,
"name":"Magdalena",
"email":"[email protected] /cdn-cgi/l/email-protection",
"gender":"male",
"mobile":"734-324-1043",
"cumulativeGPA":92.1,
"isGraduated":true,
"friends":[
{
"name":"Magdalena",
"email":"[email protected] /cdn-cgi/l/email-protection",
"gender":"male",
"mobile":"734-324-1043",
"cumulativeGPA":92.1
},
{
"name":"Harman",
"email":"[email protected] /cdn-cgi/l/email-protection",
"gender":"male",
"mobile":"158-265-8979",
"cumulativeGPA":87.9
},
{
"name":"Oliver",
"email":"[email protected] /cdn-cgi/l/email-protection",
"gender":"female",
"mobile":"500-958-5193",
"cumulativeGPA":67.9
}
],
"age":28,
"image" : {"name":"ghklk.png", "height": 50 , "width":30},
"idea": "auction",
"grantedAmount": 12000
},
{
"id":2,
"name":"Lyndell",
"email":"[email protected] /cdn-cgi/l/email-protection",
"gender":"male",
"mobile":"165-705-3521",
"cumulativeGPA":90.6,
"isGraduated":false,
"friends":[
{
"name":"Magdalena",
"email":"[email protected] /cdn-cgi/l/email-protection",
"gender":"male",
"mobile":"734-324-1043",
"cumulativeGPA":92.1
},
{
"name":"Harman",
"email":"[email protected] /cdn-cgi/l/email-protection",
"gender":"male",
"mobile":"158-265-8979",
"cumulativeGPA":87.9
},
{
"name":"Oliver",
"email":"[email protected] /cdn-cgi/l/email-protection",
"gender":"female",
"mobile":"500-958-5193",
"cumulativeGPA":67.9
}
],
"age":23,
"image" : {"name":"ghklk.png", "height": 50, "width":30},
"idea": "e-collage",
"grantedAmount": 0
},
]
async function startApolloServer(typeDefs, resolvers) {
const app = express()
const httpServer = http.createServer(app)
let schema = makeExecutableSchema({
typeDefs,
resolvers
})
schema = upperDirectiveTransformer(schema, 'upper')
const wsServer = new WebSocketServer({
server: httpServer,
path: '/graphql'
})
const serverCleanup = useServer({ schema }, wsServer);
const server = new ApolloServer({
schema,
plugins: [
ApolloServerPluginDrainHttpServer({ httpServer }),
{
async serverWillStart() {
return {
async drainServer() {
serverCleanup.dispose();
}
}
}
}
],
context: ({ req }) => {
const auth = req ? req.headers.authorization : null
try{
const decodedToken = jwt.verify(auth.slice(4), JWT_SECRET)
const user = users.find(user => user.id == decodedToken.id)
return { user }
}catch(err){
return null
}
}
})
await server.start()
server.applyMiddleware({ app })
await new Promise(resolve => httpServer.listen({ port: 4000 }, resolve))
console.log(`???? Server ready at http://localhost:4000${server.graphqlPath}`);
return { server, app }
}
startApolloServer(typeDefs, resolvers)