Skip to content
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

hashAlgorithm function wrong operation string #10200

Open
Satont opened this issue Nov 23, 2024 · 0 comments
Open

hashAlgorithm function wrong operation string #10200

Satont opened this issue Nov 23, 2024 · 0 comments

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant