Skip to content

Commit 6c360b9

Browse files
committed
feat: allow to use alias directives for import in federation 2
1 parent 68405e8 commit 6c360b9

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { GraphQLSchemaBuilder } from '../graphql-schema.builder';
2828
import { GraphQLSchemaHost } from '../graphql-schema.host';
2929
import { GqlModuleOptions, BuildFederatedSchemaOptions } from '../interfaces';
3030
import { ResolversExplorerService, ScalarsExplorerService } from '../services';
31-
import { extend } from '../utils';
31+
import { extend, stringifyWithoutQuotes } from '../utils';
3232
import { transformSchema } from '../utils/transform-schema.util';
3333

3434
@Injectable()
@@ -122,10 +122,13 @@ export class GraphQLFederationFactory {
122122
'@override',
123123
'@requires',
124124
],
125-
url = 'https://specs.apollo.dev/federation/v2.0',
125+
importUrl = 'https://specs.apollo.dev/federation/v2.0',
126126
} = typeof options.useFed2 === 'boolean' ? {} : options.useFed2;
127127
const mappedDirectives = directives
128128
.map((directive) => {
129+
if (!isString(directive)) {
130+
return stringifyWithoutQuotes(directive);
131+
}
129132
let finalDirective = directive;
130133
if (!directive.startsWith('@')) {
131134
finalDirective = `@${directive}`;
@@ -135,7 +138,7 @@ export class GraphQLFederationFactory {
135138
.join(', ');
136139

137140
typeDefs = `
138-
extend schema @link(url: "${url}", import: [${mappedDirectives}])
141+
extend schema @link(url: "${importUrl}", import: [${mappedDirectives}])
139142
${typeDefs}
140143
`;
141144
} else if (options.useFed2 && !isApolloSubgraph2) {

packages/graphql/lib/interfaces/gql-module-options.interface.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ import { BuildSchemaOptions } from './build-schema-options.interface';
88

99
export type Enhancer = 'guards' | 'interceptors' | 'filters';
1010

11+
export interface AliasDirectiveImport {
12+
name: string;
13+
as: string;
14+
}
15+
1116
export interface Federation2Config {
1217
/**
1318
* The imported directives
1419
* @default ['@key', '@shareable', '@external', '@override', '@requires']
1520
*/
16-
directives?: string[];
21+
directives?: (string | AliasDirectiveImport)[];
1722
/**
1823
* The import link
1924
* @default 'https://specs.apollo.dev/federation/v2.0'
2025
*/
21-
url?: string;
26+
importUrl?: string;
2227
}
2328

2429
/**
@@ -51,7 +56,9 @@ export interface GqlModuleOptions<TDriver extends GraphQLDriver = any> {
5156
include?: Function[];
5257

5358
/**
54-
* Defines if wants to federation 2
59+
* If enabled, it will use federation 2 schema
60+
*
61+
* **Note:** You need to have installed @apollo/subgraph@^2.0.0 and enable `autoSchemaFile`
5562
*
5663
* This will add to your schema:
5764
* ```graphql

0 commit comments

Comments
 (0)