Skip to content

Commit

Permalink
fix: also send operationName to server (#67)
Browse files Browse the repository at this point in the history
* fix: also send operationName to server

* add changeset
  • Loading branch information
n1ru4l authored Sep 7, 2020
1 parent dd99745 commit bb822cd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/cool-bananas-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@n1ru4l/socket-io-graphql-client": minor
"@n1ru4l/socket-io-graphql-server": minor
---

The client now also sends the operationName to the server if provided. The `operationName` is now also optional.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Observable<T> = {

type Parameter = {
operation: string;
operationName: string;
operationName?: string | null;
variables?: { [key: string]: any };
};

Expand Down Expand Up @@ -57,7 +57,7 @@ export const createSocketIOGraphQLClient = (
};

const onReconnect = () => {
Array.from(operations.values()).forEach(record => {
Array.from(operations.values()).forEach((record) => {
record.execute();
});
};
Expand All @@ -70,7 +70,7 @@ export const createSocketIOGraphQLClient = (
socket.off("reconnect", onReconnect);
};

const execute = ({ operation, variables }: Parameter) => {
const execute = ({ operation, variables, operationName }: Parameter) => {
const operationId = currentOperationId;
currentOperationId = currentOperationId + 1;

Expand All @@ -88,11 +88,12 @@ export const createSocketIOGraphQLClient = (
execute: () => {
socket.emit("@graphql/execute", {
id: operationId,
operationName,
operation,
variables
variables,
});
},
sink
sink,
};

operations.set(operationId, record);
Expand All @@ -102,16 +103,16 @@ export const createSocketIOGraphQLClient = (
unsubscribe: () => {
operations.delete(operationId);
socket.emit("@graphql/unsubscribe", {
id: operationId
id: operationId,
});
}
},
};
}
},
} as Observable<any>;
};

return {
execute,
destroy
destroy,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const decodeMessage = (message: unknown): MessagePayload | Error => {
}
const maybeOperationName: unknown = (message as any).operationName ?? null;
if (maybeOperationName === null || typeof maybeOperationName === "string") {
operationName = maybeOperationName ?? null;
operationName = maybeOperationName;
} else {
return new Error(
"Invalid message format. Field 'operationName' is invalid."
Expand Down

0 comments on commit bb822cd

Please sign in to comment.