-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (42 loc) · 1.1 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
const { ApolloServer, gql } = require("apollo-server-express");
const express = require("express");
const bodyParser = require("body-parser");
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
}
type Mutation {
changeName(name: String!): String!
}
`;
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: (root, args, context) => "Hello world!"
},
Mutation: {
changeName: (root, args) => args.name
}
};
async function startApolloServer() {
const app = express();
app.use(bodyParser.json());
app.use((req, res, next) => {
console.log("Incoming Req. Body:", req.body);
return next();
});
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true
});
await server.start();
server.applyMiddleware({ app });
await new Promise((resolve) => app.listen({ port: 4000 }, resolve));
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
return { server, app };
}
startApolloServer().then((x) => {
console.log("Complete");
});