Skip to content

Persisted document custom hash function #9996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading