Skip to content

Commit 99f449c

Browse files
authored
feat(client-preset): persisted document custom hash function (#9996)
* Custom hash function * Moving hash function to existing hashAlgorithm field * Docs for hash algorithm function + test to match example in docs * [preset/client] Adding changeset for custom hash function * Added configuration example to changeset
1 parent 5e594ef commit 99f449c

File tree

5 files changed

+342
-21
lines changed

5 files changed

+342
-21
lines changed

.changeset/spicy-starfishes-press.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
"@graphql-codegen/client-preset": patch
3+
"website": patch
4+
---
5+
6+
Added configuration to allow for custom hash functions for persisted documents in the client preset
7+
8+
### Example
9+
```ts filename="codegen.ts" {10-12}
10+
import { type CodegenConfig } from '@graphql-codegen/cli'
11+
12+
const config: CodegenConfig = {
13+
schema: 'schema.graphql',
14+
documents: ['src/**/*.tsx'],
15+
generates: {
16+
'./src/gql/': {
17+
preset: 'client',
18+
presetConfig: {
19+
persistedDocuments: {
20+
hashAlgorithm: operation => {
21+
const shasum = crypto.createHash('sha512')
22+
shasum.update(operation)
23+
return shasum.digest('hex')
24+
}
25+
}
26+
}
27+
}
28+
}
29+
}
30+
```

packages/presets/client/src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,16 @@ export type ClientPresetConfig = {
8484
*/
8585
hashPropertyName?: string;
8686
/**
87-
* @description Algorithm used to generate the hash, could be useful if your server expects something specific (e.g., Apollo Server expects `sha256`).
87+
* @description Algorithm or function used to generate the hash, could be useful if your server expects something specific (e.g., Apollo Server expects `sha256`).
88+
*
89+
* A custom hash function can be provided to generate the hash if the preset algorithms don't fit your use case. The function receives the operation and should return the hash string.
8890
*
8991
* The algorithm parameter is typed with known algorithms and as a string rather than a union because it solely depends on Crypto's algorithms supported
9092
* by the version of OpenSSL on the platform.
9193
*
9294
* @default `sha1`
9395
*/
94-
hashAlgorithm?: 'sha1' | 'sha256' | (string & {});
96+
hashAlgorithm?: 'sha1' | 'sha256' | (string & {}) | ((operation: string) => string);
9597
};
9698
};
9799

packages/presets/client/src/persisted-documents.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import * as crypto from 'crypto';
21
import { printExecutableGraphQLDocument } from '@graphql-tools/documents';
3-
import { type DocumentNode, Kind, visit } from 'graphql';
2+
import * as crypto from 'crypto';
3+
import { Kind, visit, type DocumentNode } from 'graphql';
44

55
/**
66
* This function generates a hash from a document node.
77
*/
8-
export function generateDocumentHash(operation: string, algorithm: 'sha1' | 'sha256' | (string & {})): string {
8+
export function generateDocumentHash(
9+
operation: string,
10+
algorithm: 'sha1' | 'sha256' | (string & {}) | ((operation: string) => string)
11+
): string {
12+
if (typeof algorithm === 'function') {
13+
return algorithm(operation);
14+
}
915
const shasum = crypto.createHash(algorithm);
1016
shasum.update(operation);
1117
return shasum.digest('hex');

0 commit comments

Comments
 (0)