-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.ts
59 lines (51 loc) · 2.01 KB
/
graphql.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
import 'reflect-metadata';
import type { RequestHandler, Response } from '@sveltejs/kit';
import { getGraphQLParameters } from 'graphql-helix/dist/get-graphql-parameters.js';
import { processRequest } from 'graphql-helix/dist/process-request.js';
import { renderGraphiQL } from 'graphql-helix/dist/render-graphiql.js';
import { shouldRenderGraphiQL } from 'graphql-helix/dist/should-render-graphiql.js';
import { createSchema, defaultQuery } from '../graphql/schema';
const schemaPromise = createSchema();
const respond = async (request): Promise<Response> => {
if (shouldRenderGraphiQL(request))
return {
body: renderGraphiQL({
defaultQuery
}),
headers: { 'Content-Type': 'text/html' },
status: 200
};
const parameters = getGraphQLParameters(request);
const result = await processRequest({
...parameters,
// For example, auth information is put in context for the resolver
contextFactory: () => ({
authorization: request.headers['Authorization'] ?? request.headers['authorization']
}),
request,
schema: await schemaPromise
});
if (result.type === 'RESPONSE')
return {
body: result.payload,
headers: result.headers,
status: result.status
};
return {
// Think you could help?
// https://github.com/svelte-add/graphql/issues/1
body: "svelte-add/graphql doesn't support multipart responses or event streams",
headers: {},
status: 501
};
};
export const del: RequestHandler = ({ body, query, context: { headers } }) =>
respond({ body, headers, method: 'DELETE', query });
export const get: RequestHandler = ({ body, query, context: { headers } }) =>
respond({ body, headers, method: 'GET', query });
export const head: RequestHandler = ({ body, query, context: { headers } }) =>
respond({ body, headers, method: 'HEAD', query });
export const post: RequestHandler = ({ body, query, context: { headers } }) =>
respond({ body, headers, method: 'POST', query });
export const put: RequestHandler = ({ body, query, context: { headers } }) =>
respond({ body, headers, method: 'PUT', query });