Skip to content

Commit

Permalink
Merge pull request #2353 from katainaka0503/fix-duplicate-query-in-fe…
Browse files Browse the repository at this point in the history
…derated-query

fix(schema-first): merge duplicate IQuery interface
  • Loading branch information
kamilmysliwiec committed Sep 1, 2022
2 parents f681209 + 6a215f6 commit ca8cabb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/apollo/tests/e2e/generated-definitions.spec.ts
Expand Up @@ -339,4 +339,23 @@ describe('Generated Definitions', () => {
afterEach(async () => {
await app.close();
});

it('should generate for a federated graph with partial query definition', async () => {
const outputFile = generatedDefinitions(
'federation-partial-query.test-definitions.ts',
);
const factory = new GraphQLFederationDefinitionsFactory();
await factory.generate({
typePaths: [generatedDefinitions('federation-partial-query.graphql')],
path: outputFile,
outputAs: 'interface',
});

expect(
await readFile(
generatedDefinitions('federation-partial-query.fixture.ts'),
'utf8',
),
).toBe(await readFile(outputFile, 'utf8'));
});
});
@@ -0,0 +1,15 @@

/*
* -------------------------------------------------------
* THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY)
* -------------------------------------------------------
*/

/* tslint:disable */
/* eslint-disable */

export interface IQuery {
foo(): Nullable<boolean> | Promise<Nullable<boolean>>;
}

type Nullable<T> = T | null;
@@ -0,0 +1,3 @@
extend type Query {
foo: Boolean
}
Expand Up @@ -3,6 +3,7 @@ import { gql } from 'graphql-tag';
import { DefinitionsGeneratorOptions } from '../graphql-ast.explorer';
import { GraphQLDefinitionsFactory } from '../graphql-definitions.factory';
import { extend } from '../utils';
import { mergeTypeDefs } from '@graphql-tools/merge';

export class GraphQLFederationDefinitionsFactory extends GraphQLDefinitionsFactory {
protected async exploreAndEmit(
Expand Down Expand Up @@ -35,9 +36,20 @@ export class GraphQLFederationDefinitionsFactory extends GraphQLDefinitionsFacto
resolvers: {},
},
]);

// "buildSubgraphSchema" generates an empty Query definition if there is the `extend type Query` statement
// This leads to duplicated IQuery interfaces
// see: https://github.com//issues/2344
const mergedDefinition = mergeTypeDefs([printSubgraphSchema(schema)], {
useSchemaDefinition: false,
throwOnConflict: true,
commentDescriptions: true,
reverseDirectives: true,
});

const tsFile = await this.gqlAstExplorer.explore(
gql`
${printSubgraphSchema(schema)}
${mergedDefinition}
`,
path,
outputAs,
Expand Down

0 comments on commit ca8cabb

Please sign in to comment.