@@ -92,16 +92,42 @@ yarn add graphql-playground-middleware
92
92
93
93
#### Use
94
94
``` 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
97
117
98
118
const app = express ()
99
119
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 )
101
125
102
- app . listen ( 3000 )
126
+ console . log ( ` Serving the GraphQL Playground on http://localhost: ${ PORT } /playground ` )
103
127
```
104
128
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
+
105
131
### As Hapi Middleware
106
132
107
133
#### Install
@@ -111,24 +137,68 @@ yarn add graphql-playground-middleware
111
137
112
138
#### Use
113
139
``` 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
+ })
118
165
119
166
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
+ });
122
183
123
184
server .register ({
124
- register: playground ,
185
+ register: hapiPlayground ,
125
186
options: {
126
187
path: ' /playground' ,
127
188
endpoint: ' /graphql'
128
189
}
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
+ });
130
198
```
131
199
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
+
132
202
### As Koa Middleware
133
203
134
204
#### Install
@@ -138,22 +208,96 @@ yarn add graphql-playground-middleware
138
208
139
209
#### Use
140
210
``` 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 ());
144
240
145
- const app = new Koa ()
146
- const router = new KoaRouter ()
241
+ router .post (' /graphql' , graphqlKoa ({ schema }));
147
242
148
- router .all (' /playground' , playground ({
243
+ router .all (' /playground' , koaPlayground ({
149
244
endpoint: ' /graphql'
150
245
}))
151
246
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` )
155
252
```
156
253
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
+
157
301
## Development [](https://badge.fury.io/js/graphql-playground)
158
302
159
303
This is a mono-repo setup containing packages for the ` graphql-playground` and `graphql-playground-electron`.
0 commit comments