-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.ts
48 lines (42 loc) · 1.46 KB
/
webserver.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
import {
GraphQLServer,
JSON_CONTENT_TYPE_HEADER,
JsonLogger,
returnDataResponse,
} from './deps.ts'
import { userSchema, userSchemaResolvers } from './ExampleSchemas.ts'
const responseHeaders = new Headers(JSON_CONTENT_TYPE_HEADER)
const logger = new JsonLogger('deno-graphql-server', 'user-service', false)
const customGraphQLServer = new GraphQLServer({
schema: userSchema,
rootValue: userSchemaResolvers,
logger: logger,
})
async function handleRequest(request: Request): Promise<Response> {
if (request.method === 'POST') {
const body = await request.json()
const graphQLResponse = await customGraphQLServer.handleRequest({
body: body,
headers: JSON_CONTENT_TYPE_HEADER,
url: request.url,
method: request.method,
})
// const metrics = await customGraphQLServer.getMetrics()
// logger.info(`Metrics are ${metrics}`)
const response = new Response(
JSON.stringify(graphQLResponse.executionResult),
{
status: graphQLResponse.statusCode,
headers: responseHeaders,
},
)
return response
}
return returnDataResponse({
message:
'Welcome to deno-graphql example. Please use POST method to send your GraphQL request to this API!',
}, responseHeaders)
}
export function startGraphQLServer(options: Deno.ServeOptions | Deno.ServeTlsOptions): Deno.HttpServer {
return Deno.serve(options, handleRequest)
}