Skip to content

Commit

Permalink
feat(client-preset): persisted document custom hash function (#9996)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nathan-ahn authored Jun 13, 2024
1 parent 5e594ef commit 99f449c
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 21 deletions.
30 changes: 30 additions & 0 deletions .changeset/spicy-starfishes-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
"@graphql-codegen/client-preset": patch
"website": patch
---

Added configuration to allow for custom hash functions for persisted documents in the client preset

### Example
```ts filename="codegen.ts" {10-12}
import { type CodegenConfig } from '@graphql-codegen/cli'

const config: CodegenConfig = {
schema: 'schema.graphql',
documents: ['src/**/*.tsx'],
generates: {
'./src/gql/': {
preset: 'client',
presetConfig: {
persistedDocuments: {
hashAlgorithm: operation => {
const shasum = crypto.createHash('sha512')
shasum.update(operation)
return shasum.digest('hex')
}
}
}
}
}
}
```
6 changes: 4 additions & 2 deletions packages/presets/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ export type ClientPresetConfig = {
*/
hashPropertyName?: string;
/**
* @description Algorithm used to generate the hash, could be useful if your server expects something specific (e.g., Apollo Server expects `sha256`).
* @description Algorithm or function used to generate the hash, could be useful if your server expects something specific (e.g., Apollo Server expects `sha256`).
*
* 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.
*
* 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
* by the version of OpenSSL on the platform.
*
* @default `sha1`
*/
hashAlgorithm?: 'sha1' | 'sha256' | (string & {});
hashAlgorithm?: 'sha1' | 'sha256' | (string & {}) | ((operation: string) => string);
};
};

Expand Down
12 changes: 9 additions & 3 deletions packages/presets/client/src/persisted-documents.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import * as crypto from 'crypto';
import { printExecutableGraphQLDocument } from '@graphql-tools/documents';
import { type DocumentNode, Kind, visit } from 'graphql';
import * as crypto from 'crypto';
import { Kind, visit, type DocumentNode } from 'graphql';

/**
* This function generates a hash from a document node.
*/
export function generateDocumentHash(operation: string, algorithm: 'sha1' | 'sha256' | (string & {})): string {
export function generateDocumentHash(
operation: string,
algorithm: 'sha1' | 'sha256' | (string & {}) | ((operation: string) => string)
): string {
if (typeof algorithm === 'function') {
return algorithm(operation);
}
const shasum = crypto.createHash(algorithm);
shasum.update(operation);
return shasum.digest('hex');
Expand Down
Loading

0 comments on commit 99f449c

Please sign in to comment.