https://github.com/mstuart/graphql-hash
Generate a deterministic hash of a GraphQL query for caching and persisted queries
https://github.com/mstuart/graphql-hash
Last synced: 19 days ago
JSON representation
Generate a deterministic hash of a GraphQL query for caching and persisted queries
- Host: GitHub
- URL: https://github.com/mstuart/graphql-hash
- Owner: mstuart
- License: mit
- Created: 2026-02-16T00:28:31.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2026-06-04T04:44:50.000Z (24 days ago)
- Last Synced: 2026-06-04T05:13:38.415Z (24 days ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
README
# graphql-hash
> Generate a deterministic hash of a GraphQL query for caching and persisted queries
## Install
```sh
npm install graphql-hash
```
## Usage
```js
import graphqlHash from 'graphql-hash';
graphqlHash('{ user { name } }');
//=> 'e3b0c44298fc1c14...'
// Whitespace and comments don't affect the hash
graphqlHash(`
# Fetch user
{
user {
name
}
}
`);
//=> 'e3b0c44298fc1c14...' (same hash)
```
## API
### graphqlHash(query, options?)
Returns a hex-encoded hash `string`.
#### query
Type: `string`
The GraphQL query string.
#### options
Type: `object`
##### algorithm
Type: `string`\
Default: `'sha256'`
The hash algorithm to use. Any algorithm supported by `node:crypto` can be used.
### normalizeQuery(query)
Returns a normalized `string` with comments stripped, whitespace collapsed, and spaces removed around structural characters.
#### query
Type: `string`
The GraphQL query string to normalize.
## Related
- [error-serialize](https://github.com/mstuart/error-serialize) - Serialize and deserialize Error objects
## License
MIT