diff --git a/subgraph-js/CHANGELOG.md b/subgraph-js/CHANGELOG.md index 95eb3a5de9..baae3c7608 100644 --- a/subgraph-js/CHANGELOG.md +++ b/subgraph-js/CHANGELOG.md @@ -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). diff --git a/subgraph-js/src/__tests__/buildSubgraphSchema.test.ts b/subgraph-js/src/__tests__/buildSubgraphSchema.test.ts index 3c6adc7455..4605bcb997 100644 --- a/subgraph-js/src/__tests__/buildSubgraphSchema.test.ts +++ b/subgraph-js/src/__tests__/buildSubgraphSchema.test.ts @@ -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'; @@ -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(` diff --git a/subgraph-js/src/buildSubgraphSchema.ts b/subgraph-js/src/buildSubgraphSchema.ts index 639436f489..01f371ffc1 100644 --- a/subgraph-js/src/buildSubgraphSchema.ts +++ b/subgraph-js/src/buildSubgraphSchema.ts @@ -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;