Skip to content

Add subscription support to @apollo/subgraph #2388

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

Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/spotty-hornets-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@apollo/subgraph": patch
---

Correctly attach provided subscription resolvers to the schema object

32 changes: 32 additions & 0 deletions subgraph-js/src/__tests__/buildSubgraphSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,38 @@ describe('buildSubgraphSchema', () => {
expect(() => buildSubgraphSchema(doc)).not.toThrow();
});

it('correctly attaches the provided subscribe function to the schema object', () => {
async function* subscribeFn () {
for await (const word of ['Hello', 'Bonjour', 'Ciao']) {
yield word;
}
}
const schema = buildSubgraphSchema([
{
typeDefs: gql`
type Query {
hello: String!
}

type Subscription {
hello: String!
}
`,
resolvers: {
Subscription: {
hello: {
subscribe: subscribeFn,
},
},
},
},
]);

expect(schema.getSubscriptionType()?.getFields()['hello'].subscribe).toBe(
subscribeFn,
);
});

// Those tests ensures that we expand older federation specification to their proper definitions,
// so they explicitely link to older spec and should not be changed.
describe('federation specification backward compatibility', () => {
Expand Down
1 change: 1 addition & 0 deletions subgraph-js/src/schema-helper/buildSchemaFromSDL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ export function addResolversToSchema(
field.resolve = fieldConfig;
} else {
field.resolve = fieldConfig.resolve;
field.subscribe = fieldConfig.subscribe;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion subgraph-js/src/schema-helper/resolverMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export interface GraphQLResolverMap<TContext = {}> {
| GraphQLFieldResolver<any, TContext>
| {
requires?: string;
resolve: GraphQLFieldResolver<any, TContext>;
resolve?: GraphQLFieldResolver<any, TContext>;
subscribe?: GraphQLFieldResolver<any, TContext>;
};
}
| GraphQLScalarType
Expand Down