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

Add __resolveType to _Entity union #1773

Merged
merged 6 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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: 2 additions & 0 deletions subgraph-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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.

- Add __resolveType to _Entity union
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved

## v2.0.1

- Released in sync with other federation packages but no changes to this package.
Expand Down
19 changes: 18 additions & 1 deletion subgraph-js/src/__tests__/buildSubgraphSchema.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import gql from 'graphql-tag';
import { Kind, graphql, DocumentNode, execute, type DefinitionNode, OperationTypeNode } from 'graphql';
import {
Kind,
graphql,
DocumentNode,
execute,
type DefinitionNode,
OperationTypeNode,
GraphQLUnionType,
} from 'graphql';
import { buildSubgraphSchema } from '../buildSubgraphSchema';
import { typeSerializer } from 'apollo-federation-integration-testsuite';
import { errorCauses } from '@apollo/federation-internals';
Expand Down Expand Up @@ -1046,6 +1054,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
5 changes: 5 additions & 0 deletions subgraph-js/src/buildSubgraphSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export function buildSubgraphSchema(
addResolversToSchema(schema, {
[queryRootName] : {
_entities: (_source, { representations }, context, info) => entitiesResolver({ representations, context, info }),
},
_Entity: {
__resolveType(parent: { __typename: string }) {
return parent.__typename;
},
}
});
}
Expand Down