Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@nestjs/graphql) fix federated schema generated with code first approach #1780

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/graphql-definitions.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { isEmpty } from '@nestjs/common/utils/shared.utils';
import { gql } from 'apollo-server-core';
import * as chokidar from 'chokidar';
import { printSchema } from 'graphql';
import { printSchema } from '@apollo/federation';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apollo/federation is an optional dependency so we'd have to make it required

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GraphQLModule is a wrapper around Apollo server, so it could make sense to have only the @apollo/federation as a required dependency since it provides the Apollo Federation subgraph specifications (e.g. directives declaration).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem here is that you can use @nestjs/graphql in the scenario where graph is not federated and will never be, thus dependency is optional and not required for such case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that @kamilmysliwiec is referring to the fact that @apollo/federation is an optional peer dependency:

"optional": true
and it is lazy loaded:

You can use the printSchema() function of @apollo/federation also in the scenario where the graph is not federated since Apollo has forked the code from graphql-js (as reported in the source code) and patched the function to enable also the federation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The GraphQLModule is a wrapper around Apollo server, so it could make sense to have only the @apollo/federation as a required dependency since it provides the Apollo Federation subgraph specifications (e.g. directives declaration).

We can't make @apollo/federation required as it's not needed for regular GraphQL apps. I'd say we should use the original printSchema for non-federated graphs and the printSchema from @apollo/federation (lazy loaded) for Federation-based apps

import {
DefinitionsGeneratorOptions,
GraphQLAstExplorer,
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql-schema.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { isString } from '@nestjs/common/utils/shared.utils';
import {
GraphQLSchema,
lexicographicSortSchema,
printSchema,
specifiedDirectives,
} from 'graphql';
import { printSchema } from '@apollo/federation';
import { resolve } from 'path';
import { GRAPHQL_SDL_FILE_HEADER } from './graphql.constants';
import { GqlModuleOptions } from './interfaces';
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
GraphQLSchema,
GraphQLSchemaConfig,
lexicographicSortSchema,
printSchema,
} from 'graphql';
import { printSchema } from '@apollo/federation';
import { forEach, isEmpty } from 'lodash';
import {
DefinitionsGeneratorOptions,
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { ApplicationConfig, HttpAdapterHost } from '@nestjs/core';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { ApolloServerBase } from 'apollo-server-core';
import { printSchema } from 'graphql';
import { printSchema } from '@apollo/federation';
import { GraphQLAstExplorer } from './graphql-ast.explorer';
import { GraphQLSchemaBuilder } from './graphql-schema.builder';
import { GraphQLSchemaHost } from './graphql-schema.host';
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/code-first-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
GraphQLSchema,
IntrospectionField,
IntrospectionSchema,
printSchema,
TypeKind
} from 'graphql';
import { printSchema } from '@apollo/federation';
import { GRAPHQL_SDL_FILE_HEADER } from '../../lib/graphql.constants';
import {
GraphQLSchemaBuilderModule,
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/graphql-sort-auto-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { printSchema, GraphQLSchema } from 'graphql';
import { GraphQLSchema } from 'graphql';
import { printSchema } from '@apollo/federation';
import { FileSystemHelper } from '../../lib/schema-builder/helpers/file-system.helper';
import { sortedPrintedSchemaSnapshot } from '../utils/printed-schema.snapshot';
import { GRAPHQL_SDL_FILE_HEADER } from '../../lib/graphql.constants';
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/graphql-sort-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { printSchema, GraphQLSchema } from 'graphql';
import { GraphQLSchema } from 'graphql';
import { printSchema } from '@apollo/federation';
import { SortSchemaModule } from '../graphql/sort-schema.module';
import { GRAPHQL_SDL_FILE_HEADER } from '../../lib/graphql.constants';
import { GraphQLSchemaHost } from '../../lib';
Expand Down