Skip to content

Commit 33e423f

Browse files
Merge branch 'CarsonF-refactor/schema-generation-hook' into next
2 parents 14a9aee + 2577291 commit 33e423f

File tree

11 files changed

+117
-145
lines changed

11 files changed

+117
-145
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
22
import { loadPackage } from '@nestjs/common/utils/load-package.util';
33
import { ModulesContainer } from '@nestjs/core';
44
import { extend, GraphQLFederationFactory } from '@nestjs/graphql';
5+
import { GraphQLSchema } from 'graphql';
56
import { ApolloDriverConfig } from '../interfaces';
67
import { PluginsExplorerService } from '../services/plugins-explorer.service';
78
import { ApolloBaseDriver } from './apollo-base.driver';
@@ -24,23 +25,19 @@ export class ApolloFederationDriver extends ApolloBaseDriver {
2425
this.pluginsExplorerService.explore(options),
2526
);
2627

27-
const adapterOptions = await this.graphqlFederationFactory.mergeWithSchema(
28-
options,
29-
);
30-
3128
if (options.definitions && options.definitions.path) {
3229
const { printSubgraphSchema } = loadPackage(
3330
'@apollo/subgraph',
3431
'ApolloFederation',
3532
() => require('@apollo/subgraph'),
3633
);
3734
await this.graphQlFactory.generateDefinitions(
38-
printSubgraphSchema(adapterOptions.schema),
35+
printSubgraphSchema(options.schema),
3936
options,
4037
);
4138
}
4239

43-
await super.start(adapterOptions);
40+
await super.start(options);
4441

4542
if (options.installSubscriptionHandlers || options.subscriptions) {
4643
// TL;DR <https://github.com/apollographql/apollo-server/issues/2776>
@@ -49,4 +46,8 @@ export class ApolloFederationDriver extends ApolloBaseDriver {
4946
);
5047
}
5148
}
49+
50+
public generateSchema(options: ApolloDriverConfig): Promise<GraphQLSchema> {
51+
return this.graphqlFederationFactory.generateSchema(options);
52+
}
5253
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,8 @@ export class ApolloGatewayDriver extends ApolloBaseDriver<ApolloGatewayDriverCon
4343
server: await super.mergeDefaultOptions(options?.server ?? {}),
4444
};
4545
}
46+
47+
public generateSchema(_: ApolloGatewayDriverConfig) {
48+
return null;
49+
}
4650
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,12 @@ export class ApolloDriver extends ApolloBaseDriver {
2020
this.pluginsExplorerService = new PluginsExplorerService(modulesContainer);
2121
}
2222

23-
public async start(apolloOptions: ApolloDriverConfig) {
24-
apolloOptions.plugins = extend(
25-
apolloOptions.plugins || [],
26-
this.pluginsExplorerService.explore(apolloOptions),
23+
public async start(options: ApolloDriverConfig) {
24+
options.plugins = extend(
25+
options.plugins || [],
26+
this.pluginsExplorerService.explore(options),
2727
);
2828

29-
const options =
30-
await this.graphQlFactory.mergeWithSchema<ApolloDriverConfig>(
31-
apolloOptions,
32-
);
33-
3429
if (options.definitions && options.definitions.path) {
3530
await this.graphQlFactory.generateDefinitions(
3631
printSchema(options.schema),

packages/apollo/tests/e2e/__snapshots__/serialized-graph.spec.ts.snap

Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,22 +1438,6 @@ exports[`Serialized graph should generate a post-initialization graph and match
14381438
},
14391439
"id": "568827916"
14401440
},
1441-
"609903163": {
1442-
"source": "2071909260",
1443-
"target": "2327576890",
1444-
"metadata": {
1445-
"type": "class-to-class",
1446-
"sourceModuleName": "GraphQLModule",
1447-
"sourceClassName": "GraphQLFederationFactory",
1448-
"targetClassName": "GraphQLSchemaHost",
1449-
"sourceClassToken": "GraphQLFederationFactory",
1450-
"targetClassToken": "GraphQLSchemaHost",
1451-
"targetModuleName": "GraphQLModule",
1452-
"keyOrIndex": 3,
1453-
"injectionType": "constructor"
1454-
},
1455-
"id": "609903163"
1456-
},
14571441
"687726954": {
14581442
"source": "1220772931",
14591443
"target": "2401268572",
@@ -1578,6 +1562,22 @@ exports[`Serialized graph should generate a post-initialization graph and match
15781562
},
15791563
"id": "1131367312"
15801564
},
1565+
"1171497430": {
1566+
"source": "801400998",
1567+
"target": "2327576890",
1568+
"metadata": {
1569+
"type": "class-to-class",
1570+
"sourceModuleName": "GraphQLModule",
1571+
"sourceClassName": "GraphQLModule",
1572+
"targetClassName": "GraphQLSchemaHost",
1573+
"sourceClassToken": "GraphQLModule",
1574+
"targetClassToken": "GraphQLSchemaHost",
1575+
"targetModuleName": "GraphQLModule",
1576+
"keyOrIndex": 4,
1577+
"injectionType": "constructor"
1578+
},
1579+
"id": "1171497430"
1580+
},
15811581
"1224417053": {
15821582
"source": "4011145642",
15831583
"target": "3165047378",
@@ -2075,7 +2075,7 @@ exports[`Serialized graph should generate a post-initialization graph and match
20752075
},
20762076
"id": "2476818369"
20772077
},
2078-
"2479150365": {
2078+
"2483746115": {
20792079
"source": "2071909260",
20802080
"target": "1232825470",
20812081
"metadata": {
@@ -2086,10 +2086,10 @@ exports[`Serialized graph should generate a post-initialization graph and match
20862086
"sourceClassToken": "GraphQLFederationFactory",
20872087
"targetClassToken": "TypeDefsDecoratorFactory",
20882088
"targetModuleName": "GraphQLModule",
2089-
"keyOrIndex": 4,
2089+
"keyOrIndex": 3,
20902090
"injectionType": "constructor"
20912091
},
2092-
"id": "2479150365"
2092+
"id": "2483746115"
20932093
},
20942094
"2517941991": {
20952095
"source": "2071909260",
@@ -2471,22 +2471,6 @@ exports[`Serialized graph should generate a post-initialization graph and match
24712471
},
24722472
"id": "3909875272"
24732473
},
2474-
"3943074983": {
2475-
"source": "1220772931",
2476-
"target": "2327576890",
2477-
"metadata": {
2478-
"type": "class-to-class",
2479-
"sourceModuleName": "GraphQLModule",
2480-
"sourceClassName": "GraphQLFactory",
2481-
"targetClassName": "GraphQLSchemaHost",
2482-
"sourceClassToken": "GraphQLFactory",
2483-
"targetClassToken": "GraphQLSchemaHost",
2484-
"targetModuleName": "GraphQLModule",
2485-
"keyOrIndex": 4,
2486-
"injectionType": "constructor"
2487-
},
2488-
"id": "3943074983"
2489-
},
24902474
"3943511424": {
24912475
"source": "2401268572",
24922476
"target": "764140560",
@@ -2853,6 +2837,22 @@ exports[`Serialized graph should generate a pre-initialization graph and match s
28532837
"source": "1015269748",
28542838
"target": "334507858",
28552839
},
2840+
"1171497430": {
2841+
"id": "1171497430",
2842+
"metadata": {
2843+
"injectionType": "constructor",
2844+
"keyOrIndex": 4,
2845+
"sourceClassName": "GraphQLModule",
2846+
"sourceClassToken": "GraphQLModule",
2847+
"sourceModuleName": "GraphQLModule",
2848+
"targetClassName": "GraphQLSchemaHost",
2849+
"targetClassToken": "GraphQLSchemaHost",
2850+
"targetModuleName": "GraphQLModule",
2851+
"type": "class-to-class",
2852+
},
2853+
"source": "801400998",
2854+
"target": "2327576890",
2855+
},
28562856
"1224417053": {
28572857
"id": "1224417053",
28582858
"metadata": {
@@ -3414,11 +3414,11 @@ exports[`Serialized graph should generate a pre-initialization graph and match s
34143414
"source": "2129265135",
34153415
"target": "1711044370",
34163416
},
3417-
"2479150365": {
3418-
"id": "2479150365",
3417+
"2483746115": {
3418+
"id": "2483746115",
34193419
"metadata": {
34203420
"injectionType": "constructor",
3421-
"keyOrIndex": 4,
3421+
"keyOrIndex": 3,
34223422
"sourceClassName": "GraphQLFederationFactory",
34233423
"sourceClassToken": "GraphQLFederationFactory",
34243424
"sourceModuleName": "GraphQLModule",
@@ -3862,22 +3862,6 @@ exports[`Serialized graph should generate a pre-initialization graph and match s
38623862
"source": "3645104976",
38633863
"target": "3469962808",
38643864
},
3865-
"3943074983": {
3866-
"id": "3943074983",
3867-
"metadata": {
3868-
"injectionType": "constructor",
3869-
"keyOrIndex": 4,
3870-
"sourceClassName": "GraphQLFactory",
3871-
"sourceClassToken": "GraphQLFactory",
3872-
"sourceModuleName": "GraphQLModule",
3873-
"targetClassName": "GraphQLSchemaHost",
3874-
"targetClassToken": "GraphQLSchemaHost",
3875-
"targetModuleName": "GraphQLModule",
3876-
"type": "class-to-class",
3877-
},
3878-
"source": "1220772931",
3879-
"target": "2327576890",
3880-
},
38813865
"3943511424": {
38823866
"id": "3943511424",
38833867
"metadata": {
@@ -4049,22 +4033,6 @@ exports[`Serialized graph should generate a pre-initialization graph and match s
40494033
"source": "2322078987",
40504034
"target": "1711044370",
40514035
},
4052-
"609903163": {
4053-
"id": "609903163",
4054-
"metadata": {
4055-
"injectionType": "constructor",
4056-
"keyOrIndex": 3,
4057-
"sourceClassName": "GraphQLFederationFactory",
4058-
"sourceClassToken": "GraphQLFederationFactory",
4059-
"sourceModuleName": "GraphQLModule",
4060-
"targetClassName": "GraphQLSchemaHost",
4061-
"targetClassToken": "GraphQLSchemaHost",
4062-
"targetModuleName": "GraphQLModule",
4063-
"type": "class-to-class",
4064-
},
4065-
"source": "2071909260",
4066-
"target": "2327576890",
4067-
},
40684036
"687726954": {
40694037
"id": "687726954",
40704038
"metadata": {

packages/graphql/lib/drivers/abstract-graphql.driver.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Inject } from '@nestjs/common';
22
import { ApplicationConfig, HttpAdapterHost } from '@nestjs/core';
3+
import { GraphQLSchema } from 'graphql';
34
import { GraphQLFactory } from '../graphql.factory';
45
import { GqlModuleOptions, GraphQLDriver } from '../interfaces';
56
import { normalizeRoutePath } from '../utils';
@@ -12,7 +13,7 @@ export abstract class AbstractGraphQLDriver<
1213
protected readonly httpAdapterHost: HttpAdapterHost;
1314

1415
@Inject()
15-
protected readonly applicationConfig: ApplicationConfig;
16+
protected readonly applicationConfig?: ApplicationConfig;
1617

1718
@Inject()
1819
protected readonly graphQlFactory: GraphQLFactory;
@@ -36,6 +37,10 @@ export abstract class AbstractGraphQLDriver<
3637
return clonedOptions;
3738
}
3839

40+
public generateSchema(options: TOptions): Promise<GraphQLSchema> | null {
41+
return this.graphQlFactory.generateSchema(options);
42+
}
43+
3944
public subscriptionWithFilter(
4045
instanceRef: unknown,
4146
filterFn: (
@@ -49,7 +54,7 @@ export abstract class AbstractGraphQLDriver<
4954
}
5055

5156
protected getNormalizedPath(options: TOptions): string {
52-
const prefix = this.applicationConfig.getGlobalPrefix();
57+
const prefix = this.applicationConfig?.getGlobalPrefix() ?? '';
5358
const useGlobalPrefix = prefix && options.useGlobalPrefix;
5459
const gqlOptionsPath = normalizeRoutePath(options.path);
5560
return useGlobalPrefix

packages/graphql/lib/federation/graphql-federation.factory.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
import { gql } from 'graphql-tag';
2626
import { forEach, isEmpty } from 'lodash';
2727
import { GraphQLSchemaBuilder } from '../graphql-schema.builder';
28-
import { GraphQLSchemaHost } from '../graphql-schema.host';
2928
import {
3029
AutoSchemaFileValue,
3130
BuildFederatedSchemaOptions,
@@ -46,35 +45,31 @@ export class GraphQLFederationFactory {
4645
private readonly resolversExplorerService: ResolversExplorerService,
4746
private readonly scalarsExplorerService: ScalarsExplorerService,
4847
private readonly gqlSchemaBuilder: GraphQLSchemaBuilder,
49-
private readonly gqlSchemaHost: GraphQLSchemaHost,
5048
private readonly typeDefsDecoratorFactory: TypeDefsDecoratorFactory,
5149
) {}
5250

53-
async mergeWithSchema<T extends GqlModuleOptions>(
51+
async generateSchema<T extends GqlModuleOptions>(
5452
options: T = {} as T,
5553
buildFederatedSchema?: (
5654
options: BuildFederatedSchemaOptions,
5755
) => GraphQLSchema,
58-
): Promise<T> {
59-
const transformSchema = async (schema: GraphQLSchema) =>
60-
options.transformSchema ? options.transformSchema(schema) : schema;
56+
): Promise<GraphQLSchema> {
57+
const transformSchema =
58+
options.transformSchema ?? ((schema: GraphQLSchema) => schema);
6159

6260
let schema: GraphQLSchema;
6361
if (options.autoSchemaFile) {
64-
schema = await this.generateSchema(options, buildFederatedSchema);
62+
schema = await this.generateSchemaFromCodeFirst(
63+
options,
64+
buildFederatedSchema,
65+
);
6566
} else if (isEmpty(options.typeDefs)) {
6667
schema = options.schema;
6768
} else {
6869
schema = this.buildSchemaFromTypeDefs(options);
6970
}
7071

71-
this.gqlSchemaHost.schema = schema;
72-
73-
return {
74-
...options,
75-
schema: await transformSchema(schema),
76-
typeDefs: undefined,
77-
};
72+
return await transformSchema(schema);
7873
}
7974

8075
private buildSchemaFromTypeDefs<T extends GqlModuleOptions>(options: T) {
@@ -93,7 +88,7 @@ export class GraphQLFederationFactory {
9388
]);
9489
}
9590

96-
private async generateSchema<T extends GqlModuleOptions>(
91+
private async generateSchemaFromCodeFirst<T extends GqlModuleOptions>(
9792
options: T,
9893
buildFederatedSchema?: (
9994
options: BuildFederatedSchemaOptions,

0 commit comments

Comments
 (0)