Skip to content
New issue

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

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

Already on GitHub? # to your account

hashAlgorithm function wrong operation string #10200

Open
Satont opened this issue Nov 23, 2024 · 1 comment
Open

hashAlgorithm function wrong operation string #10200

Satont opened this issue Nov 23, 2024 · 1 comment
Labels
waiting-for-answer Waiting for answer from author

Comments

@Satont
Copy link

Satont commented Nov 23, 2024

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

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

@eddeee888
Copy link
Collaborator

eddeee888 commented Jan 23, 2025

Hi @Satont ,

I'm trying this on my end and cannot reproduce this.

Could you please create a minimal reproduction available on GitHub, Stackblitz or CodeSandbox.

@eddeee888 eddeee888 added the waiting-for-answer Waiting for answer from author label Jan 23, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
waiting-for-answer Waiting for answer from author
Projects
None yet
Development

No branches or pull requests

2 participants