Skip to content

Commit

Permalink
(core) - Fix empty and no variables resulting in differing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Jun 3, 2021
1 parent f5e0b88 commit e1f5a8d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-beers-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Treat empty variables the same as no variables in `@urql/core`'s `createRequest`.
5 changes: 5 additions & 0 deletions packages/core/src/utils/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }');
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ export const createRequest = <Data = any, Variables = object>(
q: string | DocumentNode | TypedDocumentNode<Data, Variables>,
vars?: Variables
): GraphQLRequest<Data, Variables> => {
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,
};
};

Expand Down

0 comments on commit e1f5a8d

Please sign in to comment.