/
oak.ts
63 lines (50 loc) · 1.59 KB
/
oak.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
import {
Application,
Middleware,
Request as OakRequest,
Router,
} from 'https://deno.land/x/oak@14.2.0/mod.ts'
import { GraphQLHTTP } from '../mod.ts'
import { makeExecutableSchema } from 'npm:@graphql-tools/schema@10.0.3'
import { gql } from 'https://deno.land/x/graphql_tag@0.1.2/mod.ts'
const typeDefs = gql`
type Query {
hello: String
}
`
const resolvers = {
Query: {
hello: (_root: undefined, _args: unknown, ctx: { request: OakRequest }) => {
return `Hello from ${ctx.request.url}`
},
},
}
const schema = makeExecutableSchema({ typeDefs, resolvers })
const handleGraphQL: Middleware = async (ctx) => {
// cast Oak request into a normal Request
const req = new Request(ctx.request.url.toString(), {
body: ctx.request.originalRequest.getBody().body,
headers: ctx.request.headers,
method: ctx.request.method,
})
const res = await GraphQLHTTP<OakRequest>({
schema,
graphiql: true,
context: () => ({ request: ctx.request }),
})(req)
for (const [k, v] of res.headers.entries()) ctx.response.headers.append(k, v)
ctx.response.status = res.status
ctx.response.body = res.body
}
const graphqlRouter = new Router().all('/graphql', handleGraphQL)
const app = new Application().use(
graphqlRouter.routes(),
graphqlRouter.allowedMethods(),
)
app.addEventListener('listen', ({ secure, hostname, port }) => {
if (hostname === '0.0.0.0') hostname = 'localhost'
const protocol = secure ? 'https' : 'http'
const url = `${protocol}://${hostname ?? 'localhost'}:${port}`
console.log('☁ Started on ' + url)
})
await app.listen({ port: 3000 })