-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
111 lines (89 loc) · 2.8 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { config } from 'dotenv';
if (process.env.NODE_ENV !== "production") {
config();
}
import { ApolloServer, AuthenticationError} from 'apollo-server-express';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { execute, subscribe } from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';
import express from 'express';
import typeDefs from './src/typedefs';
import resolvers from './src/resolvers';
import decodeUser from './@utils/decodeUser';
(async function() {
const app = express();
const httpServer = createServer(app);
const schema = makeExecutableSchema({ typeDefs, resolvers });
const server = new ApolloServer({
schema,
plugins: [{
async serverWillStart() {
return {
async drainServer() {
subscriptionServer.close();
}
};
}
}],
context: async ({ req }) => {
let user;
if( req && req.headers.x_user_token !== undefined ) {
let token = req.headers.authorization || req.headers.x_user_token;
user = await decodeUser(token);
return user;
}
}
});
// subscription server
const subscriptionServer = SubscriptionServer.create({
schema,
execute,
subscribe,
}, {
server: httpServer,
path: server.graphqlPath
});
await server.start();
server.applyMiddleware({ app });
const PORT = process.env.PORT || 4000;
httpServer.listen(PORT, () =>
console.log(`Server is now running on http://localhost:${PORT}/graphql`)
);
})();
// const mocks = {
// Query: () => ({
// getUser: () => [...new Array(1)],
// }),
// User: () => ({
// id: () => "a7f15e8d-9334-464a-9527-7a46044aacbf",
// fullname: () => "John Doe",
// email: () => "johndoe@mail.com",
// username: () => "Johndoe",
// country: () => "Usa",
// password: () => "hshhdhuu83366",
// isActivated: () => true,
// avatar: () =>
// "https://res.cloudinary.com/dety84pbu/image/upload/v1606816219/kitty-veyron-sm_mctf3c.jpg",
// accountBalance: () => 100000,
// createdAt: () => "2021-07-23 14:59:31.064562+01",
// updateAt: () => "2021-07-24 14:59:31.064562+01",
// transactions: () => {
// return [{
// id: "a7f15e8d-9334-464a-9527-7a46044aacbf",
// amount: 20000,
// transactionStatus: "success",
// senderId: "a7f15e8d-9334-464a-9527-7a46044aacbf",
// receiverId: "a7f15e8d-9334-464a-9527-7a46044aacbf",
// transactedAt: "2021-07-23 14:59:31.064562+01",
// }
// }];
// }),
// };
// server.listen().then(() => {
// console.log(`
// 🚀 Server is running!
// 🔉 Listening on port 4000
// 📭 Query at https://studio.apollographql.com/dev
// `);
// });