From 2d60dda93c09b0c8d7b69241833174f991d7b450 Mon Sep 17 00:00:00 2001 From: enisdenjo Date: Sat, 31 Oct 2020 20:46:08 +0100 Subject: [PATCH] fix(client): Accept nullish values for `operationName` and `variables` --- src/message.ts | 6 +++++- src/tests/client.ts | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/message.ts b/src/message.ts index b8b45c57..468d1c2d 100644 --- a/src/message.ts +++ b/src/message.ts @@ -101,10 +101,14 @@ export function isMessage(val: unknown): val is Message { hasOwnStringProperty(val, 'id') && hasOwnObjectProperty(val, 'payload') && (!hasOwnProperty(val.payload, 'operationName') || - hasOwnStringProperty(val.payload, 'operationName')) && + val.payload.operationName === undefined || + val.payload.operationName === null || + typeof val.payload.operationName === 'string') && (hasOwnStringProperty(val.payload, 'query') || // string query or persisted query id hasOwnObjectProperty(val.payload, 'query')) && // document node query (!hasOwnProperty(val.payload, 'variables') || + val.payload.variables === undefined || + val.payload.variables === null || hasOwnObjectProperty(val.payload, 'variables')) ); case MessageType.Next: diff --git a/src/tests/client.ts b/src/tests/client.ts index adac6053..d817527a 100644 --- a/src/tests/client.ts +++ b/src/tests/client.ts @@ -170,6 +170,31 @@ describe('query operation', () => { await sub.waitForComplete(); }); + + it('should accept nullish value for `operationName` and `variables`', async () => { + const { url } = await startTServer(); + + const client = createClient({ url }); + + // nothing + await tsubscribe(client, { + query: 'query { getValue }', + }).waitForComplete(); + + // undefined + await tsubscribe(client, { + operationName: undefined, + query: 'query { getValue }', + variables: undefined, + }).waitForComplete(); + + // null + await tsubscribe(client, { + operationName: null, + query: 'query { getValue }', + variables: null, + }).waitForComplete(); + }); }); describe('subscription operation', () => {