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 diff --git a/packages/core/src/utils/request.test.ts b/packages/core/src/utils/request.test.ts index 82e87f7440..2e196c1599 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 = ` + { #query + # 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..b1813b9313 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); @@ -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;