Skip to content
This repository was archived by the owner on Jun 29, 2021. It is now read-only.

Commit fad6a45

Browse files
committed
Only minify source once when generating ID
1 parent d50b4da commit fad6a45

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

packages/graphql-mini-transforms/src/document.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,14 @@ import {DocumentNode, SimpleDocument} from 'graphql-typed';
1616
const IMPORT_REGEX = /^#import\s+['"]([^'"]*)['"];?[\s\n]*/gm;
1717
const DEFAULT_NAME = 'Operation';
1818

19-
function defaultGenerateId(normalizedSource: string) {
20-
// This ID is a hash of the full file contents that are part of the document,
21-
// including other documents that are injected in, but excluding any unused
22-
// fragments. This is useful for things like persisted queries.
23-
return createHash('sha256')
24-
.update(minifySource(normalizedSource))
25-
.digest('hex');
26-
}
27-
2819
export interface CleanDocumentOptions {
2920
removeUnused?: boolean;
3021
generateId?: (normalizedSource: string) => string;
3122
}
3223

3324
export function cleanDocument(
3425
document: UntypedDocumentNode,
35-
{
36-
removeUnused = true,
37-
generateId = defaultGenerateId,
38-
}: CleanDocumentOptions = {},
26+
{removeUnused = true, generateId}: CleanDocumentOptions = {},
3927
): DocumentNode<any, any, any> {
4028
if (removeUnused) {
4129
removeUnusedDefinitions(document);
@@ -53,7 +41,18 @@ export function cleanDocument(
5341
stripLoc(definition);
5442
}
5543

56-
const id = generateId(documentSource);
44+
let id: string;
45+
46+
if (generateId === undefined) {
47+
// This ID is a hash of the full file contents that are part of the document,
48+
// including other documents that are injected in, but excluding any unused
49+
// fragments. This is useful for things like persisted queries.
50+
id = createHash('sha256')
51+
.update(minifySource(normalizedSource))
52+
.digest('hex');
53+
} else {
54+
id = generateId(documentSource);
55+
}
5756

5857
Reflect.defineProperty(normalizedDocument, 'id', {
5958
value: id,

0 commit comments

Comments
 (0)