dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.72k stars 1.31k forks source link

Persisted document custom hash function #9996

Closed nahn20 closed 3 weeks ago

nahn20 commented 4 weeks ago

Description

This features allows for customization of the hashAlgorithm configuration for persisted documents in the client preset. This mitigates the need to support additional future algorithms beyond sha1 and sha256 and gives developers full flexibility for generating document hashes, unlocking new use-cases.

Related #9994

Type of change

How Has This Been Tested?

Added three new test cases to client-preset.spec.ts. Ran yarn test client-preset.

Test Environment:

Checklist:

changeset-bot[bot] commented 4 weeks ago

🦋 Changeset detected

Latest commit: d4f44689b4210d723c982ca09d5ae0064751a8a4

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

n1ru4l commented 4 weeks ago

@nahn20 Can you please also add a changeset for these changes?

n1ru4l commented 4 weeks ago

What would happen if the hash function returns the same hash for multiple operations? Should we assert this?

nahn20 commented 4 weeks ago

I think that considering how advanced of a feature that this is, any developer using a custom function should know how to design an effective hash function. I'd be comfortable leaving in this footgun for the sake of full customizability, to account for potential use-cases that we aren't thinking of.

I also think that the cost of a bug here is minimal due to this potential issue being easy to debug. If a developer implements a poor hashing function, then they'll find their GraphQL calls aren't working. They would disable persisted documents, then see they are working again. From there, I have full confidence a developer can debug it down to multiple equal hash outputs by analyzing the mismatched operations.

Additionally, I think this would add a false sense of security. Not all approved operations are guaranteed to be present at generation time (for example, a backend which remotely stores operations from previous client versions).

This functionality is also easy for a developer to implement if they want the extra safety. I don't think this is necessary to build into the preset itself.