-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
85 lines (76 loc) · 2.02 KB
/
index.js
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
require("dotenv").config();
const { ApolloServer, gql } = require("apollo-server");
const { makeExecutableSchema } = require("graphql-tools");
const { applyMiddleware } = require("graphql-middleware");
const merge = require("lodash/merge");
const { decode } = require("./Authentification/jwtUserToken");
const Permissions = require("./Permissions");
const Annuaire = require("./Annuaire");
const Authentification = require("./Authentification");
const Drive = require("./Drive");
const Planning = require("./Planning");
const App = {
typeDefs: gql`
type Query {
"Version actuelle du code de l'API"
version: String
}
type Mutation {
"Permet de s'assurer du bon fonctionnement du serveur"
ping: String
}
"Réponse recommandée pour une mutation"
interface MutationResponse {
success: Boolean!
code: String
message: String
}
`,
resolvers: {
Query: {
version: () => process.env.npm_package_version
},
Mutation: {
ping: () => "pong"
},
MutationResponse: {
__resolveType: () => null // see https://github.com/apollographql/apollo-server/issues/1075#issuecomment-427476421
}
}
};
const schema = makeExecutableSchema({
typeDefs: [
App.typeDefs,
Annuaire.typeDefs,
Authentification.typeDefs,
Drive.typeDefs,
Planning.typeDefs
],
resolvers: merge(
App.resolvers,
Annuaire.resolvers,
Authentification.resolvers,
Drive.resolvers,
Planning.resolvers
)
});
const server = new ApolloServer({
schema: applyMiddleware(schema, Permissions.middleware),
context: ({ req }) => {
const token = req.headers.authorization || "";
const user = token ? decode(token).data : null;
return {
user
};
},
dataSources: () =>
merge(Annuaire.dataSources(), Drive.dataSources(), Planning.dataSources())
});
server
.listen({
port: process.env.API_PORT || 4000,
host: process.env.API_HOST || "0.0.0.0"
})
.then(({ url }) => {
console.log(`-> Serveur démarré : ${url}`);
});