Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/healthy-jokes-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Don't send `operationType` in the payload sent by `GraphQLWsLink`.
33 changes: 32 additions & 1 deletion src/link/subscriptions/__tests__/graphqlWsLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import type { Observable } from "rxjs";

import { CombinedGraphQLErrors } from "@apollo/client/errors";
import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
import { executeWithDefaultContext as execute } from "@apollo/client/testing/internal";
import {
executeWithDefaultContext as execute,
ObservableStream,
} from "@apollo/client/testing/internal";

const query = gql`
query SampleQuery {
Expand Down Expand Up @@ -175,3 +178,31 @@ describe("GraphQLWSlink", () => {
});
});
});

// https://github.com/apollographql/apollo-client/issues/12946
test("sends only known keys to the GraphQLWsLink", async () => {
const knownKeys = [
"query",
"variables",
"operationName",
"extensions",
].sort();

type SubscribeFn = Client["subscribe"];
const subscribe = jest.fn<ReturnType<SubscribeFn>, Parameters<SubscribeFn>>(
(_payload, sink) => {
sink.complete();
return () => {};
}
);
const client = mockClient(subscribe);
const link = new GraphQLWsLink(client);

const stream = new ObservableStream(execute(link, { query: subscription }));
await stream.takeComplete();

expect(subscribe).toHaveBeenCalledTimes(1);

const payload = subscribe.mock.calls[0][0];
expect(Object.keys(payload).sort()).toStrictEqual(knownKeys);
});
3 changes: 2 additions & 1 deletion src/link/subscriptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ export class GraphQLWsLink extends ApolloLink {
operation: ApolloLink.Operation
): Observable<ApolloLink.Result> {
return new Observable((observer) => {
const { operationType, ...request } = operation;
return this.client.subscribe<ApolloLink.Result>(
{ ...operation, query: print(operation.query) },
{ ...request, query: print(operation.query) },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think - should we just be a bit more specific here and only pass in known properties?

      const { query, variables, operationName, extensions} = operation;
      return this.client.subscribe<ApolloLink.Result>(
        { variables, operationName, extensions, query: print(query) },

{
next: observer.next.bind(observer),
complete: observer.complete.bind(observer),
Expand Down