Skip to content

Commit e5b466e

Browse files
authored
Update README.md
1 parent b5da2d8 commit e5b466e

File tree

1 file changed

+165
-21
lines changed

1 file changed

+165
-21
lines changed

README.md

Lines changed: 165 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,42 @@ yarn add graphql-playground-middleware
9292

9393
#### Use
9494
```js
95-
import express from 'express'
96-
import { express as playground } from 'graphql-playground-middleware'
95+
const express = require('express')
96+
const bodyParser = require('body-parser')
97+
const {graphqlExpress} = require('apollo-server-express')
98+
const {makeExecutableSchema} = require('graphql-tools')
99+
const {expressPlayground} = require('graphql-playground-middleware')
100+
101+
const schema = makeExecutableSchema({
102+
typeDefs: `
103+
type Query {
104+
hello: String!
105+
}
106+
schema {
107+
query: Query
108+
}
109+
`,
110+
resolvers: {
111+
Query: {
112+
hello: () => 'world',
113+
},
114+
},
115+
})
116+
const PORT = 4000
97117

98118
const app = express()
99119

100-
app.use('/playground', playground({ endpoint: '/graphql' }))
120+
// bodyParser is needed just for POST.
121+
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }))
122+
app.get('/playground', expressPlayground({ endpoint: '/graphql' })) // if you want GraphiQL enabled
123+
124+
app.listen(PORT)
101125

102-
app.listen(3000)
126+
console.log(`Serving the GraphQL Playground on http://localhost:${PORT}/playground`)
103127
```
104128

129+
See [./packages/graphql-playground-middleware/examples/express](https://github.com/graphcool/graphql-playground/tree/master/packages/graphql-playground-middleware/examples/express) for a full example.
130+
105131
### As Hapi Middleware
106132

107133
#### Install
@@ -111,24 +137,68 @@ yarn add graphql-playground-middleware
111137

112138
#### Use
113139
```js
114-
import hapi from 'hapi'
115-
import { hapi as playground } from 'graphql-playground-middleware'
116-
117-
const server = new Hapi.Server()
140+
const hapi = require('hapi')
141+
const {graphqlHapi} = require('apollo-server-hapi')
142+
const {hapiPlayground} = require('graphql-playground-middleware')
143+
const {makeExecutableSchema} = require('graphql-tools')
144+
145+
const server = new hapi.Server({ debug: { request: "*" } });
146+
147+
const HOST = 'localhost';
148+
const PORT = 4000;
149+
150+
const schema = makeExecutableSchema({
151+
typeDefs: `
152+
type Query {
153+
hello: String!
154+
}
155+
schema {
156+
query: Query
157+
}
158+
`,
159+
resolvers: {
160+
Query: {
161+
hello: () => 'world',
162+
},
163+
},
164+
})
118165

119166
server.connection({
120-
port: 3001
121-
})
167+
host: HOST,
168+
port: PORT,
169+
});
170+
171+
server.register({
172+
register: graphqlHapi,
173+
options: {
174+
path: '/graphql',
175+
graphqlOptions: {
176+
schema,
177+
},
178+
route: {
179+
cors: true
180+
}
181+
},
182+
});
122183

123184
server.register({
124-
register: playground,
185+
register: hapiPlayground,
125186
options: {
126187
path: '/playground',
127188
endpoint: '/graphql'
128189
}
129-
},() => server.start())
190+
})
191+
192+
server.start((err) => {
193+
if (err) {
194+
throw err;
195+
}
196+
console.log(`Server running at: ${server.info.uri}`);
197+
});
130198
```
131199

200+
See [./packages/graphql-playground-middleware/examples/hapi](https://github.com/graphcool/graphql-playground/tree/master/packages/graphql-playground-middleware/examples/hapi) for a full example.
201+
132202
### As Koa Middleware
133203

134204
#### Install
@@ -138,22 +208,96 @@ yarn add graphql-playground-middleware
138208

139209
#### Use
140210
```js
141-
import Koa from 'koa'
142-
import KoaRouter from 'koa-router'
143-
import { koa as playground } from 'graphql-playground/middleware'
211+
const koa = require('koa')
212+
const koaRouter = require('koa-router')
213+
const koaBody = require('koa-bodyparser')
214+
const { graphqlKoa } = require('apollo-server-koa')
215+
const {makeExecutableSchema} = require('graphql-tools')
216+
const {koaPlayground} = require('graphql-playground-middleware')
217+
218+
const schema = makeExecutableSchema({
219+
typeDefs: `
220+
type Query {
221+
hello: String!
222+
}
223+
schema {
224+
query: Query
225+
}
226+
`,
227+
resolvers: {
228+
Query: {
229+
hello: () => 'world',
230+
},
231+
},
232+
})
233+
234+
const app = new koa();
235+
const router = new koaRouter();
236+
const PORT = 4000;
237+
238+
// koaBody is needed just for POST.
239+
app.use(koaBody());
144240

145-
const app = new Koa()
146-
const router = new KoaRouter()
241+
router.post('/graphql', graphqlKoa({ schema }));
147242

148-
router.all('/playground', playground({
243+
router.all('/playground', koaPlayground({
149244
endpoint: '/graphql'
150245
}))
151246

152-
app.use(router.routes())
153-
app.use(router.allowedMethods())
154-
app.listen(3001)
247+
app.use(router.routes());
248+
app.use(router.allowedMethods());
249+
app.listen(PORT);
250+
251+
console.log(`Serving the GraphQL Playground on http://localhost:${PORT}/playground`)
155252
```
156253

254+
See [./packages/graphql-playground-middleware/examples/koa](https://github.com/graphcool/graphql-playground/tree/master/packages/graphql-playground-middleware/examples/koa) for a full example.
255+
256+
### As serverless handler
257+
#### Install
258+
259+
#### Use
260+
`handler.js`
261+
262+
```js
263+
exports.graphqlHandler = function graphqlHandler(event, context, callback) {
264+
function callbackFilter(error, output) {
265+
// eslint-disable-next-line no-param-reassign
266+
output.headers['Access-Control-Allow-Origin'] = '*';
267+
callback(error, output);
268+
}
269+
270+
const handler = graphqlLambda({ schema: myGraphQLSchema });
271+
return handler(event, context, callbackFilter);
272+
};
273+
274+
exports.playgroundHandler = lambdaPlayground({
275+
endpoint: '/dev/graphql',
276+
});
277+
```
278+
279+
`serverless.yml`
280+
281+
```yaml
282+
functions:
283+
graphql:
284+
handler: handler.graphqlHandler
285+
events:
286+
- http:
287+
path: graphql
288+
method: post
289+
cors: true
290+
playground:
291+
handler: handler.playgroundHandler
292+
events:
293+
- http:
294+
path: playground
295+
method: get
296+
cors: true
297+
```
298+
299+
See [serverless-graphql-apollo](https://github.com/serverless/serverless-graphql-apollo) for a full example.
300+
157301
## Development [![npm version](https://badge.fury.io/js/graphql-playground.svg)](https://badge.fury.io/js/graphql-playground)
158302
159303
This is a mono-repo setup containing packages for the `graphql-playground` and `graphql-playground-electron`.

0 commit comments

Comments
 (0)