@@ -51,34 +51,18 @@ const schema = new GraphQLSchema({
51
51
52
52
``` js
53
53
import http from ' http' ;
54
- import { createHandler } from ' graphql-http' ;
54
+ import { createHandler } from ' graphql-http/lib/use/node ' ;
55
55
import { schema } from ' ./previous-step' ;
56
56
57
- // Create the GraphQL over HTTP handler
57
+ // Create the GraphQL over HTTP Node request handler
58
58
const handler = createHandler ({ schema });
59
59
60
- // Create a HTTP server using the handler on `/graphql`
61
- const server = http .createServer (async (req , res ) => {
62
- if (! req .url .startsWith (' /graphql' )) {
63
- return res .writeHead (404 ).end ();
64
- }
65
-
66
- try {
67
- const [body , init ] = await handler ({
68
- url: req .url ,
69
- method: req .method ,
70
- headers: req .headers ,
71
- body : () =>
72
- new Promise ((resolve ) => {
73
- let body = ' ' ;
74
- req .on (' data' , (chunk ) => (body += chunk));
75
- req .on (' end' , () => resolve (body));
76
- }),
77
- raw: req,
78
- });
79
- res .writeHead (init .status , init .statusText , init .headers ).end (body);
80
- } catch (err) {
81
- res .writeHead (500 ).end (err .message );
60
+ // Create a HTTP server using the listner on `/graphql`
61
+ const server = http .createServer ((req , res ) => {
62
+ if (req .url .startsWith (' /graphql' )) {
63
+ handler (req, res);
64
+ } else {
65
+ res .writeHead (404 ).end ();
82
66
}
83
67
});
84
68
@@ -98,10 +82,10 @@ $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
98
82
``` js
99
83
import fs from ' fs' ;
100
84
import http2 from ' http2' ;
101
- import { createHandler } from ' graphql-http' ;
85
+ import { createHandler } from ' graphql-http/lib/use/node ' ;
102
86
import { schema } from ' ./previous-step' ;
103
87
104
- // Create the GraphQL over HTTP handler
88
+ // Create the GraphQL over HTTP Node request handler
105
89
const handler = createHandler ({ schema });
106
90
107
91
// Create a HTTP/2 server using the handler on `/graphql`
@@ -110,27 +94,11 @@ const server = http2.createSecureServer(
110
94
key: fs .readFileSync (' localhost-privkey.pem' ),
111
95
cert: fs .readFileSync (' localhost-cert.pem' ),
112
96
},
113
- async (req , res ) => {
114
- if (! req .url .startsWith (' /graphql' )) {
115
- return res .writeHead (404 ).end ();
116
- }
117
-
118
- try {
119
- const [body , init ] = await handler ({
120
- url: req .url ,
121
- method: req .method ,
122
- headers: req .headers ,
123
- body : () =>
124
- new Promise ((resolve ) => {
125
- let body = ' ' ;
126
- req .on (' data' , (chunk ) => (body += chunk));
127
- req .on (' end' , () => resolve (body));
128
- }),
129
- raw: req,
130
- });
131
- res .writeHead (init .status , init .statusText , init .headers ).end (body);
132
- } catch (err) {
133
- res .writeHead (500 ).end (err .message );
97
+ (req , res ) => {
98
+ if (req .url .startsWith (' /graphql' )) {
99
+ handler (req, res);
100
+ } else {
101
+ res .writeHead (404 ).end ();
134
102
}
135
103
},
136
104
);
@@ -143,102 +111,81 @@ console.log('Listening to port 4000');
143
111
144
112
``` js
145
113
import express from ' express' ; // yarn add express
146
- import { createHandler } from ' graphql-http' ;
114
+ import { createHandler } from ' graphql-http/lib/use/express ' ;
147
115
import { schema } from ' ./previous-step' ;
148
116
149
- // Create the GraphQL over HTTP handler
150
- const handler = createHandler ({ schema });
151
-
152
- // Create an express app serving all methods on `/graphql`
117
+ // Create a express instance serving all methods on `/graphql`
118
+ // where the GraphQL over HTTP express request handler is
153
119
const app = express ();
154
- app .use (' /graphql' , async (req , res ) => {
155
- try {
156
- const [body , init ] = await handler ({
157
- url: req .url ,
158
- method: req .method ,
159
- headers: req .headers ,
160
- body : () =>
161
- new Promise ((resolve ) => {
162
- let body = ' ' ;
163
- req .on (' data' , (chunk ) => (body += chunk));
164
- req .on (' end' , () => resolve (body));
165
- }),
166
- raw: req,
167
- });
168
- res .writeHead (init .status , init .statusText , init .headers ).end (body);
169
- } catch (err) {
170
- res .writeHead (500 ).end (err .message );
171
- }
172
- });
120
+ app .all (' /graphql' , createHandler ({ schema }));
173
121
174
- app .listen (4000 );
122
+ app .listen ({ port : 4000 } );
175
123
console .log (' Listening to port 4000' );
176
124
```
177
125
178
126
##### With [ ` fastify ` ] ( https://www.fastify.io/ )
179
127
180
128
``` js
181
129
import Fastify from ' fastify' ; // yarn add fastify
182
- import { createHandler } from ' graphql-http' ;
130
+ import { createHandler } from ' graphql-http/lib/use/fastify ' ;
183
131
import { schema } from ' ./previous-step' ;
184
132
185
- // Create the GraphQL over HTTP handler
186
- const handler = createHandler ({ schema });
187
-
188
133
// Create a fastify instance serving all methods on `/graphql`
134
+ // where the GraphQL over HTTP fastify request handler is
189
135
const fastify = Fastify ();
190
- fastify .all (' /graphql' , async (req , res ) => {
191
- try {
192
- const [body , init ] = await handler ({
193
- url: req .url ,
194
- method: req .method ,
195
- headers: req .headers ,
196
- body: req .body , // fastify reads the body for you
197
- raw: req,
198
- });
199
- res .writeHead (init .status , init .statusText , init .headers ).end (body);
200
- } catch (err) {
201
- res .writeHead (500 ).end (err .message );
202
- }
203
- });
136
+ fastify .all (' /graphql' , createHandler ({ schema }));
204
137
205
- fastify .listen (4000 );
138
+ fastify .listen ({ port : 4000 } );
206
139
console .log (' Listening to port 4000' );
207
140
```
208
141
209
142
##### With [ ` Deno ` ] ( https://deno.land/ )
210
143
211
144
``` ts
212
145
import {
serve }
from ' https://deno.land/[email protected] /http/server.ts' ;
213
- import { createHandler } from ' https://esm.sh/graphql-http' ;
146
+ import { createHandler } from ' https://esm.sh/graphql-http/lib/use/fetch ' ;
214
147
import { schema } from ' ./previous-step' ;
215
148
216
- // Create the GraphQL over HTTP handler
217
- const handler = createHandler < Request > ({ schema });
149
+ // Create the GraphQL over HTTP native fetch handler
150
+ const handler = createHandler ({ schema });
218
151
219
152
// Start serving on `/graphql` using the handler
220
153
await serve (
221
- async (req : Request ) => {
154
+ (req : Request ) => {
222
155
const [path, _search] = req .url .split (' ?' );
223
- if (! path .endsWith (' /graphql' )) {
224
- return new Response (null , { status: 404 , statusText: ' Not Found' });
156
+ if (path .endsWith (' /graphql' )) {
157
+ return handler (req );
158
+ } else {
159
+ return new Response (null , { status: 404 });
225
160
}
226
-
227
- const [body, init] = await handler ({
228
- url: req .url ,
229
- method: req .method ,
230
- headers: req .headers ,
231
- body : () => req .text (),
232
- raw: req ,
233
- });
234
- return new Response (body , init );
235
161
},
236
162
{
237
- port: 4000 ,
163
+ port: 4000 , // Listening to port 4000
238
164
},
239
165
);
166
+ ```
167
+
168
+ ##### With [ ` Bun ` ] ( https://bun.sh/ )
169
+
170
+ ``` js
171
+ import { createHandler } from ' graphql-http/lib/use/fetch' ; // bun install graphql-http
172
+ import { schema } from ' ./previous-step' ;
173
+
174
+ // Create the GraphQL over HTTP native fetch handler
175
+ const handler = createHandler ({ schema });
240
176
241
- // Listening to port 4000
177
+ // Start serving on `/graphql` using the handler
178
+ export default {
179
+ port: 4000 , // Listening to port 4000
180
+ fetch (req ) {
181
+ const [path , _search ] = req .url .split (' ?' );
182
+ if (path .endsWith (' /graphql' )) {
183
+ return handler (req);
184
+ } else {
185
+ return new Response (null , { status: 404 });
186
+ }
187
+ },
188
+ };
242
189
```
243
190
244
191
#### Use the client
@@ -553,7 +500,7 @@ const client = createClient({
553
500
<summary ><a href =" #deno-client " >🔗</a > Client usage in Deno</summary >
554
501
555
502
``` js
556
- import { createClient } from ' graphql-http' ;
503
+ import { createClient } from ' https://esm.sh/ graphql-http' ;
557
504
558
505
const client = createClient ({
559
506
url: ' http://deno.earth:4000/graphql' ,
@@ -564,6 +511,21 @@ const client = createClient({
564
511
565
512
</details >
566
513
514
+ <details id =" bun-client " >
515
+ <summary ><a href =" #bun-client " >🔗</a > Client usage in Bun</summary >
516
+
517
+ ``` js
518
+ import { createClient } from ' graphql-http' ; // bun install graphql-http
519
+
520
+ const client = createClient ({
521
+ url: ' http://bun.bread:4000/graphql' ,
522
+ });
523
+
524
+ // consider other recipes for usage inspiration
525
+ ```
526
+
527
+ </details >
528
+
567
529
<details id =" auth " >
568
530
<summary ><a href =" #auth " >🔗</a > Server handler usage with authentication</summary >
569
531
0 commit comments