Skip to content

Commit 69b1f43

Browse files
authored
get graphql-http working with netlify (#3353)
1 parent eee11cc commit 69b1f43

File tree

5 files changed

+63
-52
lines changed

5 files changed

+63
-52
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = {
2929
'next-env.d.ts',
3030
'changesets/**/*.md',
3131
'**/CHANGELOG.md',
32+
'functions/*',
3233
],
3334
overrides: [
3435
{

functions/graphql.ts

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

functions/schema-demo.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

packages/graphiql/resources/renderExample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function getSchemaUrl() {
7575
}
7676
return '/graphql';
7777
}
78-
return '/.netlify/functions/schema-demo';
78+
return '/.netlify/functions/graphql';
7979
}
8080

8181
// Render <GraphiQL /> into the body.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2+
"extends": "./resources/tsconfig.base.cjs.json",
23
"compilerOptions": {
34
"composite": true
45
},
56
"files": [],
6-
"include": [],
77
"references": [
88
{
99
"path": "resources/tsconfig.build.esm.json"

0 commit comments

Comments
 (0)