Skip to content

Commit 2b1ab14

Browse files
authored
feat: Use Netlify Functions (#96)
1 parent ef8b311 commit 2b1ab14

File tree

6 files changed

+141
-0
lines changed

6 files changed

+141
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ export default {
222222
};
223223
```
224224

225+
##### With [`Netlify Functions`](https://docs.netlify.com/functions/overview/)
226+
227+
```js
228+
import { createHandler } from 'graphql-http/lib/use/@netlify/functions'; // yarn add @netlify/functions
229+
import { schema } from './previous-step';
230+
231+
// Create the GraphQL over HTTP native fetch handler
232+
export const handler = createHandler({ schema });
233+
```
234+
225235
#### Use the client
226236

227237
```js

docs/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ graphql-http
1212
- [client](modules/client.md)
1313
- [common](modules/common.md)
1414
- [handler](modules/handler.md)
15+
- [use/@netlify/functions](modules/use__netlify_functions.md)
1516
- [use/express](modules/use_express.md)
1617
- [use/fastify](modules/use_fastify.md)
1718
- [use/fetch](modules/use_fetch.md)
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[graphql-http](../README.md) / use/@netlify/functions
2+
3+
# Module: use/@netlify/functions
4+
5+
## Table of contents
6+
7+
### Type Aliases
8+
9+
- [HandlerOptions](use__netlify_functions.md#handleroptions)
10+
11+
### Functions
12+
13+
- [createHandler](use__netlify_functions.md#createhandler)
14+
15+
## Server/@netlify/functions
16+
17+
### HandlerOptions
18+
19+
Ƭ **HandlerOptions**<`Context`\>: [`HandlerOptions`](../interfaces/handler.HandlerOptions.md)<`NetlifyHandlerEvent`, `NetlifyHandlerContext`, `Context`\>
20+
21+
Handler options when using the netlify adapter
22+
23+
#### Type parameters
24+
25+
| Name | Type |
26+
| :------ | :------ |
27+
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |
28+
29+
___
30+
31+
### createHandler
32+
33+
**createHandler**<`Context`\>(`options`): `NetlifyHandler`
34+
35+
Create a GraphQL over HTTP spec compliant request handler for netlify functions
36+
37+
#### Type parameters
38+
39+
| Name | Type |
40+
| :------ | :------ |
41+
| `Context` | extends [`OperationContext`](handler.md#operationcontext) = `undefined` |
42+
43+
#### Parameters
44+
45+
| Name | Type |
46+
| :------ | :------ |
47+
| `options` | [`HandlerOptions`](use__netlify_functions.md#handleroptions)<`Context`\> |
48+
49+
#### Returns
50+
51+
`NetlifyHandler`

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
"require": "./lib/use/fastify.js",
6666
"import": "./lib/use/fastify.mjs"
6767
},
68+
"./lib/use/@netlify/functions": {
69+
"types": "./lib/use/@netlify/functions.d.ts",
70+
"require": "./lib/use/@netlify/functions.js",
71+
"import": "./lib/use/@netlify/functions.mjs"
72+
},
6873
"./lib/use/koa": {
6974
"types": "./lib/use/koa.d.ts",
7075
"require": "./lib/use/koa.js",
@@ -112,6 +117,7 @@
112117
},
113118
"devDependencies": {
114119
"@cspell/cspell-types": "^6.31.1",
120+
"@netlify/functions": "^1.6.0",
115121
"@rollup/plugin-terser": "^0.4.3",
116122
"@rollup/plugin-typescript": "^11.1.2",
117123
"@semantic-release/changelog": "^6.0.3",

src/use/@netlify/functions.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {
2+
createHandler as createRawHandler,
3+
HandlerOptions as RawHandlerOptions,
4+
OperationContext,
5+
} from '../../handler';
6+
7+
import type {
8+
Handler as NetlifyHandler,
9+
HandlerEvent as NetlifyHandlerEvent,
10+
HandlerContext as NetlifyHandlerContext,
11+
} from '@netlify/functions';
12+
13+
/**
14+
* Handler options when using the netlify adapter
15+
*
16+
* @category Server/@netlify/functions
17+
*/
18+
export type HandlerOptions<Context extends OperationContext = undefined> =
19+
RawHandlerOptions<NetlifyHandlerEvent, NetlifyHandlerContext, Context>;
20+
21+
/**
22+
* Create a GraphQL over HTTP spec compliant request handler for netlify functions
23+
*
24+
* @category Server/@netlify/functions
25+
*/
26+
export function createHandler<Context extends OperationContext = undefined>(
27+
options: HandlerOptions<Context>,
28+
): NetlifyHandler {
29+
const handler = createRawHandler(options);
30+
return async function handleRequest(req, ctx) {
31+
try {
32+
const [body, init] = await handler({
33+
method: req.httpMethod,
34+
url: req.rawUrl,
35+
headers: req.headers,
36+
body: req.body,
37+
raw: req,
38+
context: ctx,
39+
});
40+
return {
41+
// if body is null, return undefined
42+
body: body ?? undefined,
43+
statusCode: init.status,
44+
};
45+
} catch (err) {
46+
// The handler shouldnt throw errors.
47+
// If you wish to handle them differently, consider implementing your own request handler.
48+
console.error(
49+
'Internal error occurred during request handling. ' +
50+
'Please check your implementation.',
51+
err,
52+
);
53+
return { statusCode: 500 };
54+
}
55+
};
56+
}

yarn.lock

+17
Original file line numberDiff line numberDiff line change
@@ -3073,6 +3073,15 @@ __metadata:
30733073
languageName: node
30743074
linkType: hard
30753075

3076+
"@netlify/functions@npm:^1.6.0":
3077+
version: 1.6.0
3078+
resolution: "@netlify/functions@npm:1.6.0"
3079+
dependencies:
3080+
is-promise: ^4.0.0
3081+
checksum: ecff9a1161b2df94d139431e7a2b42d9790cf4ec250ad3aec57935cc1b1e78beccfe855c6e5522311baefce84b91ed690904aedf02b2511eaaafdc8f6daab75e
3082+
languageName: node
3083+
linkType: hard
3084+
30763085
"@nicolo-ribaudo/semver-v6@npm:^6.3.3":
30773086
version: 6.3.3
30783087
resolution: "@nicolo-ribaudo/semver-v6@npm:6.3.3"
@@ -7499,6 +7508,7 @@ __metadata:
74997508
resolution: "graphql-http@workspace:."
75007509
dependencies:
75017510
"@cspell/cspell-types": ^6.31.1
7511+
"@netlify/functions": ^1.6.0
75027512
"@rollup/plugin-terser": ^0.4.3
75037513
"@rollup/plugin-typescript": ^11.1.2
75047514
"@semantic-release/changelog": ^6.0.3
@@ -8278,6 +8288,13 @@ __metadata:
82788288
languageName: node
82798289
linkType: hard
82808290

8291+
"is-promise@npm:^4.0.0":
8292+
version: 4.0.0
8293+
resolution: "is-promise@npm:4.0.0"
8294+
checksum: 0b46517ad47b00b6358fd6553c83ec1f6ba9acd7ffb3d30a0bf519c5c69e7147c132430452351b8a9fc198f8dd6c4f76f8e6f5a7f100f8c77d57d9e0f4261a8a
8295+
languageName: node
8296+
linkType: hard
8297+
82818298
"is-property@npm:^1.0.2":
82828299
version: 1.0.2
82838300
resolution: "is-property@npm:1.0.2"

0 commit comments

Comments
 (0)