Skip to content

Commit a34d3cf

Browse files
[client-preset] Make persisted docs default hashing algorithm sha256, and follow recommended format (#10652)
* Update default hash to be sha256 for persisted documents * Add changeset * chore(dependencies): updated changesets for modified dependencies * Update tests * chore(dependencies): updated changesets for modified dependencies --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 9090a84 commit a34d3cf

9 files changed

Lines changed: 41 additions & 31 deletions

File tree

.changeset/sad-rules-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/client-preset': major
3+
---
4+
5+
BREAKING CHANGES: The default hashing algorithm is now sha256 instead of sha1. Generated sha256 format also follows the standard outlined in https://github.com/graphql/graphql-over-http/blob/52d56fb36d51c17e08a920510a23bdc2f6a720be/spec/Appendix%20A%20--%20Persisted%20Documents.md#sha256-hex-document-identifier

examples/persisted-documents-string-mode/src/gql/graphql.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ export const HelloQueryDocument = new TypedDocumentString(
3333
hello
3434
}
3535
`,
36-
{ hash: '86f01e23de1c770cabbc35b2d87f2e5fd7557b6f' }
36+
{ hash: 'sha256:4c3f5d98b02279859b4c0c4efdba9553ac7acf89b9b0785eb24be68d5a67e6e8' }
3737
) as unknown as TypedDocumentString<HelloQueryQuery, HelloQueryQueryVariables>;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"86f01e23de1c770cabbc35b2d87f2e5fd7557b6f": "query HelloQuery { hello }"
2+
"sha256:4c3f5d98b02279859b4c0c4efdba9553ac7acf89b9b0785eb24be68d5a67e6e8": "query HelloQuery { hello }"
33
}

examples/persisted-documents/src/gql/graphql.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type HelloQueryQueryVariables = Exact<{ [key: string]: never }>;
99
export type HelloQueryQuery = { hello: string };
1010

1111
export const HelloQueryDocument = {
12-
__meta__: { hash: '86f01e23de1c770cabbc35b2d87f2e5fd7557b6f' },
12+
__meta__: { hash: 'sha256:4c3f5d98b02279859b4c0c4efdba9553ac7acf89b9b0785eb24be68d5a67e6e8' },
1313
kind: 'Document',
1414
definitions: [
1515
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"86f01e23de1c770cabbc35b2d87f2e5fd7557b6f": "query HelloQuery { hello }"
2+
"sha256:4c3f5d98b02279859b4c0c4efdba9553ac7acf89b9b0785eb24be68d5a67e6e8": "query HelloQuery { hello }"
33
}

packages/presets/client/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export type ClientPresetConfig = {
9090
* 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
9191
* by the version of OpenSSL on the platform.
9292
*
93-
* @default `sha1`
93+
* @default `sha256`
9494
*/
9595
hashAlgorithm?: 'sha1' | 'sha256' | (string & {}) | ((operation: string) => string);
9696
};
@@ -164,7 +164,7 @@ export const preset: Types.OutputPreset<ClientPresetConfig> = {
164164
hashAlgorithm:
165165
(typeof options.presetConfig.persistedDocuments === 'object' &&
166166
options.presetConfig.persistedDocuments.hashAlgorithm) ||
167-
'sha1',
167+
'sha256',
168168
}
169169
: null;
170170

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const CONNECTION_DIRECTIVE_NAME = 'connection';
77

88
/**
99
* This function generates a hash from a document node.
10+
* When `sha256` algorithm is used, the hash should be prefixed with `sha256:`
11+
* https://github.com/graphql/graphql-over-http/blob/52d56fb36d51c17e08a920510a23bdc2f6a720be/spec/Appendix%20A%20--%20Persisted%20Documents.md#sha256-hex-document-identifier
1012
*/
1113
export function generateDocumentHash(
1214
operation: string,
@@ -17,7 +19,10 @@ export function generateDocumentHash(
1719
}
1820
const shasum = crypto.createHash(algorithm);
1921
shasum.update(operation);
20-
return shasum.digest('hex');
22+
23+
const algorithmPrefix = algorithm === 'sha256' ? 'sha256:' : '';
24+
25+
return algorithmPrefix + shasum.digest('hex');
2126
}
2227

2328
/**

packages/presets/client/tests/client-preset.spec.ts

Lines changed: 22 additions & 22 deletions
Large diffs are not rendered by default.

website/src/pages/plugins/presets/preset-client.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ console.log(await response.json())
496496

497497
### Hashing algorithm
498498

499-
To override the default hash algorithm of sha1 set `persistedDocuments.hashAlgorithm`
499+
To override the default hash algorithm of sha256 set `persistedDocuments.hashAlgorithm`
500500

501501
```ts filename="codegen.ts" {10-12}
502502
import { type CodegenConfig } from '@graphql-codegen/cli'
@@ -509,7 +509,7 @@ const config: CodegenConfig = {
509509
preset: 'client',
510510
presetConfig: {
511511
persistedDocuments: {
512-
hashAlgorithm: 'sha256'
512+
hashAlgorithm: 'sha1'
513513
}
514514
}
515515
}

0 commit comments

Comments
 (0)