Skip to content

Commit 5b4f36a

Browse files
authored
Filter operationType in payload of GraphQLWsLink (#12950)
1 parent e93fbf6 commit 5b4f36a

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

.changeset/healthy-jokes-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Don't send `operationType` in the payload sent by `GraphQLWsLink`.

src/link/subscriptions/__tests__/graphqlWsLink.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import type { Observable } from "rxjs";
66

77
import { CombinedGraphQLErrors } from "@apollo/client/errors";
88
import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
9-
import { executeWithDefaultContext as execute } from "@apollo/client/testing/internal";
9+
import {
10+
executeWithDefaultContext as execute,
11+
ObservableStream,
12+
} from "@apollo/client/testing/internal";
1013

1114
const query = gql`
1215
query SampleQuery {
@@ -175,3 +178,31 @@ describe("GraphQLWSlink", () => {
175178
});
176179
});
177180
});
181+
182+
// https://github.com/apollographql/apollo-client/issues/12946
183+
test("sends only known keys to the GraphQLWsLink", async () => {
184+
const knownKeys = [
185+
"query",
186+
"variables",
187+
"operationName",
188+
"extensions",
189+
].sort();
190+
191+
type SubscribeFn = Client["subscribe"];
192+
const subscribe = jest.fn<ReturnType<SubscribeFn>, Parameters<SubscribeFn>>(
193+
(_payload, sink) => {
194+
sink.complete();
195+
return () => {};
196+
}
197+
);
198+
const client = mockClient(subscribe);
199+
const link = new GraphQLWsLink(client);
200+
201+
const stream = new ObservableStream(execute(link, { query: subscription }));
202+
await stream.takeComplete();
203+
204+
expect(subscribe).toHaveBeenCalledTimes(1);
205+
206+
const payload = subscribe.mock.calls[0][0];
207+
expect(Object.keys(payload).sort()).toStrictEqual(knownKeys);
208+
});

src/link/subscriptions/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ export class GraphQLWsLink extends ApolloLink {
7979
operation: ApolloLink.Operation
8080
): Observable<ApolloLink.Result> {
8181
return new Observable((observer) => {
82+
const { query, variables, operationName, extensions } = operation;
8283
return this.client.subscribe<ApolloLink.Result>(
83-
{ ...operation, query: print(operation.query) },
84+
{ variables, operationName, extensions, query: print(query) },
8485
{
8586
next: observer.next.bind(observer),
8687
complete: observer.complete.bind(observer),

0 commit comments

Comments
 (0)