Skip to content

Commit

Permalink
(core) - Shorten queries for hashing (#911)
Browse files Browse the repository at this point in the history
* remove comments and whitespace from our own queries

* add changeset

* add comment after opening bracket in test

* Remove location data from parsed queries

Co-authored-by: Phil Pluckthun <[email protected]>
  • Loading branch information
JoviDeCroock and kitten authored Jul 21, 2020
1 parent ac64214 commit 873ba9d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-adults-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/core': patch
---

Remove whitespace and comments from string-queries
14 changes: 14 additions & 0 deletions packages/core/src/utils/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,17 @@ it('should return a valid query object with variables', () => {
variables: { test: 5 },
});
});

it('should remove comments', () => {
const doc = `
{ #query
# broken
test
}
`;
const val = createRequest(doc);
expect(print(val.query)).toBe(`{
test
}
`);
});
6 changes: 4 additions & 2 deletions packages/core/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ interface Documents {
[key: number]: DocumentNode;
}

const hashQuery = (q: string): number => hash(q.replace(/[\s,]+/g, ' ').trim());
const hashQuery = (q: string): number =>
hash(q.replace(/([\s,]|#[^\n\r]+)+/g, ' ').trim());

const docs: Documents = Object.create(null);

Expand All @@ -19,7 +20,8 @@ export const createRequest = (
let query: DocumentNode;
if (typeof q === 'string') {
key = hashQuery(q);
query = docs[key] !== undefined ? docs[key] : parse(q);
query =
docs[key] !== undefined ? docs[key] : parse(q, { noLocation: true });
} else if ((q as any).__key !== undefined) {
key = (q as any).__key;
query = q;
Expand Down

0 comments on commit 873ba9d

Please sign in to comment.