-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
192 lines (160 loc) · 5.46 KB
/
index.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { getObjectValue } from '@graphql-ez/utils/object';
import { LazyPromise } from '@graphql-ez/utils/promise';
import type { FastifyInstance, FastifyPluginCallback, FastifyReply, FastifyRequest, RouteOptions } from 'fastify';
import type { FastifyCorsOptions, FastifyCorsOptionsDelegate, FastifyPluginOptionsDelegate } from 'fastify-cors';
import type { AppOptions, BuildAppOptions, EZAppFactoryType, GetEnvelopedFn, ProcessRequestOptions } from 'graphql-ez';
import {
BaseAppBuilder,
createEZAppFactory,
handleRequest,
InternalAppBuildContextKey,
InternalAppBuildContext,
} from 'graphql-ez';
declare module 'graphql-ez' {
interface BuildContextArgs {
fastify?: {
request: FastifyRequest;
reply: FastifyReply;
};
}
interface InternalAppBuildIntegrationContext {
fastify?: FastifyInstance;
}
}
export interface FastifyAppOptions extends AppOptions {
path?: string;
/**
* Enable or configure CORS
*/
cors?: boolean | FastifyCorsOptions | FastifyPluginOptionsDelegate<FastifyCorsOptionsDelegate>;
/**
* Custom Fastify Route options
*/
routeOptions?: Omit<RouteOptions, 'method' | 'url' | 'handler'>;
/**
* Customize some Helix processRequest options
*/
processRequestOptions?: (req: FastifyRequest, reply: FastifyReply) => ProcessRequestOptions;
}
export type FastifyAppPlugin = FastifyPluginCallback<{}>;
declare module 'fastify' {
interface FastifyRequest {
isMultipart?: true;
}
}
export interface EZApp {
readonly fastifyPlugin: FastifyAppPlugin;
readonly getEnveloped: Promise<GetEnvelopedFn<unknown>>;
readonly path: string;
[InternalAppBuildContextKey]: InternalAppBuildContext;
}
export interface EZAppBuilder extends BaseAppBuilder {
readonly buildApp: (options?: BuildAppOptions) => EZApp;
readonly path: string;
}
export function CreateApp(config: FastifyAppOptions = {}): EZAppBuilder {
const appConfig = { ...config };
const path = (appConfig.path ||= '/graphql');
let ezApp: EZAppFactoryType;
try {
ezApp = createEZAppFactory(
{
integrationName: 'fastify',
},
appConfig
);
} catch (err) {
err instanceof Error && Error.captureStackTrace(err, CreateApp);
throw err;
}
const { appBuilder, onIntegrationRegister, ...commonApp } = ezApp;
const buildApp: EZAppBuilder['buildApp'] = function buildApp(buildOptions = {}) {
const appPromise = LazyPromise(() => {
return appBuilder(buildOptions, ({ ctx, getEnveloped }) => {
const { cors, routeOptions, buildContext, onAppRegister, processRequestOptions } = appConfig;
return async function FastifyPlugin(instance: FastifyInstance) {
const integration = {
fastify: instance,
};
if (onAppRegister) await onAppRegister({ ctx, integration, getEnveloped });
await onIntegrationRegister(integration);
if (cors) {
const fastifyCors = (await import('fastify-cors')).default;
await instance.register(fastifyCors, getObjectValue(cors));
}
const {
preProcessRequest,
options: { customHandleRequest },
} = ctx;
const requestHandler = customHandleRequest || handleRequest;
instance.route({
...routeOptions,
method: ['GET', 'POST'],
url: path,
handler(fastifyReq, reply) {
const request = {
body: fastifyReq.body,
headers: fastifyReq.headers,
method: fastifyReq.method,
query: fastifyReq.query,
};
const req = fastifyReq.raw;
return requestHandler({
req,
request,
getEnveloped,
baseOptions: appConfig,
contextArgs() {
return {
req,
fastify: {
request: fastifyReq,
reply,
},
};
},
buildContext,
onResponse(result) {
for (const { name, value } of result.headers) {
reply.header(name, value);
}
reply.status(result.status);
reply.send(result.payload);
},
onMultiPartResponse(result, defaultHandle) {
reply.hijack();
return defaultHandle(req, reply.raw, result);
},
onPushResponse(result, defaultHandle) {
reply.hijack();
return defaultHandle(req, reply.raw, result);
},
processRequestOptions: processRequestOptions && (() => processRequestOptions(fastifyReq, reply)),
preProcessRequest,
});
},
});
};
});
});
const skipOverride: unique symbol = Symbol.for('skip-override');
const fastifyPlugin: FastifyPluginCallback & { [skipOverride]?: true } = async function fastifyPlugin(
instance: FastifyInstance
) {
await (await appPromise).app(instance);
};
fastifyPlugin[skipOverride] = true;
return {
fastifyPlugin,
getEnveloped: LazyPromise(() => appPromise.then(v => v.getEnveloped)),
path,
[InternalAppBuildContextKey]: ezApp[InternalAppBuildContextKey],
};
};
return {
...commonApp,
path,
buildApp,
};
}
export * from 'graphql-ez';