Skip to content

Commit aa05169

Browse files
committed
Added koa example
1 parent e7cf887 commit aa05169

File tree

3 files changed

+431
-0
lines changed

3 files changed

+431
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const koa = require('koa')
2+
const koaRouter = require('koa-router')
3+
const koaBody = require('koa-bodyparser')
4+
const { graphqlKoa } = require('apollo-server-koa')
5+
const {makeExecutableSchema} = require('graphql-tools')
6+
const {koaPlayground} = require('graphql-playground-middleware')
7+
8+
const schema = makeExecutableSchema({
9+
typeDefs: `
10+
type Query {
11+
hello: String!
12+
}
13+
schema {
14+
query: Query
15+
}
16+
`,
17+
resolvers: {
18+
Query: {
19+
hello: () => 'world',
20+
},
21+
},
22+
})
23+
24+
const app = new koa();
25+
const router = new koaRouter();
26+
const PORT = 4000;
27+
28+
// koaBody is needed just for POST.
29+
app.use(koaBody());
30+
31+
router.post('/graphql', graphqlKoa({ schema }));
32+
33+
router.all('/playground', koaPlayground({
34+
endpoint: '/graphql'
35+
}))
36+
37+
app.use(router.routes());
38+
app.use(router.allowedMethods());
39+
app.listen(PORT);
40+
41+
console.log(`Serving the GraphQL Playground on http://localhost:${PORT}/playground`)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "koa",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"apollo-server-koa": "^1.2.0",
14+
"graphql": "^0.11.7",
15+
"graphql-playground-middleware": "^1.0.36-alpha.10",
16+
"graphql-tools": "^2.7.1",
17+
"koa": "^2.3.0",
18+
"koa-bodyparser": "^4.2.0",
19+
"koa-router": "^7.2.1"
20+
}
21+
}

0 commit comments

Comments
 (0)