From b602ec837ebba3bad9637e24a677c1910e1fafed Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Fri, 17 Jul 2020 12:36:46 +0200 Subject: [PATCH 1/4] remove comments and whitespace from our own queries --- packages/core/src/utils/request.test.ts | 14 ++++++++++++++ packages/core/src/utils/request.ts | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/core/src/utils/request.test.ts b/packages/core/src/utils/request.test.ts index 82e87f7440..f7c8ef9687 100644 --- a/packages/core/src/utils/request.test.ts +++ b/packages/core/src/utils/request.test.ts @@ -73,3 +73,17 @@ it('should return a valid query object with variables', () => { variables: { test: 5 }, }); }); + +it('should remove comments', () => { + const doc = ` + { + # broken + test + } + `; + const val = createRequest(doc); + expect(print(val.query)).toBe(`{ + test +} +`); +}); diff --git a/packages/core/src/utils/request.ts b/packages/core/src/utils/request.ts index 1eaa1ded4b..d74a528734 100644 --- a/packages/core/src/utils/request.ts +++ b/packages/core/src/utils/request.ts @@ -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); From 63c00149216bc1a8b78f7fd50889441fee88b3e7 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Fri, 17 Jul 2020 12:37:39 +0200 Subject: [PATCH 2/4] add changeset --- .changeset/hip-adults-look.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hip-adults-look.md diff --git a/.changeset/hip-adults-look.md b/.changeset/hip-adults-look.md new file mode 100644 index 0000000000..7c7314e32f --- /dev/null +++ b/.changeset/hip-adults-look.md @@ -0,0 +1,5 @@ +--- +'@urql/core': patch +--- + +Remove whitespace and comments from string-queries From 5dd37067d3dc88f52d6b0e32b2e25f060c9e7705 Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Sat, 18 Jul 2020 10:53:54 +0200 Subject: [PATCH 3/4] add comment after opening bracket in test --- packages/core/src/utils/request.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/utils/request.test.ts b/packages/core/src/utils/request.test.ts index f7c8ef9687..2e196c1599 100644 --- a/packages/core/src/utils/request.test.ts +++ b/packages/core/src/utils/request.test.ts @@ -76,7 +76,7 @@ it('should return a valid query object with variables', () => { it('should remove comments', () => { const doc = ` - { + { #query # broken test } From 9521137e024d42349c6c872b179e2d50b7450983 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Tue, 21 Jul 2020 15:10:25 +0100 Subject: [PATCH 4/4] Remove location data from parsed queries --- packages/core/src/utils/request.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/core/src/utils/request.ts b/packages/core/src/utils/request.ts index d74a528734..b1813b9313 100644 --- a/packages/core/src/utils/request.ts +++ b/packages/core/src/utils/request.ts @@ -20,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;