Skip to content

Commit b97a9c0

Browse files
committed
docs: deno server example [skip ci]
1 parent 9e91141 commit b97a9c0

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

+35
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,41 @@ fastify.listen(4000);
207207
console.log('Listening to port 4000');
208208
```
209209

210+
##### With [`Deno`](https://deno.land/)
211+
212+
```ts
213+
import { serve } from 'https://deno.land/[email protected]/http/server.ts';
214+
import { createHandler } from 'https://esm.sh/graphql-http';
215+
import { schema } from './previous-step';
216+
217+
const handler = createHandler<Request>({ schema });
218+
219+
await serve(
220+
async (req: Request) => {
221+
const [path, _search] = req.url.split('?');
222+
if (!path.endsWith('/graphql')) {
223+
return new Response(null, { status: 404, statusText: 'Not Found' });
224+
}
225+
226+
const headers: Record<string, string> = {};
227+
req.headers.forEach((value, key) => (headers[key] = value));
228+
const [body, init] = await handler({
229+
url: req.url,
230+
method: req.method,
231+
headers,
232+
body: await req.text(),
233+
raw: req,
234+
});
235+
return new Response(body, init);
236+
},
237+
{
238+
port: 4000,
239+
},
240+
);
241+
242+
// Listening to port 4000
243+
```
244+
210245
#### Use the client
211246

212247
```js

0 commit comments

Comments
 (0)