diff --git a/packages/core/src/utils/request.test.ts b/packages/core/src/utils/request.test.ts index f3b0511808..adc9224907 100644 --- a/packages/core/src/utils/request.test.ts +++ b/packages/core/src/utils/request.test.ts @@ -92,7 +92,7 @@ describe('stringifyDocument ', () => { expect(stringifyDocument(formatted)).toBe(print(formatted)); }); - it('should reprint gql documents', () => { + it('should reprint request documents', () => { const request = createRequest(`query { test { field } }`, {}); const formatted = formatDocument(request.query); expect(print(formatted)).toMatchInlineSnapshot(` @@ -106,6 +106,29 @@ describe('stringifyDocument ', () => { expect(stringifyDocument(formatted)).toBe(print(formatted)); }); + it.only('should reprint gql documents', () => { + const request = createRequest( + gql` + query { + test { + field + } + } + `, + {} + ); + const formatted = formatDocument(request.query); + expect(print(formatted)).toMatchInlineSnapshot(` + "{ + test { + field + __typename + } + }" + `); + expect(stringifyDocument(formatted)).toBe(print(formatted)); + }); + it('should remove comments', () => { const doc = ` { #query diff --git a/packages/core/src/utils/typenames.ts b/packages/core/src/utils/typenames.ts index b6bda51721..d0a1bd7299 100755 --- a/packages/core/src/utils/typenames.ts +++ b/packages/core/src/utils/typenames.ts @@ -4,6 +4,7 @@ import { InlineFragmentNode, Kind, visit, + print, } from 'graphql'; import { KeyedDocumentNode, keyDocument } from './request'; @@ -83,6 +84,11 @@ export const formatDocument = (node: T): T => { }); formattedDocs.set(query.__key, result); + if (typeof node !== 'string' && node.loc?.source.name === 'gql') { + const printed = print(result); + node.loc.source.body = printed; + (node.loc as any).end = printed.length; + } } return (result as unknown) as T;