0no-co / gql.tada

🪄 Magical GraphQL query engine for TypeScript
https://gql-tada.0no.co
MIT License
2.58k stars 43 forks source link

fix: Allow `readFragment()` to accept fragment document as a generic #128

Closed kitten closed 7 months ago

kitten commented 7 months ago

[!NOTE]
It's debatable whether this is a feature or a fix, but since it's a relatively safe addition that complements #98 (for #77) and that we can safely roll back if needed, I'd like to try shipping this as a patch to evaluate this quickly.

Summary

This allows the document in readFragment to be passed as a generic, removing the need to keep fragments around as a runtime value.

// previous:
const unmaskedData = readFragment(document, maskedData);
// new:
const unmaskedData = readFragment<typeof document>(maskedData);

Replacing readFragment's complex type argument and data generic with an overload is long and wordy, but potentially makes it simpler for TypeScript to evaluate and prevents us having to maintain mirrorTypeRec.

In case this is needed, we can add its definition back to the overload, but this wouldn't be compatible with being able to pass just one generic for the document.

Set of changes

changeset-bot[bot] commented 7 months ago

🦋 Changeset detected

Latest commit: e0e4f7a179774536f1ea249201e64290cb67d499

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](https://github.com/0no-co/gql.tada/new/feat/allow-generic-fragments?filename=.changeset/fluffy-zebras-wave.md&value=---%0A%22%40fake-scope%2Ffake-pkg%22%3A%20patch%0A---%0A%0Afix%3A%20Allow%20%60readFragment()%60%20to%20accept%20fragment%20document%20as%20a%20generic%0A)