-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
94 lines (79 loc) · 2.13 KB
/
server.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
// Koa libraries
import Koa from "koa";
import KoaRouter from "koa-router";
import KoaBodyParser from "koa-bodyparser";
// Apollo Server for Koa
import { graphqlKoa, graphiqlKoa } from "apollo-server-koa";
// Prisma
import { Prisma } from "prisma-binding";
// GraphQL Playground Middleware for Koa
const koaPlayground = require("graphql-playground-middleware-koa").default;
// Schema and Resolvers
import { makeExecutableSchema } from "graphql-tools";
import Resolvers from "./graphql/resolvers";
import { importSchema } from "graphql-import";
const typeDefs = importSchema("./graphql/schema.graphql");
// MongoDB
const mongoose = require("./mongodb/config/mongoose");
mongoose();
const PORT = process.env.PORT || 8080;
const NODE_ENV = process.env.NODE_ENV || "development";
const graphQLServer = new Koa();
const router = new KoaRouter();
const bodyParser = new KoaBodyParser();
// Validate GraphQL API Schema
const schema = makeExecutableSchema({
typeDefs,
resolvers: Resolvers,
resolverValidationOptions: {
requireResolversForResolveType: false
}
});
// Use bodyparser middleware
graphQLServer.use(bodyParser);
// Define GraphQL endpoints
router.post(
"/graphql",
graphqlKoa({
schema,
context: () => ({
prisma: new Prisma({
typeDefs: "graphql/generated/prisma.graphql",
endpoint: "http://prisma:4466"
})
})
})
);
router.get(
"/graphql",
graphqlKoa({
schema,
context: () => ({
prisma: new Prisma({
typeDefs: "graphql/generated/prisma.graphql",
endpoint: "http://prisma:4466"
})
})
})
);
// Define endpoint for GraphQL Playground
router.all(
"/playground",
koaPlayground({
endpoint: "/graphql"
})
);
// Define GraphiQL endpoints
if (NODE_ENV !== "production") {
router.get("/graphiql", graphiqlKoa({ endpointURL: "/graphql" }));
}
// Use router middleware
graphQLServer.use(router.routes());
graphQLServer.use(router.allowedMethods());
graphQLServer.listen(PORT, () => {
console.log(
`GraphQL Playground is now running on http://localhost:${PORT}/playground`
);
});
// Add some seed data to MongoDB database
require("./mongodb/models/seeds");