Skip to content

Commit db90d54

Browse files
Add __resolveType to _Entity union (#1773)
1 parent ce09189 commit db90d54

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

subgraph-js/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
This CHANGELOG pertains only to Apollo Federation packages in the 2.x range. The Federation v0.x equivalent for this package can be found [here](https://github.com/apollographql/federation/blob/version-0.x/subgraph-js/CHANGELOG.md) on the `version-0.x` branch of this repo.
44

5+
- Add __resolveType to _Entity union [PR #1773](https://github.com/apollographql/federation/pull/1773)
6+
57
## v2.0.1
68

79
- Released in sync with other federation packages but no changes to this package.

subgraph-js/src/__tests__/buildSubgraphSchema.test.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import gql from 'graphql-tag';
2-
import { Kind, graphql, DocumentNode, execute, type DefinitionNode, OperationTypeNode } from 'graphql';
2+
import {
3+
Kind,
4+
graphql,
5+
DocumentNode,
6+
execute,
7+
type DefinitionNode,
8+
OperationTypeNode,
9+
GraphQLUnionType,
10+
} from 'graphql';
311
import { buildSubgraphSchema } from '../buildSubgraphSchema';
412
import { typeSerializer } from 'apollo-federation-integration-testsuite';
513
import { errorCauses } from '@apollo/federation-internals';
@@ -1046,6 +1054,15 @@ type Product {
10461054
`union _Entity = Product`,
10471055
);
10481056
});
1057+
1058+
it('defines the `resolveType` resolver on the `_Entity` union', async () => {
1059+
const schema = buildSubgraphSchema({ typeDefs });
1060+
1061+
expect(
1062+
(schema.getType('_Entity') as GraphQLUnionType).resolveType,
1063+
).toBeDefined();
1064+
});
1065+
10491066
it('allows legacy schema module interface as a simple array of documents', async () => {
10501067
const schema = buildSubgraphSchema({ typeDefs });
10511068
expect(schema.getType('Product')).toMatchInlineSnapshot(`

subgraph-js/src/buildSubgraphSchema.ts

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ export function buildSubgraphSchema(
7676
addResolversToSchema(schema, {
7777
[queryRootName] : {
7878
_entities: (_source, { representations }, context, info) => entitiesResolver({ representations, context, info }),
79+
},
80+
_Entity: {
81+
__resolveType(parent: { __typename: string }) {
82+
return parent.__typename;
83+
},
7984
}
8085
});
8186
}

0 commit comments

Comments
 (0)