-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
mod.ts
86 lines (80 loc) · 2.17 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
accepts,
createHandler,
type OperationContext,
type RawRequest,
STATUS_TEXT,
type StatusCode,
} from './deps.ts'
import type { GQLOptions } from './types.ts'
function toRequest<Req = Request, Ctx = unknown>(
req: Pick<Request, 'method' | 'url' | 'headers' | 'text'>,
context: Ctx,
): RawRequest<Req, Ctx> {
return {
method: req.method,
url: req.url,
headers: req.headers,
body: () => req.text(),
raw: req as Req,
context,
}
}
export function GraphQLHTTP<
Req = Request,
Context extends OperationContext = { request: Req },
ReqCtx extends { request: Req } = { request: Req },
>(
{
headers = {},
graphiql,
playgroundOptions = {},
...options
}: GQLOptions<Req, ReqCtx, Context>,
reqCtx?: (req: Req) => ReqCtx,
): (req: Request) => Promise<Response> {
const handler = createHandler(options)
return async function handleRequest(req: Request): Promise<Response> {
try {
if (
req.method === 'GET' && graphiql && accepts(req)[0] === 'text/html'
) {
const urlQuery = req.url.substring(req.url.indexOf('?'))
const queryParams = new URLSearchParams(urlQuery)
if (!queryParams.has('raw')) {
const { renderPlaygroundPage } = await import('./graphiql/render.ts')
const playground = renderPlaygroundPage({
...playgroundOptions,
endpoint: '/graphql',
})
return new Response(playground, {
headers: new Headers({
'Content-Type': 'text/html',
...headers,
}),
})
}
}
const [body, init] = await handler(
toRequest<Req, ReqCtx>(
req,
reqCtx ? reqCtx(req as Req) : { request: req } as ReqCtx,
),
)
return new Response(
body || STATUS_TEXT[init.status as StatusCode],
{
...init,
headers: new Headers({ ...init.headers, ...headers }),
},
)
} catch (e) {
console.error(
'Internal error occurred during request handling. ' +
'Please check your implementation.',
e,
)
return new Response(null, { status: 500 })
}
}
}