Skip to content

Commit 94b3cf5

Browse files
committed
Working Next.js GraphQL server
0 parents  commit 94b3cf5

File tree

8 files changed

+125
-0
lines changed

8 files changed

+125
-0
lines changed

.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"presets": [
3+
"next/babel"
4+
],
5+
}

.eslintrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"mocha": true,
5+
},
6+
"extends": "airbnb",
7+
"parser": "babel-eslint",
8+
"rules": {
9+
"semi": 0,
10+
"no-unused-vars": 0,
11+
"arrow-body-style": 0
12+
}
13+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.next/

data/resolvers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
Query: {
3+
foo(root, args, context) {
4+
return 'bar'
5+
},
6+
},
7+
}

data/schema.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default [`
2+
type Query {
3+
foo: String
4+
}
5+
6+
schema {
7+
query: Query
8+
}
9+
`]

package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "graphql-guide",
3+
"version": "0.0.1",
4+
"description": "https://graphql.guide",
5+
"main": "server.js",
6+
"scripts": {
7+
"dev": "babel-node server.js",
8+
"build": "next build",
9+
"start": "NODE_ENV=production babel-node server.js",
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+ssh://[email protected]/GraphQLGuide/guide.git"
15+
},
16+
"author": "Loren Sands Ramshaw and Jonas Helfer",
17+
"bugs": {
18+
"url": "https://github.com/GraphQLGuide/guide/issues"
19+
},
20+
"homepage": "https://github.com/GraphQLGuide/guide#readme",
21+
"license": "UNLICENCED",
22+
"private": true,
23+
"dependencies": {
24+
"body-parser": "^1.15.2",
25+
"express": "^4.14.0",
26+
"graphql": "^0.8.2",
27+
"graphql-server-express": "^0.4.4",
28+
"graphql-tools": "^0.9.0",
29+
"next": "^2.0.0-beta",
30+
"react": "^15.4.2"
31+
},
32+
"devDependencies": {
33+
"babel-cli": "^6.18.0",
34+
"babel-eslint": "^7.1.1",
35+
"eslint": "^3.13.1",
36+
"eslint-config-airbnb": "^14.0.0",
37+
"eslint-plugin-import": "^2.2.0",
38+
"eslint-plugin-jsx-a11y": "^3.0.2",
39+
"eslint-plugin-react": "^6.9.0"
40+
}
41+
}

pages/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Link from 'next/link'
2+
3+
export default () => (
4+
<ul>
5+
<li><Link href='/b' as='/a'><a>a</a></Link></li>
6+
<li><Link href='/a' as='/b'><a>b</a></Link></li>
7+
</ul>
8+
)

server.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import express from 'express'
2+
import next from 'next'
3+
4+
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express'
5+
import { makeExecutableSchema } from 'graphql-tools'
6+
import bodyParser from 'body-parser'
7+
8+
import typeDefs from './data/schema'
9+
import resolvers from './data/resolvers'
10+
11+
const PORT = process.env.PORT || 3000
12+
const dev = process.env.NODE_ENV !== 'production'
13+
const app = next({ dir: '.', dev })
14+
const handle = app.getRequestHandler()
15+
const executableSchema = makeExecutableSchema({
16+
typeDefs,
17+
resolvers,
18+
})
19+
20+
app.prepare()
21+
.then(() => {
22+
const server = express()
23+
24+
server.use('/graphql', bodyParser.json(), graphqlExpress({
25+
schema: executableSchema,
26+
}))
27+
28+
server.use('/graphiql', graphiqlExpress({
29+
endpointURL: '/graphql',
30+
}))
31+
32+
server.get('*', (req, res) => {
33+
return handle(req, res)
34+
})
35+
36+
server.listen(PORT, (err) => {
37+
if (err) throw err
38+
console.log(`> Ready on http://localhost:${PORT}`) // eslint-disable-line
39+
})
40+
})

0 commit comments

Comments
 (0)