Skip to content

Commit 750e600

Browse files
authored
feat(use): Built-in handlers for some environments and frameworks (#13)
1 parent 01c45d8 commit 750e600

36 files changed

+1585
-616
lines changed

README.md

+71-109
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,18 @@ const schema = new GraphQLSchema({
5151

5252
```js
5353
import http from 'http';
54-
import { createHandler } from 'graphql-http';
54+
import { createHandler } from 'graphql-http/lib/use/node';
5555
import { schema } from './previous-step';
5656

57-
// Create the GraphQL over HTTP handler
57+
// Create the GraphQL over HTTP Node request handler
5858
const handler = createHandler({ schema });
5959

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();
8266
}
8367
});
8468

@@ -98,10 +82,10 @@ $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
9882
```js
9983
import fs from 'fs';
10084
import http2 from 'http2';
101-
import { createHandler } from 'graphql-http';
85+
import { createHandler } from 'graphql-http/lib/use/node';
10286
import { schema } from './previous-step';
10387

104-
// Create the GraphQL over HTTP handler
88+
// Create the GraphQL over HTTP Node request handler
10589
const handler = createHandler({ schema });
10690

10791
// Create a HTTP/2 server using the handler on `/graphql`
@@ -110,27 +94,11 @@ const server = http2.createSecureServer(
11094
key: fs.readFileSync('localhost-privkey.pem'),
11195
cert: fs.readFileSync('localhost-cert.pem'),
11296
},
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();
134102
}
135103
},
136104
);
@@ -143,102 +111,81 @@ console.log('Listening to port 4000');
143111

144112
```js
145113
import express from 'express'; // yarn add express
146-
import { createHandler } from 'graphql-http';
114+
import { createHandler } from 'graphql-http/lib/use/express';
147115
import { schema } from './previous-step';
148116

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
153119
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 }));
173121

174-
app.listen(4000);
122+
app.listen({ port: 4000 });
175123
console.log('Listening to port 4000');
176124
```
177125

178126
##### With [`fastify`](https://www.fastify.io/)
179127

180128
```js
181129
import Fastify from 'fastify'; // yarn add fastify
182-
import { createHandler } from 'graphql-http';
130+
import { createHandler } from 'graphql-http/lib/use/fastify';
183131
import { schema } from './previous-step';
184132

185-
// Create the GraphQL over HTTP handler
186-
const handler = createHandler({ schema });
187-
188133
// Create a fastify instance serving all methods on `/graphql`
134+
// where the GraphQL over HTTP fastify request handler is
189135
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 }));
204137

205-
fastify.listen(4000);
138+
fastify.listen({ port: 4000 });
206139
console.log('Listening to port 4000');
207140
```
208141

209142
##### With [`Deno`](https://deno.land/)
210143

211144
```ts
212145
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';
214147
import { schema } from './previous-step';
215148

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 });
218151

219152
// Start serving on `/graphql` using the handler
220153
await serve(
221-
async (req: Request) => {
154+
(req: Request) => {
222155
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 });
225160
}
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);
235161
},
236162
{
237-
port: 4000,
163+
port: 4000, // Listening to port 4000
238164
},
239165
);
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 });
240176

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+
};
242189
```
243190

244191
#### Use the client
@@ -553,7 +500,7 @@ const client = createClient({
553500
<summary><a href="#deno-client">🔗</a> Client usage in Deno</summary>
554501

555502
```js
556-
import { createClient } from 'graphql-http';
503+
import { createClient } from 'https://esm.sh/graphql-http';
557504

558505
const client = createClient({
559506
url: 'http://deno.earth:4000/graphql',
@@ -564,6 +511,21 @@ const client = createClient({
564511

565512
</details>
566513

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+
567529
<details id="auth">
568530
<summary><a href="#auth">🔗</a> Server handler usage with authentication</summary>
569531

0 commit comments

Comments
 (0)