-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (45 loc) · 1.39 KB
/
index.js
File metadata and controls
54 lines (45 loc) · 1.39 KB
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
import { ApolloServer } from '@apollo/server';
import { createServer } from 'http';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import { makeExecutableSchema } from '@graphql-tools/schema';
import bodyParser from 'body-parser';
import express from 'express';
const port = 3000;
const typeDefs = `
type Query {
foo: String!
}
type Mutation {
scheduleOperation(name: String!): String!
}
`;
const resolvers = {
Mutation: {
scheduleOperation(_, { name }) {
console.log(`Mocking operation: ${name}`);
return `Operation: ${name} scheduled!`;
}
},
Query: {
foo() {
return 'foo';
}
}
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
const app = express();
const httpServer = createServer(app);
const apolloServer = new ApolloServer({
schema,
plugins: [
// Proper shutdown for the HTTP server.
ApolloServerPluginDrainHttpServer({ httpServer }),
]
});
await apolloServer.start();
app.use('/graphql', bodyParser.json(), expressMiddleware(apolloServer));
httpServer.listen(port, () => {
console.log(`🚀 Query endpoint ready at http://localhost:${port}/graphql`);
console.log(`🚀 Subscription endpoint ready at ws://localhost:${port}/graphql`);
});