Add support for passing gql.tada
fragments in context.query
#9490
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.
The short version is, assuming you have
gql.tada
setup and you have an import to@keystone-6/core/gql.tada
somewhere (this includes some types).From the example updated in this PR:
this makes the
data
variable type:The types to make this work are only added when
@keystone-6/core/gql.tada
is imported so that Keystone itself doesn't have to take on a hard dependency ongql.tada
since then we would either have a normal dependency ongql.tada
and create the same problems that Keystone has with requiring a specific version of Next or make it a required peer dependency and force every user to installgql.tada
even if they're not using it. This way it's optional and people don't have to use an exact version ofgql.tada
that matches what Keystone specified. This is what the weird declaration merging stuff is for, it's weird but it works fine and doesn't really affect users (beyond being required to import@keystone-6/core/gql.tada
before this works).While users have to specify a bit more when selecting fields (especially the type condition,
on Post
/etc.), this is setup so that it enforces that the type condition matches the list you're querying on so if you mess it up, you get a TypeScript error (the runtime error you see getting thrown in the code is just a backstop, you will get a TypeScript error for it, not just the runtime error).Technically this also makes the following code work:
That's not really by design and you probably shouldn't do it but just due to the way this is written, that also works which seems fine imo. (like we could do this in a way in which that code wouldn't work but I don't really think it's a problem)
The making
context.query.*.findOne
returnPromise<T | null>
instead ofPromise<T>
(which was wrong) is unrelated the other type stuff but I just noticed it was wrong and fixed it here.