Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(core): Use documentId for operation hash if it's available (#3575)

authored by

Phil Pluckthun and committed by
GitHub
1c35bcfb a4ea19c7

+29 -5
+5
.changeset/forty-walls-hug.md
··· 1 + --- 2 + '@urql/core': patch 3 + --- 4 + 5 + Use `documentId` from persisted documents for document keys, when it's available.
+10
packages/core/src/utils/request.test.ts
··· 81 81 variables: { test: 5 }, 82 82 }); 83 83 }); 84 + 85 + it('should hash persisted documents consistently', () => { 86 + const doc = parse('{ testG }'); 87 + const docPersisted = parse('{ testG }'); 88 + (docPersisted as any).documentId = 'testG'; 89 + 90 + const req = createRequest(doc, undefined); 91 + const reqPersisted = createRequest(docPersisted, undefined); 92 + expect(req.key).not.toBe(reqPersisted.key); 93 + }); 84 94 }); 85 95 86 96 describe('stringifyDocument ', () => {
+14 -5
packages/core/src/utils/request.ts
··· 12 12 RequestExtensions, 13 13 } from '../types'; 14 14 15 + type PersistedDocumentNode = TypedDocumentNode & { 16 + documentId?: string; 17 + }; 18 + 15 19 /** A `DocumentNode` annotated with its hashed key. 16 20 * @internal 17 21 */ ··· 90 94 const hashDocument = ( 91 95 node: string | DefinitionNode | DocumentNode 92 96 ): HashValue => { 93 - let key = phash(stringifyDocument(node)); 94 - // Add the operation name to the produced hash 95 - if ((node as DocumentNode).definitions) { 96 - const operationName = getOperationName(node as DocumentNode); 97 - if (operationName) key = phash(`\n# ${operationName}`, key); 97 + let key: HashValue; 98 + if ((node as PersistedDocumentNode).documentId) { 99 + key = phash((node as PersistedDocumentNode).documentId!); 100 + } else { 101 + key = phash(stringifyDocument(node)); 102 + // Add the operation name to the produced hash 103 + if ((node as DocumentNode).definitions) { 104 + const operationName = getOperationName(node as DocumentNode); 105 + if (operationName) key = phash(`\n# ${operationName}`, key); 106 + } 98 107 } 99 108 return key; 100 109 };