dotansimha / dotansimha/graphql-code-generator

hashAlgorithm function wrong operation string

Open
#10,200 1 comment 0 reactions 0 assignees View on GitHub
waiting-for-answer
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Which packages are impacted by your issue?

@graphql-codegen/client-preset

### Describe the bug

When using `hashAlgorithm: (operation) => {}` in `persistedDocuments` config, i got string without `\n` and `\t`.

However, they actually generated with `\t` and `\n` in `gql.ts`. That produces bug, when hash of query is wrong, and my backend validating that.

### Your Example Website or App

There is not

### Steps to Reproduce the Bug or Issue

Is it neccecary?

### Expected behavior

Should pass `\t` and `\n` symbols into hashing function, also maybe should do that by default when using `hashAlgorithm: 'sha256'`.

### Screenshots or Videos

_No response_

### Platform

- OS: [e.g. macOS, Windows, Linux]
- NodeJS: 20
- `graphql` version: 16.8.1
- `@graphql-codegen/*` version(s): "@graphql-codegen/cli": "5.0.3", "@graphql-codegen/client-preset": "4.5.1",

### Codegen Config File

```ts
import crypto from 'node:crypto'
import { join, resolve } from 'node:path'
import process from 'node:process'

import { addTypenameSelectionDocumentTransform } from '@graphql-codegen/client-preset'
import type { CodegenConfig } from '@graphql-codegen/cli'

const schemaDir = resolve(join(process.cwd(), '..', '..', 'apps', 'api-gql', 'schema', '*.graphqls'))

const config: CodegenConfig = {
config: {
scalars: {
Upload: 'File',
},
},
schema: schemaDir,
documents: ['src/api/**/*.ts'],
ignoreNoDocuments: true, // for better experience with the watcher
generates: {
'./src/gql/': {
preset: 'client',
config: {
useTypeImports: true,
},
presetConfig: {
persistedDocuments: {
hashAlgorithm: (operation) => {
const hash = computeQueryHash(operation)
return hash
},
},
},
documentTransforms: [addTypenameSelectionDocumentTransform],
},
},
}

export default config

function computeQueryHash(query) {
const hash = crypto.createHash('sha256')
hash.update(query)
return hash.digest('hex')
}
```

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.