Skip to content

Commit

Permalink
Add __resolveType to _Entity union (#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-watson authored and trevor-scheer committed Apr 25, 2022
1 parent 9d7837d commit 3f68f75
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion subgraph-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This CHANGELOG pertains only to Apollo Federation packages in the `0.x` range. T

> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the appropriate changes within that release will be moved into the new section.
- _Nothing yet! Stay tuned._
- Add __resolveType to _Entity union [PR #1778](https://github.com/apollographql/federation/pull/1778)

## v0.4.0
- Support for Node 17 [PR #1648](https://github.com/apollographql/federation/pull/1648).
Expand Down
17 changes: 16 additions & 1 deletion subgraph-js/src/__tests__/buildSubgraphSchema.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import gql from 'graphql-tag';
import { Kind, graphql, DocumentNode, execute } from 'graphql';
import {
Kind,
graphql,
DocumentNode,
execute,
GraphQLUnionType,
} from 'graphql';
import { buildSubgraphSchema } from '../buildSubgraphSchema';
import { typeSerializer } from 'apollo-federation-integration-testsuite';

Expand Down Expand Up @@ -625,6 +631,15 @@ type Product {
`union _Entity = Product`,
);
});

it('defines the `resolveType` resolver on the `_Entity` union', async () => {
const schema = buildSubgraphSchema({ typeDefs });

expect(
(schema.getType('_Entity') as GraphQLUnionType).resolveType,
).toBeDefined();
});

it('allows legacy schema module interface as a simple array of documents', async () => {
const schema = buildSubgraphSchema({ typeDefs });
expect(schema.getType('Product')).toMatchInlineSnapshot(`
Expand Down
3 changes: 3 additions & 0 deletions subgraph-js/src/buildSubgraphSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ export function buildSubgraphSchema(
return new GraphQLUnionType({
...EntityType.toConfig(),
types: entityTypes.filter(isObjectType),
resolveType(parent: { __typename: string }) {
return parent.__typename;
},
});
}
return undefined;
Expand Down

0 comments on commit 3f68f75

Please sign in to comment.