forked from aaronwlee/oak-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
applyGraphQL.ts
140 lines (125 loc) · 3.71 KB
/
applyGraphQL.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { graphql } from "./deps.ts";
import { renderPlaygroundPage } from "./graphql-playground-html/render-playground-html.ts";
import { makeExecutableSchema } from "./graphql-tools/schema/makeExecutableSchema.ts";
interface Constructable<T> {
new(...args: any): T & OakRouter;
}
interface OakRouter {
post: any;
get: any;
}
export interface ApplyGraphQLOptions<T> {
Router: Constructable<T>;
path?: string;
typeDefs: any;
resolvers: ResolversProps;
context?: (ctx: any) => any;
usePlayground?: boolean;
}
export interface ResolversProps {
Query?: any;
Mutation?: any;
[dynamicProperty: string]: any;
}
export async function applyGraphQL<T>({
Router,
path = "/graphql",
typeDefs,
resolvers,
context,
usePlayground = true,
}: ApplyGraphQLOptions<T>): Promise<T> {
const router = new Router();
const schema = makeExecutableSchema({ typeDefs, resolvers });
await router.post(path, async (ctx: any) => {
const { response, request } = ctx;
if (request.hasBody) {
try {
const contextResult = context ? await context(ctx) : undefined;
const body = await request.body().value;
const result = await (graphql as any)(
schema,
body.query,
resolvers,
contextResult,
body.variables || undefined,
body.operationName || undefined,
);
response.status = 200;
response.body = result;
return;
} catch (error) {
response.status = 200;
response.body = {
data: null,
errors: [{
message: error.message ? error.message : error,
}],
}
return;
}
}
});
await router.get(path, async (ctx: any) => {
const { request, response } = ctx;
if (usePlayground) {
// perform more expensive content-type check only if necessary
// XXX We could potentially move this logic into the GuiOptions lambda,
// but I don't think it needs any overriding
const prefersHTML = request.accepts("text/html");
if (prefersHTML) {
const playground = renderPlaygroundPage({
endpoint: request.url.origin + path,
subscriptionEndpoint: request.url.origin,
});
response.status = 200;
response.body = playground;
return;
}
}
});
// await router.get("/", async (ctx) => {
// const { request, response } = ctx;
// const WS = new SubscriptionServer(
// {
// schema,
// execute: execute as any,
// subscribe,
// onConnect: onConnect
// ? onConnect
// : (connectionParams: Object) => ({ ...connectionParams }),
// onDisconnect: onDisconnect,
// onOperation: async (
// message: { payload: any },
// connection: any,
// ) => {
// connection.formatResponse = (value: any) => value;
// // connection.formatError = this.requestOptions.formatError;
// let contextResult;
// try {
// contextResult = context ? await context(ctx) : undefined;
// } catch (error) {
// response.status = 200;
// response.body = {
// data: null,
// errors: [{
// message: error.message ? error.message : error,
// }],
// }
// }
// return { ...connection, context: contextResult };
// },
// keepAlive: undefined,
// validationRules: undefined
// },
// );
// })
// await router.get("/", async (ctx) => {
// if (ctx.isUpgradable) {
// const socket = await ctx.upgrade();
// socket.send("graphql-ws")
// await chat(socket)
// }
// })
return router;
};