-
Notifications
You must be signed in to change notification settings - Fork 991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Query and QueryVariables type arguments in mockGraphQLQuery and mockGraphQLMutation #5274
Support Query and QueryVariables type arguments in mockGraphQLQuery and mockGraphQLMutation #5274
Conversation
✅ Deploy Preview for redwoodjs-docs canceled.
|
d254503
to
8f817cc
Compare
export type DataFunction = ( | ||
variables: Record<string, any>, | ||
export type DataFunction< | ||
Query extends Record<string, unknown> = Record<string, unknown>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the extends Record<string, unknown>
constraint this morning as I realized we still need to constrain this when no type argument is passed so that Query
isn't inferred as something like number
, e.g.:
// Still does not allow non-Record<string, unknown> data
// Argument of type 'number' is not assignable to parameter of type
// 'Record<string, unknown> | DataFunction<Record<string, unknown>, Record<string, any>>'.
mockGraphQLMutation('ExampleQuery', 42) // ❌
This should align with the previous behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tried this out locally, looks good! Thanks for the PR @pvenable.
I think we should update documentation for this new support. I can take care of it in separate PR but let me know @pvenable if you want to give it a go. https://redwoodjs.com/docs/testing#mocking-graphql-calls
Thanks, @callingmedic911. Please feel free to go ahead with docs, as it may be a while before I would be able to get to that. |
When using
msw
, I find it helpful to supply type arguments to the mock handlers to ensure that my query results are accurate and/or that I am accessing valid variables. This way I can't mess up the result shape or access variables that don't actually exist, and TypeScript catches if these things change so my mocks stay in sync with reality.This PR adds optional type arguments to
mockGraphQLQuery
andmockGraphQLMutation
to enable such type-safety. When these type arguments are omitted, the types default to what they were before this PR.Here's an example of how this works in practice: