From b27c39442a54a336591d538cb3f6c3f89d1fb8e0 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Thu, 3 Jun 2021 13:49:52 +0100 Subject: [PATCH] (core) - Fix empty and no variables resulting in differing keys (#1695) --- .changeset/two-beers-exist.md | 5 +++++ packages/core/src/utils/request.test.ts | 5 +++++ packages/core/src/utils/request.ts | 7 +++---- 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 .changeset/two-beers-exist.md diff --git a/.changeset/two-beers-exist.md b/.changeset/two-beers-exist.md new file mode 100644 index 0000000000..76010754fb --- /dev/null +++ b/.changeset/two-beers-exist.md @@ -0,0 +1,5 @@ +--- +'@urql/core': patch +--- + +Treat empty variables the same as no variables in `@urql/core`'s `createRequest`. diff --git a/packages/core/src/utils/request.test.ts b/packages/core/src/utils/request.test.ts index 34400ad35f..3225a5323a 100644 --- a/packages/core/src/utils/request.test.ts +++ b/packages/core/src/utils/request.test.ts @@ -2,6 +2,11 @@ import { parse, print } from 'graphql'; import { gql } from '../gql'; import { createRequest } from './request'; +jest.mock('./hash', () => ({ + hash: jest.requireActual('./hash').hash, + phash: (x: number) => x, +})); + it('should hash identical queries identically', () => { const reqA = createRequest('{ test }'); const reqB = createRequest('{ test }'); diff --git a/packages/core/src/utils/request.ts b/packages/core/src/utils/request.ts index f490549434..7b85daf79f 100644 --- a/packages/core/src/utils/request.ts +++ b/packages/core/src/utils/request.ts @@ -78,13 +78,12 @@ export const createRequest = ( q: string | DocumentNode | TypedDocumentNode, vars?: Variables ): GraphQLRequest => { + if (!vars) vars = {} as Variables; const query = keyDocument(q); return { - key: vars - ? phash(query.__key, stringifyVariables(vars)) >>> 0 - : query.__key, + key: phash(query.__key, stringifyVariables(vars)) >>> 0, query, - variables: vars || ({} as Variables), + variables: vars, }; };