Skip to content

Commit

Permalink
feat(use): Deprecate node adapter in favor of http and http2 ad…
Browse files Browse the repository at this point in the history
…apters
  • Loading branch information
enisdenjo committed Feb 13, 2023
1 parent f14a821 commit f5b0305
Show file tree
Hide file tree
Showing 10 changed files with 431 additions and 67 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const schema = new GraphQLSchema({

```js
import http from 'http';
import { createHandler } from 'graphql-http/lib/use/node';
import { createHandler } from 'graphql-http/lib/use/http';
import { schema } from './previous-step';

// Create the GraphQL over HTTP Node request handler
Expand Down Expand Up @@ -86,7 +86,7 @@ $ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
```js
import fs from 'fs';
import http2 from 'http2';
import { createHandler } from 'graphql-http/lib/use/node';
import { createHandler } from 'graphql-http/lib/use/http2';
import { schema } from './previous-step';

// Create the GraphQL over HTTP Node request handler
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ graphql-http
- [use/express](modules/use_express.md)
- [use/fastify](modules/use_fastify.md)
- [use/fetch](modules/use_fetch.md)
- [use/http](modules/use_http.md)
- [use/http2](modules/use_http2.md)
- [use/koa](modules/use_koa.md)
- [use/node](modules/use_node.md)
76 changes: 76 additions & 0 deletions docs/modules/use_http.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[graphql-http](../README.md) / use/http

# Module: use/http

## Table of contents

### Type Aliases

- [HandlerOptions](use_http.md#handleroptions)

### Functions

- [createHandler](use_http.md#createhandler)

## Server/http

### HandlerOptions

Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`IncomingMessage`, `undefined`, `Context`\>

Handler options when using the http adapter.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |

___

### createHandler

**createHandler**<`Context`\>(`options`): (`req`: `IncomingMessage`, `res`: `ServerResponse`) => `Promise`<`void`\>

Create a GraphQL over HTTP Protocol compliant request handler for
the Node environment http module.

```js
import http from 'http';
import { createHandler } from 'graphql-http/lib/use/http';
import { schema } from './my-graphql-step';

const server = http.createServer(createHandler({ schema }));

server.listen(4000);
console.log('Listening to port 4000');
```

#### Type parameters

| Name | Type |
| :------ | :------ |
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `options` | [`HandlerOptions`](use_http.md#handleroptions)<`Context`\> |

#### Returns

`fn`

▸ (`req`, `res`): `Promise`<`void`\>

##### Parameters

| Name | Type |
| :------ | :------ |
| `req` | `IncomingMessage` |
| `res` | `ServerResponse` |

##### Returns

`Promise`<`void`\>
88 changes: 88 additions & 0 deletions docs/modules/use_http2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[graphql-http](../README.md) / use/http2

# Module: use/http2

## Table of contents

### Type Aliases

- [HandlerOptions](use_http2.md#handleroptions)

### Functions

- [createHandler](use_http2.md#createhandler)

## Server/http2

### HandlerOptions

Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`Http2ServerRequest`, `undefined`, `Context`\>

Handler options when using the http adapter.

#### Type parameters

| Name | Type |
| :------ | :------ |
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |

___

### createHandler

**createHandler**<`Context`\>(`options`): (`req`: `Http2ServerRequest`, `res`: `Http2ServerResponse`) => `Promise`<`void`\>

Create a GraphQL over HTTP Protocol compliant request handler for
the Node environment http2 module.

```shell
$ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
-keyout localhost-privkey.pem -out localhost-cert.pem
```

```js
import fs from 'fs';
import http2 from 'http2';
import { createHandler } from 'graphql-http/lib/use/http2';
import { schema } from './my-graphql-step';

const server = http2.createSecureServer(
{
key: fs.readFileSync('localhost-privkey.pem'),
cert: fs.readFileSync('localhost-cert.pem'),
},
createHandler({ schema }),
);

server.listen(4000);
console.log('Listening to port 4000');
```

#### Type parameters

| Name | Type |
| :------ | :------ |
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `options` | [`HandlerOptions`](use_http2.md#handleroptions)<`Context`\> |

#### Returns

`fn`

▸ (`req`, `res`): `Promise`<`void`\>

##### Parameters

| Name | Type |
| :------ | :------ |
| `req` | `Http2ServerRequest` |
| `res` | `Http2ServerResponse` |

##### Returns

`Promise`<`void`\>
45 changes: 42 additions & 3 deletions docs/modules/use_node.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

### HandlerOptions

Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`IncomingMessage`, `undefined`, `Context`\>
Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](use_http.md#handleroptions)<`Context`\>

Handler options when using the node adapter.

**`Deprecated`**

Please use [http](use_http.md#handleroptions) or [http2](use_http2.md#handleroptions) adapters instead.

#### Type parameters

| Name | Type |
Expand All @@ -30,7 +34,7 @@ ___

### createHandler

**createHandler**<`Context`\>(`options`): `RequestListener`
**createHandler**<`Context`\>(`options`): (`req`: `IncomingMessage`, `res`: `ServerResponse`<`IncomingMessage`\>) => `Promise`<`void`\>

Create a GraphQL over HTTP Protocol compliant request handler for
the Node environment.
Expand All @@ -46,6 +50,10 @@ server.listen(4000);
console.log('Listening to port 4000');
```

**`Deprecated`**

Please use [http](use_http.md#createhandler) or [http2](use_http2.md#createhandler) adapters instead.

#### Type parameters

| Name | Type |
Expand All @@ -60,4 +68,35 @@ console.log('Listening to port 4000');

#### Returns

`RequestListener`
`fn`

▸ (`req`, `res`): `Promise`<`void`\>

Create a GraphQL over HTTP Protocol compliant request handler for
the Node environment http module.

```js
import http from 'http';
import { createHandler } from 'graphql-http/lib/use/http';
import { schema } from './my-graphql-step';

const server = http.createServer(createHandler({ schema }));

server.listen(4000);
console.log('Listening to port 4000');
```

**`Category`**

Server/http

##### Parameters

| Name | Type |
| :------ | :------ |
| `req` | `IncomingMessage` |
| `res` | `ServerResponse`<`IncomingMessage`\> |

##### Returns

`Promise`<`void`\>
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@
"require": "./lib/use/node.js",
"import": "./lib/use/node.mjs"
},
"./lib/use/http": {
"types": "./lib/use/http.d.ts",
"require": "./lib/use/http.js",
"import": "./lib/use/http.mjs"
},
"./lib/use/http2": {
"types": "./lib/use/http2.d.ts",
"require": "./lib/use/http2.js",
"import": "./lib/use/http2.mjs"
},
"./lib/use/express": {
"types": "./lib/use/express.d.ts",
"require": "./lib/use/express.js",
Expand Down
10 changes: 7 additions & 3 deletions src/__tests__/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { startDisposableServer } from './utils/tserver';
import { serverAudits } from '../audits';
import { schema } from './fixtures/simple';

import { createHandler as createNodeHandler } from '../use/node';
import { createHandler as createHttpHandler } from '../use/http';
import { createHandler as createExpressHandler } from '../use/express';
import { createHandler as createFastifyHandler } from '../use/fastify';
import { createHandler as createFetchHandler } from '../use/fetch';
import { createHandler as createKoaHandler } from '../use/koa';

describe('node', () => {
describe('http', () => {
const [url, dispose] = startDisposableServer(
http.createServer(createNodeHandler({ schema })),
http.createServer(createHttpHandler({ schema })),
);
afterAll(dispose);

Expand All @@ -31,6 +31,10 @@ describe('node', () => {
}
});

describe('http2', () => {
it.todo('should pass all server audits');
});

describe('express', () => {
const app = express();
app.all('/', createExpressHandler({ schema }));
Expand Down

0 comments on commit f5b0305

Please sign in to comment.