Skip to content

Commit

Permalink
Merge pull request #1797 from iMobs/upgrade-apollo-federation-with-su…
Browse files Browse the repository at this point in the history
…bgraph

fix(deps) Upgrade apollo federation with subgraph
  • Loading branch information
kamilmysliwiec authored Oct 20, 2021
2 parents a64645e + 6982a85 commit 0af4f4a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 45 deletions.
27 changes: 16 additions & 11 deletions lib/federation/graphql-federation.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,25 @@ export class GraphQLFederationFactory {
private async generateSchema(
options: GqlModuleOptions,
): Promise<GraphQLSchema> {
const {
buildFederatedSchema,
printSchema,
} = loadPackage('@apollo/federation', 'ApolloFederation', () =>
require('@apollo/federation'),
const { buildFederatedSchema } = loadPackage(
'@apollo/federation',
'ApolloFederation',
() => require('@apollo/federation'),
);

const autoGeneratedSchema: GraphQLSchema = await this.gqlSchemaBuilder.buildFederatedSchema(
options.autoSchemaFile,
options,
this.resolversExplorerService.getAllCtors(),
const { printSubgraphSchema } = loadPackage(
'@apollo/subgraph',
'ApolloFederation',
() => require('@apollo/subgraph'),
);

const autoGeneratedSchema: GraphQLSchema =
await this.gqlSchemaBuilder.buildFederatedSchema(
options.autoSchemaFile,
options,
this.resolversExplorerService.getAllCtors(),
);
let executableSchema: GraphQLSchema = buildFederatedSchema({
typeDefs: gql(printSchema(autoGeneratedSchema)),
typeDefs: gql(printSubgraphSchema(autoGeneratedSchema)),
resolvers: this.getResolvers(options.resolvers),
});

Expand Down
8 changes: 4 additions & 4 deletions lib/federation/graphql-federation.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ export class GraphQLFederationModule implements OnModuleInit, OnModuleDestroy {
if (!this.httpAdapterHost || !this.httpAdapterHost.httpAdapter) {
return;
}
const { printSchema } = loadPackage(
'@apollo/federation',
const { printSubgraphSchema } = loadPackage(
'@apollo/subgraph',
'ApolloFederation',
() => require('@apollo/federation'),
() => require('@apollo/subgraph'),
);

const { typePaths } = this.options;
Expand All @@ -161,7 +161,7 @@ export class GraphQLFederationModule implements OnModuleInit, OnModuleDestroy {

if (this.options.definitions && this.options.definitions.path) {
await this.graphqlFactory.generateDefinitions(
printSchema(apolloOptions.schema),
printSubgraphSchema(apolloOptions.schema),
this.options,
);
}
Expand Down
17 changes: 11 additions & 6 deletions lib/graphql-definitions.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,16 @@ export class GraphQLDefinitionsFactory {
const typePathDefs = await this.gqlTypesLoader.mergeTypesByPaths(typePaths);
const mergedTypeDefs = extend(typePathDefs, typeDefs);

const {
buildFederatedSchema,
printSchema,
} = loadPackage('@apollo/federation', 'ApolloFederation', () =>
require('@apollo/federation'),
const { buildFederatedSchema } = loadPackage(
'@apollo/federation',
'ApolloFederation',
() => require('@apollo/federation'),
);

const { printSubgraphSchema } = loadPackage(
'@apollo/subgraph',
'ApolloFederation',
() => require('@apollo/subgraph'),
);

const schema = buildFederatedSchema([
Expand All @@ -133,7 +138,7 @@ export class GraphQLDefinitionsFactory {
]);
const tsFile = await this.gqlAstExplorer.explore(
gql`
${printSchema(schema)}
${printSubgraphSchema(schema)}
`,
path,
outputAs,
Expand Down
4 changes: 2 additions & 2 deletions lib/graphql-schema.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ export class GraphQLSchemaBuilder {

private loadFederationDirectives() {
const { federationDirectives } = loadPackage(
'@apollo/federation/dist/directives',
'@apollo/subgraph/dist/directives',
'SchemaBuilder',
() => require('@apollo/federation/dist/directives'),
() => require('@apollo/subgraph/dist/directives'),
);
return federationDirectives;
}
Expand Down
13 changes: 1 addition & 12 deletions lib/utils/transform-schema.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The changed lines are 31-40 and 85-87 and the original file can be found here:
// https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo-graphql/src/schema/transformSchema.ts

import { GraphQLReferenceResolver } from '@apollo/federation/dist/types';
import '@apollo/subgraph/dist/schemaExtensions';

This comment has been minimized.

Copy link
@timofei-iatsenko

timofei-iatsenko Oct 20, 2021

Contributor

@kamilmysliwiec this change make @apollo/subgraph required package even if you are not using Apollo Federation

 FAIL  src/entities/place/__tests__/google-place.model.spec.ts
  ● Test suite failed to run

    Cannot find module '@apollo/subgraph/dist/schemaExtensions' from '../node_modules/@nestjs/graphql/dist/utils/transform-schema.util.js'

    Require stack:
      /Users/tim/projects/timescenery/projects/backend/node_modules/@nestjs/graphql/dist/utils/transform-schema.util.js
      /Users/tim/projects/timescenery/projects/backend/node_modules/@nestjs/graphql/dist/federation/graphql-federation.factory.js
      /Users/tim/projects/timescenery/projects/backend/node_modules/@nestjs/graphql/dist/federation/index.js
      /Users/tim/projects/timescenery/projects/backend/node_modules/@nestjs/graphql/dist/index.js

This is a breaking change

This comment has been minimized.

Copy link
@kamilmysliwiec

kamilmysliwiec Oct 21, 2021

Author Member

Great catch, thank you @thekip. Fixed in the latest version

import {
GraphQLFieldConfigArgumentMap,
GraphQLFieldConfigMap,
Expand All @@ -27,17 +27,6 @@ import {
isUnionType,
} from 'graphql';

// Definitions taken from here: https://github.com/apollographql/apollo-server/blob/main/packages/apollo-federation/src/types.ts#L62
declare module 'graphql/type/definition' {
interface GraphQLObjectType {
resolveReference?: GraphQLReferenceResolver<any>;
}

interface GraphQLObjectTypeConfig<TSource, TContext> {
resolveReference?: GraphQLReferenceResolver<TContext>;
}
}

type TypeTransformer = (
type: GraphQLNamedType,
) => GraphQLNamedType | null | undefined;
Expand Down
47 changes: 39 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
"release": "release-it"
},
"devDependencies": {
"@apollo/federation": "0.33.0",
"@apollo/federation": "^0.33.3",
"@apollo/gateway": "0.42.0",
"@apollo/subgraph": "^0.1.2",
"@commitlint/cli": "13.2.1",
"@commitlint/config-angular": "13.2.0",
"@nestjs/common": "8.1.1",
Expand Down Expand Up @@ -80,8 +81,9 @@
"ws": "8.2.3"
},
"peerDependencies": {
"@apollo/federation": "^0.26.0 || ^0.27.0 || ^0.29.0 || 0.33.0",
"@apollo/federation": "^0.26.0 || ^0.27.0 || ^0.29.0 || ^0.33.0",
"@apollo/gateway": "^0.29.0 || ^0.32.0 || ^0.33.0 || ^0.35.0 || ^0.38.0 || ^0.42.0",
"@apollo/subgraph": "^0.1.2",
"@nestjs/common": "^8.0.0",
"@nestjs/core": "^8.0.0",
"apollo-server-core": "^3.0.0",
Expand All @@ -98,6 +100,9 @@
"@apollo/gateway": {
"optional": true
},
"@apollo/subgraph": {
"optional": true
},
"apollo-server-core": {
"optional": true
},
Expand Down

0 comments on commit 0af4f4a

Please sign in to comment.