-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
88 lines (73 loc) · 2.24 KB
/
app.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
86
87
if (typeof(PhusionPassenger) !== 'undefined') {
PhusionPassenger.configure({ autoInstall: false });
}
const express = require('express');
const Sentry = require('@sentry/node');
const compression = require('compression');
const morgan = require('morgan');
const { ApolloServer } = require('apollo-server-express');
const { ApolloGateway, IntrospectAndCompose, RemoteGraphQLDataSource } = require("@apollo/gateway");
const { ApolloServerPluginLandingPageGraphQLPlayground } = require('apollo-server-core');
const CLIENT_API_URL = process.env.CLIENT_API_URL || 'https://api.stage.datacite.org/client-api/graphql';
class AuthenticationHeader extends RemoteGraphQLDataSource {
willSendRequest({ request, context }) {
// Pass the token from the context to underlying services
// via the authorization header
request.http.headers.set('authorization', context.token);
}
}
const gateway = new ApolloGateway({
supergraphSdl: new IntrospectAndCompose({
subgraphs: [
{ name: 'client-api', url: CLIENT_API_URL },
],
}),
buildService({ name, url }) {
return new AuthenticationHeader({ url });
},
});
(async () => {
const server = new ApolloServer({
gateway,
cors: false,
subscriptions: false,
introspection: true,
tracing: true,
engine: {
apiKey: process.env.APOLLO_API_KEY,
graphVariant: process.env.NODE_ENV
},
plugins: [
ApolloServerPluginLandingPageGraphQLPlayground({
settings: {
'editor.theme': 'light',
}
}),
],
context: ({ req }) => {
// Get the user token from the headers
const token = req.headers.authorization || '';
// Add the token to the context
return { token };
},
});
let app = express();
Sentry.init({ dsn: process.env.SENTRY_DSN });
// The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());
// The error handler must be before any other error middleware
app.use(Sentry.Handlers.errorHandler());
// compress responses
app.use(compression());
// logging
app.use(morgan('combined'));
await server.start();
server.applyMiddleware({ app, cors: false });
// disable headers
app.disable('x-powered-by');
if (typeof(PhusionPassenger) !== 'undefined') {
app.listen('passenger');
} else {
app.listen(4000);
}
})();