-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (109 loc) · 2.66 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import express from "express";
import { graphqlExpress, graphiqlExpress } from "apollo-server-express";
import bodyParser from "body-parser";
import { ApolloEngine } from "apollo-engine";
import { makeExecutableSchema } from "graphql-tools";
import cors from "cors";
import "./src/config/db";
import constants from "./src/config/constants";
import { typeDefs } from "./src/graphql/schema";
import { resolvers } from "./src/graphql/resolvers";
import middlewares from "./src/config/middlewares";
const app = express();
app.use(cors());
middlewares(app);
require("dotenv").config();
if (!process.env.TM_API_KEY) {
throw new Error(
"Please provide an API key for Ticketmaster in the environment variable TM_API_KEY."
);
}
if (!process.env.ENGINE_API_KEY) {
throw new Error(
"Please provide an API key for Apollo Engine in the environment variable ENGINE_API_KEY."
);
}
// Required: Export the GraphQL.js schema object as "schema"
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
app.post(
constants.GRAPHQL_PATH,
bodyParser.json(),
graphqlExpress(req => ({
schema,
tracing: true,
cacheControl: true,
context: {
user: req.user,
secrets: {
TM_API_KEY: process.env.TM_API_KEY
}
}
}))
);
const gql = String.raw;
// Using Route Handlers
app.get("/thanks", (req, res) => {
res.send({ greeting: "Hey friend!" });
});
app.get(
constants.GRAPHIQL_PATH,
graphiqlExpress({
endpointURL: constants.GRAPHQL_PATH
// query: gql`
// query UpcomingEvents {
// myFavoriteArtists {
// name
// twitterUrl
// events {
// name
// startDateTime
// }
// }
// }
// `
})
);
if (process.env.NODE_ENV === "production") {
// Express will serve up production assets
// like our main.js file, or main.css file!
app.use(express.static("client/build"));
// Express will serve up the index.html file
// if it doesn't recognize the route
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
// app.use(express.static("public"));
const engine = new ApolloEngine({
apiKey: process.env.ENGINE_API_KEY,
// logging: {
// level: "DEBUG"
// },
stores: [
{
name: "publicResponseCache",
inMemory: {
cacheSize: 10485760
}
}
],
queryCache: {
publicFullQueryStore: "publicResponseCache"
}
});
// // Start the app
engine.listen(
{
port: constants.PORT,
expressApp: app
},
() => {
console.log(
`Go to http://localhost:${constants.PORT}/graphiql to run queries!`
);
}
);