Replies: 5 comments 4 replies
-
because we already pass it would be possible with a breaking change - to always inject a
that would need to become:
not sure why you would need a code change for that, but I also don't know much about graphQL. Isn't the solution just to stub the request on network level? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply (and apologies for my belated response). I know it would be inconsistent, but would you consider passing
Also not entirely sure i understand the suggestion, but yea, I am attempting to stub the request at the network level, using Playwright's response interception. The thing with GraphQL, is that it's kinda hard to stub, because every request to your GraphQL endpoint has exactly the same request method and url, eg: (Btw I'm not really pushing for this change. I understand it's not ideal to introduce this change and I guess i'm just curious as the reason why we can't access |
Beta Was this translation helpful? Give feedback.
-
oh yes, that sucks :( I'm used to rest where this isn't a problem 😅
I actually think that is not a bad idea, however:
we wouldn't get type inference for the context like we would with queries, because we'll always need to add a type annotation for the anyways, this would be a non-breaking change, so if you want to go ahead with a PoC pull request, I'd be happy to take a look |
Beta Was this translation helpful? Give feedback.
-
Just an update if anyone is still looking to do something similar. It seems like this is possible by using the this-context in the function mutateFn(variables) {
const { mutationKey } = this
// gather params from mutationKey
const { params } = mutationKey[1]
return engine.mutate(mutationObj, {
variables: { ...params, ...variables },
})
} Haven't found any documentation of this, so might break - so use at your own risk :) |
Beta Was this translation helpful? Give feedback.
-
I also need the same flexibility on mutationFn but in my case for setting some initial variables (just coz unlike queryFn, mutationFn only allows access to variables). Here's how I achieved it (simplified version): I don't want to define mutationFn in each and every mutation so I set a default one in QueryClient: new QueryClient({
defaultOptions: {
mutations: {
mutationFn: async variables => {
const { endpoint, ...body } = variables
// ...
}
}
}
}) Abstracted useMutation to modify the mutate method so that it accepts some initial variables: function useDataMutation<
ResponseData,
RequestVariables extends Record<string, unknown>
>({
endpoint,
method = 'POST',
mutationKey,
mutationOptions,
}: {
endpoint: string;
method?: HttpMethod;
mutationKey: string,
mutationOptions?: UseMutationOptions<ResponseData, RequestVariables>
}) {
const { mutate, ...mutation } = useMutation([mutationKey], mutationOptions)
return {
....mutation.
mutate: (variables: RequestVariables) => mutate({
...variables,
endpoint,
method,
// ...
})
}
} Mutate const add = useDataMutation('/add')
const add = useDataMutation('/delete', 'PUT') |
Beta Was this translation helpful? Give feedback.
-
Hi there.
For queries, we can access the
queryKey
in thequeryFn
, but this is not the case formutationFn
.The only argument passed to the
mutationFn
is the mutationvariables
.Why is the
mutationKey
not passed to themutationFn
?Context:
operationName
property in the graphql request body, within my generic "fetcher"operationName
for mutations, as I can't get access to themutationKey
within themutationFn
Here's my generic fetcher:
Example generated query hook:
Example generate mutation hook:
Thoughts?
Beta Was this translation helpful? Give feedback.
All reactions