如何避免 Apollo useMutation 钩子的解构突变?
How to avoid destructuring mutation from Apollo useMutation hook?
我正在包装来自 react-apollo
的 useMutation
钩子,这样我就可以在每次使用突变时将错误发送给 Sentry
:
import { useMutation } from '@apollo/react-hooks'
import { DocumentNode } from 'graphql'
import { MutationHookOptions } from 'react-apollo'
type UseMutationWithSentryErrorsProps = {
refetch: MutationHookOptions<any, Record<string, any>>
from: string
mutation: DocumentNode
}
export const useMutationWithSentryErrors = ({
refetch,
from,
mutation
}: UseMutationWithSentryErrorsProps) => {
const response = useMutation(mutation, refetch)
const [_apolloMutation, { error: mutationError }] = response
if (mutationError) {
captureException(mutationError, {
info: 'There was an error fetching data.',
from
})
}
return response
}
我如何称呼突变是:
const [updateSpeakerMutation] = useMutationWithSentryErrors({
refetch,
from: 'SomeProvider',
mutation: UPDATE
})
这一行有问题:
const [_apolloMutation, { error: mutationError }] = response
我收到 variable is not read
错误。我无法在不访问突变的情况下访问响应错误,但我不是在这个包装函数中使用突变,而是在我真正使用它的地方。我尝试了经典的 _
语法,但仍然出现同样的错误。
你可以这样做:
const [, { error: mutationError }] = response
注意逗号
我正在包装来自 react-apollo
的 useMutation
钩子,这样我就可以在每次使用突变时将错误发送给 Sentry
:
import { useMutation } from '@apollo/react-hooks'
import { DocumentNode } from 'graphql'
import { MutationHookOptions } from 'react-apollo'
type UseMutationWithSentryErrorsProps = {
refetch: MutationHookOptions<any, Record<string, any>>
from: string
mutation: DocumentNode
}
export const useMutationWithSentryErrors = ({
refetch,
from,
mutation
}: UseMutationWithSentryErrorsProps) => {
const response = useMutation(mutation, refetch)
const [_apolloMutation, { error: mutationError }] = response
if (mutationError) {
captureException(mutationError, {
info: 'There was an error fetching data.',
from
})
}
return response
}
我如何称呼突变是:
const [updateSpeakerMutation] = useMutationWithSentryErrors({
refetch,
from: 'SomeProvider',
mutation: UPDATE
})
这一行有问题:
const [_apolloMutation, { error: mutationError }] = response
我收到 variable is not read
错误。我无法在不访问突变的情况下访问响应错误,但我不是在这个包装函数中使用突变,而是在我真正使用它的地方。我尝试了经典的 _
语法,但仍然出现同样的错误。
你可以这样做:
const [, { error: mutationError }] = response
注意逗号