Skip to content

Commit 1e099fa

Browse files
feat(apollo): update drivers, remove deprecated options, fix runners
1 parent d97e653 commit 1e099fa

File tree

8 files changed

+87
-109
lines changed

8 files changed

+87
-109
lines changed

packages/apollo/lib/drivers/apollo-base.driver.ts

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1+
import { ApolloServer, type BaseContext } from '@apollo/server';
2+
import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground';
3+
import { ApolloServerErrorCode } from '@apollo/server/errors';
4+
import { expressMiddleware } from '@apollo/server/express4';
5+
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled';
6+
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
7+
import { HttpStatus } from '@nestjs/common';
18
import { loadPackage } from '@nestjs/common/utils/load-package.util';
29
import { isFunction } from '@nestjs/common/utils/shared.utils';
310
import { AbstractGraphQLDriver } from '@nestjs/graphql';
4-
511
import { GraphQLError, GraphQLFormattedError } from 'graphql';
612
import * as omit from 'lodash.omit';
713
import { ApolloDriverConfig } from '../interfaces';
814
import { createAsyncIterator } from '../utils/async-iterator.util';
915

10-
import { ApolloServer, type BaseContext } from '@apollo/server';
11-
import { ApolloServerErrorCode } from '@apollo/server/errors';
12-
import { expressMiddleware } from '@apollo/server/express4';
13-
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';
14-
import * as express from 'express';
15-
import * as http from 'node:http';
16-
17-
import { fastifyApolloHandler } from '@as-integrations/fastify';
18-
import { HttpStatus } from '@nestjs/common';
19-
2016
const apolloPredefinedExceptions: Partial<Record<HttpStatus, string>> = {
2117
[HttpStatus.BAD_REQUEST]: ApolloServerErrorCode.BAD_USER_INPUT,
2218
[HttpStatus.UNAUTHORIZED]: 'UNAUTHENTICATED',
@@ -65,7 +61,9 @@ export abstract class ApolloBaseDriver<
6561
typeof options.playground === 'object' ? options.playground : undefined;
6662
defaults = {
6763
...defaults,
68-
plugins: [ApolloServerPluginLandingPageLocalDefault(playgroundOptions)],
64+
plugins: [
65+
ApolloServerPluginLandingPageGraphQLPlayground(playgroundOptions),
66+
],
6967
};
7068
} else if (
7169
(options.playground === undefined &&
@@ -74,7 +72,7 @@ export abstract class ApolloBaseDriver<
7472
) {
7573
defaults = {
7674
...defaults,
77-
plugins: [ApolloServerPluginLandingPageLocalDefault()],
75+
plugins: [ApolloServerPluginLandingPageDisabled()],
7876
};
7977
}
8078

@@ -109,66 +107,63 @@ export abstract class ApolloBaseDriver<
109107
);
110108
}
111109

112-
protected async registerExpress(options: T, hooks?: any) {
113-
if (hooks?.preStartHook) {
114-
hooks?.preStartHook();
115-
}
116-
117-
const cors = loadPackage('cors', null, () => require('cors'));
118-
110+
protected async registerExpress(
111+
options: T,
112+
{ preStartHook }: { preStartHook?: () => void } = {},
113+
) {
119114
const { path, typeDefs, resolvers, schema } = options;
120115

121116
const httpAdapter = this.httpAdapterHost.httpAdapter;
122117
const app = httpAdapter.getInstance();
123-
const httpServer = http.createServer(app);
118+
const drainHttpServerPlugin = ApolloServerPluginDrainHttpServer({
119+
httpServer: httpAdapter.getHttpServer(),
120+
});
121+
122+
preStartHook?.();
124123

125-
// Set up Apollo Server
126124
const server = new ApolloServer({
127125
typeDefs,
128126
resolvers,
129127
schema,
130128
...options,
131-
/**
132-
* @TODO
133-
* should remove serverWillStart from default plugins.
134-
* after include plugins here
135-
*/
136-
// TODO: fix - dont override plugins
137-
// plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
129+
plugins: options.plugins
130+
? options.plugins.concat([drainHttpServerPlugin])
131+
: [drainHttpServerPlugin],
138132
});
139133

140134
await server.start();
141135

142-
app.use(
143-
path,
144-
cors(options.cors),
145-
express.json(),
146-
expressMiddleware(server),
147-
);
136+
app.use(path, expressMiddleware(server));
148137

149138
this.apolloServer = server;
150139
}
151140

152-
protected async registerFastify(options: T, hooks?: any) {
153-
if (hooks?.preStartHook) {
154-
hooks?.preStartHook();
155-
}
156-
157-
const cors = loadPackage('@fastify/cors', null, () =>
158-
require('@fastify/cors'),
141+
protected async registerFastify(
142+
options: T,
143+
{ preStartHook }: { preStartHook?: () => void } = {},
144+
) {
145+
const { fastifyApolloDrainPlugin, fastifyApolloHandler } = loadPackage(
146+
'@as-integrations/fastify',
147+
'GraphQLModule',
148+
() => require('@as-integrations/fastify'),
159149
);
160150

161151
const httpAdapter = this.httpAdapterHost.httpAdapter;
162152
const app = httpAdapter.getInstance();
163153

164154
const { path, typeDefs, resolvers, schema } = options;
155+
const apolloDrainPlugin = fastifyApolloDrainPlugin(app);
156+
157+
preStartHook?.();
158+
165159
const server = new ApolloServer<BaseContext>({
166160
typeDefs,
167161
resolvers,
168162
schema,
169163
...options,
170-
// TODO: fix - dont override plugin
171-
//plugins: [fastifyApolloDrainPlugin(app)],
164+
plugins: options.plugins
165+
? options.plugins.concat([apolloDrainPlugin])
166+
: [apolloDrainPlugin],
172167
});
173168

174169
await server.start();
@@ -179,8 +174,6 @@ export abstract class ApolloBaseDriver<
179174
handler: fastifyApolloHandler(server),
180175
});
181176

182-
await app.register(cors, options.cors);
183-
184177
this.apolloServer = server;
185178
}
186179

@@ -219,13 +212,15 @@ export abstract class ApolloBaseDriver<
219212
},
220213
});
221214
} else {
222-
error = new GraphQLError(exceptionRef.message, httpStatus?.toString());
215+
error = new GraphQLError(exceptionRef.message, {
216+
extensions: {
217+
code: ApolloServerErrorCode.INTERNAL_SERVER_ERROR,
218+
status: httpStatus,
219+
},
220+
});
223221
}
224222

225223
error.stack = exceptionRef?.stacktrace;
226-
//TODO: we need to verify if previous behavior is to be kept
227-
// if so we must open a PR on Apollo to include response inside the raised exception
228-
//https://github.com/apollographql/apollo-server/blob/e6d0d6d9cbd78d4914adf2abb04d84710991849a/packages/server/src/errorNormalize.ts#L58
229224
error.extensions['response'] = exceptionRef?.response;
230225
return error;
231226
};

packages/apollo/lib/drivers/apollo-federation.driver.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export class ApolloFederationDriver extends ApolloBaseDriver {
2727
const adapterOptions = await this.graphqlFederationFactory.mergeWithSchema(
2828
options,
2929
);
30-
await this.runExecutorFactoryIfPresent(adapterOptions);
3130

3231
if (options.definitions && options.definitions.path) {
3332
const { printSubgraphSchema } = loadPackage(
@@ -50,12 +49,4 @@ export class ApolloFederationDriver extends ApolloBaseDriver {
5049
);
5150
}
5251
}
53-
54-
private async runExecutorFactoryIfPresent(apolloOptions: ApolloDriverConfig) {
55-
if (!apolloOptions.executorFactory) {
56-
return;
57-
}
58-
const executor = await apolloOptions.executorFactory(apolloOptions.schema);
59-
apolloOptions.executor = executor;
60-
}
6152
}

packages/apollo/lib/interfaces/apollo-driver-config.interface.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,26 @@
1+
import { ApolloServerOptionsWithTypeDefs } from '@apollo/server';
2+
import { ApolloServerPluginLandingPageGraphQLPlaygroundOptions } from '@apollo/server-plugin-landing-page-graphql-playground';
13
import {
24
GqlModuleAsyncOptions,
35
GqlModuleOptions,
46
GqlOptionsFactory,
57
SubscriptionConfig,
68
} from '@nestjs/graphql';
7-
import { GraphQLSchema } from 'graphql';
8-
import { CorsOptions } from 'cors';
99

1010
export interface ServerRegistration {
1111
/**
1212
* Path to mount GraphQL API
1313
*/
1414
path?: string;
15-
16-
/**
17-
* CORS configuration
18-
*/
19-
cors?: CorsOptions;
20-
21-
/**
22-
* Body-parser configuration
23-
*/
24-
bodyParserConfig?: any | boolean;
25-
26-
/**
27-
* On health check hook
28-
*/
29-
onHealthCheck?: (req: any) => Promise<any>;
30-
31-
/**
32-
* Whether to enable health check
33-
*/
34-
disableHealthCheck?: boolean;
3515
}
3616

3717
export interface ApolloDriverConfig
38-
extends Omit</*Config*/ any, 'typeDefs'>,
18+
extends Omit<
19+
ApolloServerOptionsWithTypeDefs<any>,
20+
'typeDefs' | 'schema' | 'resolvers' | 'gateway'
21+
>,
3922
ServerRegistration,
40-
Omit<GqlModuleOptions, 'context'> {
41-
/**
42-
* Executor factory function
43-
*/
44-
executorFactory?: (
45-
schema: GraphQLSchema,
46-
) => /*GraphQLExecutor | Promise<GraphQLExecutor>*/ any;
47-
23+
GqlModuleOptions {
4824
/**
4925
* If enabled, "subscriptions-transport-ws" will be automatically registered.
5026
*/
@@ -58,7 +34,7 @@ export interface ApolloDriverConfig
5834
/**
5935
* GraphQL playground options.
6036
*/
61-
playground?: boolean;
37+
playground?: boolean | ApolloServerPluginLandingPageGraphQLPlaygroundOptions;
6238

6339
/**
6440
* If enabled, will register a global interceptor that automatically maps

packages/apollo/package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"dependencies": {
3939
"@apollo/server": "4.3.2",
40+
"@apollo/server-plugin-landing-page-graphql-playground": "4.0.0",
4041
"iterall": "1.3.0",
4142
"lodash.omit": "4.5.0",
4243
"tslib": "2.5.0"
@@ -56,12 +57,6 @@
5657
},
5758
"@as-integrations/fastify": {
5859
"optional": true
59-
},
60-
"@fastify/cors": {
61-
"optional": true
62-
},
63-
"cors": {
64-
"optional": true
6560
}
6661
}
6762
}

packages/graphql/lib/exceptions/graphql-exception.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/graphql/lib/exceptions/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/graphql/lib/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ export * from './graphql-ast.explorer';
55
export * from './graphql-definitions.factory';
66
export * from './graphql-schema.host';
77
export * from './graphql-types.loader';
8+
export * from './graphql.constants';
89
export * from './graphql.factory';
910
export * from './graphql.module';
10-
export * from './graphql.constants';
1111
export * from './interfaces';
1212
export * from './scalars';
1313
export * from './schema-builder';
@@ -20,4 +20,3 @@ export * from './type-factories';
2020
export * from './type-helpers';
2121
export * from './utils/extend.util';
2222
export * from './utils/transform-schema.util';
23-
export * from './exceptions';

yarn.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@
115115
"@apollo/utils.keyvaluecache" "^2.1.0"
116116
"@apollo/utils.logger" "^2.0.0"
117117

118+
119+
version "4.0.0"
120+
resolved "https://registry.yarnpkg.com/@apollo/server-plugin-landing-page-graphql-playground/-/server-plugin-landing-page-graphql-playground-4.0.0.tgz#eff593de6c37a0b63d740f1c6498d69f67644aed"
121+
integrity sha512-PBDtKI/chJ+hHeoJUUH9Kuqu58txQl00vUGuxqiC9XcReulIg7RjsyD0G1u3drX4V709bxkL5S0nTeXfRHD0qA==
122+
dependencies:
123+
"@apollographql/graphql-playground-html" "1.6.29"
124+
118125
119126
version "4.1.0"
120127
resolved "https://registry.yarnpkg.com/@apollo/server-plugin-response-cache/-/server-plugin-response-cache-4.1.0.tgz#2c6752bec3bb14d2901688ae631220fd37b938a2"
@@ -283,6 +290,13 @@
283290
resolved "https://registry.yarnpkg.com/@apollo/utils.withrequired/-/utils.withrequired-2.0.0.tgz#f39340f7b621b214d7a063b8da3eab24a9b7b7e8"
284291
integrity sha512-+djpTu6AEE/A1etryZs9tmXRyDY6XXGe3G29MS/LB09uHq3pcl3n4Q5lvDTL5JWKuJixrulg5djePLDAooG8dQ==
285292

293+
"@apollographql/[email protected]":
294+
version "1.6.29"
295+
resolved "https://registry.yarnpkg.com/@apollographql/graphql-playground-html/-/graphql-playground-html-1.6.29.tgz#a7a646614a255f62e10dcf64a7f68ead41dec453"
296+
integrity sha512-xCcXpoz52rI4ksJSdOCxeOCn2DLocxwHf9dVT/Q90Pte1LX+LY+91SFtJF3KXVHH8kEin+g1KKCQPKBjZJfWNA==
297+
dependencies:
298+
xss "^1.0.8"
299+
286300
"@as-integrations/[email protected]":
287301
version "1.3.0"
288302
resolved "https://registry.yarnpkg.com/@as-integrations/fastify/-/fastify-1.3.0.tgz#fc987fffabc8f34ee301eb66f2a7da2e63621004"
@@ -3941,6 +3955,11 @@ combined-stream@^1.0.8:
39413955
dependencies:
39423956
delayed-stream "~1.0.0"
39433957

3958+
commander@^2.20.3:
3959+
version "2.20.3"
3960+
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
3961+
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==
3962+
39443963
commander@^9.4.1:
39453964
version "9.5.0"
39463965
resolved "https://registry.yarnpkg.com/commander/-/commander-9.5.0.tgz#bc08d1eb5cedf7ccb797a96199d41c7bc3e60d30"
@@ -4209,6 +4228,11 @@ crypto-random-string@^4.0.0:
42094228
dependencies:
42104229
type-fest "^1.0.1"
42114230

4231+
4232+
version "0.0.10"
4233+
resolved "https://registry.yarnpkg.com/cssfilter/-/cssfilter-0.0.10.tgz#c6d2672632a2e5c83e013e6864a42ce8defd20ae"
4234+
integrity sha512-FAaLDaplstoRsDR8XGYH51znUN0UY7nMc6Z9/fvE8EXGwvJE9hu7W2vHwx1+bd6gCYnln9nLbzxFTrcO9YQDZw==
4235+
42124236
dargs@^7.0.0:
42134237
version "7.0.0"
42144238
resolved "https://registry.yarnpkg.com/dargs/-/dargs-7.0.0.tgz#04015c41de0bcb69ec84050f3d9be0caf8d6d5cc"
@@ -10615,6 +10639,14 @@ [email protected]:
1061510639
resolved "https://registry.yarnpkg.com/xregexp/-/xregexp-2.0.0.tgz#52a63e56ca0b84a7f3a5f3d61872f126ad7a5943"
1061610640
integrity sha512-xl/50/Cf32VsGq/1R8jJE5ajH1yMCQkpmoS10QbFZWl2Oor4H0Me64Pu2yxvsRWK3m6soJbmGfzSR7BYmDcWAA==
1061710641

10642+
xss@^1.0.8:
10643+
version "1.0.14"
10644+
resolved "https://registry.yarnpkg.com/xss/-/xss-1.0.14.tgz#4f3efbde75ad0d82e9921cc3c95e6590dd336694"
10645+
integrity sha512-og7TEJhXvn1a7kzZGQ7ETjdQVS2UfZyTlsEdDOqvQF7GoxNfY+0YLCzBy1kPdsDDx4QuNAonQPddpsn6Xl/7sw==
10646+
dependencies:
10647+
commander "^2.20.3"
10648+
cssfilter "0.0.10"
10649+
1061810650
xtend@^4.0.0, xtend@^4.0.2, xtend@~4.0.1:
1061910651
version "4.0.2"
1062010652
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"

0 commit comments

Comments
 (0)