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

Federation: Insert empty Subscription type definition #4148

Closed
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,78 @@ describe('composeServices', () => {
`);
});

it('allows extension of the Subscription type with no base type definition', () => {
const serviceA = {
typeDefs: gql`
extend type Subscription {
postAdded: Post
}

type Post {
author: String!
comment: String!
}
`,
name: 'serviceA',
};

const serviceB = {
typeDefs: gql`
extend type Subscription {
postRemoved: ID
}
`,
name: 'serviceB',
};

const { schema, errors } = composeServices([serviceA, serviceB]);
expect(errors).toHaveLength(0);
expect(schema).toBeDefined();

expect(schema.getType('Subscription')).toMatchInlineSnapshot(`
type Subscription {
postAdded: Post
postRemoved: ID
}
`);
});

it('treats root Subscriptions type definition as an extension, not base definitions', () => {
const serviceA = {
typeDefs: gql`
type Subscription {
postAdded: Post
}

type Post {
author: String!
comment: String!
}
`,
name: 'serviceA',
};

const serviceB = {
typeDefs: gql`
extend type Subscription {
postRemoved: ID
}
`,
name: 'serviceB',
};

const { schema, errors } = composeServices([serviceA, serviceB]);
expect(errors).toHaveLength(0);
expect(schema).toBeDefined();

expect(schema.getType('Subscription')).toMatchInlineSnapshot(`
type Subscription {
postAdded: Post
postRemoved: ID
}
`);
});

// TODO: not sure what to do here. Haven't looked into it yet :)
it.skip('works with custom root types', () => {});
});
Expand Down
8 changes: 8 additions & 0 deletions packages/apollo-federation/src/composition/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const EmptyMutationDefinition = {
fields: [],
serviceName: null,
};
const EmptySubscriptionDefinition = {
kind: Kind.OBJECT_TYPE_DEFINITION,
name: { kind: Kind.NAME, value: 'Subscription' },
fields: [],
serviceName: null,
};

// Map of all type definitions to eventually be passed to extendSchema
interface TypeDefinitionsMap {
Expand Down Expand Up @@ -320,6 +326,8 @@ export function buildMapsFromServiceList(serviceList: ServiceDefinition[]) {
typeDefinitionsMap.Query = [EmptyQueryDefinition];
if (typeExtensionsMap.Mutation && !typeDefinitionsMap.Mutation)
typeDefinitionsMap.Mutation = [EmptyMutationDefinition];
if (typeExtensionsMap.Subscription && !typeDefinitionsMap.Subscription)
typeDefinitionsMap.Subscription = [EmptySubscriptionDefinition];

return {
typeToServiceMap,
Expand Down