feat(types): allow type passing to enforce context types #12078
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR extends the types exported by apollo client to allow users to enforce strict types for operation contexts.
Here's a real-world situation that may server as an example of what this is trying to help.
I have a large project that fires many requests, and some are much faster than others.
many of the critical queries are well optimized, but users can experience significant delays in load time due to to them getting batched with a collection of less critical, slow requests.
to get around this, we implemented the following in the
BatchHttpLink
:so that I can provide
{ context: { excludeFromBatch: true }}
to the options foruseQuery
in order to take these render-critical queries and prevent them from being glued to a random batch.( btw - whoever added this
batchKey
field on the link - nice. This is a pretty cool thing and has come in clutch :) )Here's the rub, though. we work with typescript, and there are a lot of us.
Currently, there doesn't seem to be a way for me to enforce this operation context's type;
Record<string, any>
- the type that the relevantDefaultContext
forces us to stick with, isn't something we want to stick with.I ended up creating a function to wrap the options object in order to provide this context and guarantee that others in my project can't accidentally provide
context: { excludeFromBatch: false }
as a prop in args that are wrapped with it.We'd much rather be able to have TS warn us if we do the following:
since it's a pretty easy mistake to make.
So, that's what this PR attempts to do. It keeps the
DefaultContext
interface, but adjusts type signatures to make it a default, rather than effectively barring us from trying to type it out ourselves.Most types that have
DefaultContext
mentioned have been altered to allow consumers to provide a generic in order to enforce. On types where it's been added to existing type arguments, it's provided last so codegen tools don't break and always defaults toDefaultContext
if nothing is provided there.Since it's a type-level-only addition, tests were not added. The changeset proposes a patch, rather than a feature version as this should be semver-safe.
Some core types were extended with generics. I can reverse some of this to limit the scope, but included them for review here to make things easier.
This is my first PR here - please let me know if I need to change anything, or if this is something that already has a solution that I totally wooshed over.
Thank you for building this library, and for taking the time to hear this one out. I appreciate all y'all do.