Pau*_*ool 3 reactjs graphql react-apollo apollo-client graphql-mutation
尝试refetchQueries在我的useMutation钩子中定义时遇到以下错误。
Type 'DocumentNode' is not assignable to type 'string | PureQueryOptions'.
Property 'query' is missing in type 'DocumentNode' but required in type 'PureQueryOptions'.
Run Code Online (Sandbox Code Playgroud)
我不确定我做错了什么,因为 Apollo 的官方文档对此有点不清楚,而且我在谷歌搜索时找不到任何优先级。
据我了解该错误,query我的 GraphQL 查询中缺少该属性。我是否将正确的值传递给了refetchQueries?我认为我做得对,但后来我不知道为什么它抱怨query没有参与其中。
代码
import { useMutation, useQuery } from '@apollo/client';
import { GET_CUSTOMERS, MARK_AS_VIP } from './queries';
export default function Customers() {
const [ markAsVIP, { data: vips, loading: vipLoading, error: vipError } ] = useMutation( MARK_AS_VIP );
const { loading, error, data: getCustomerResp } = useQuery( GET_CUSTOMERS );
const handleMarkAsVIP = ( customerId: string ) => {
markAsVIP( {
variables: { id: customerId, tags: [ 'VIP' ] },
refetchQueries: [
GET_CUSTOMERS, // error shows here
'getCustomers'
]
} )
}
}
Run Code Online (Sandbox Code Playgroud)
查询
import { gql } from '@apollo/client';
export const GET_CUSTOMERS = gql`
query getCustomers {
customers( first: 50 ) {
edges {
cursor
node {
id
displayName
tags
}
}
}
}
`;
export const MARK_AS_VIP = gql`
mutation markAsVIP( $id: ID!, $tags: [String!]! ) {
tagsAdd( id: $id, tags: $tags ) {
node {
id
}
userErrors {
field
message
}
}
}
`;
Run Code Online (Sandbox Code Playgroud)
自己找到了答案。虽然它看起来不像文档中的那样,但以下内容有效。
const handleMarkAsVIP = ( customerId: string ) => {
markAsVIP( {
variables: { id: customerId, tags: [ 'VIP' ] },
refetchQueries: [
{ query: GET_CUSTOMERS },
'getCustomers'
]
} )
}
Run Code Online (Sandbox Code Playgroud)
我认为它说该属性query丢失了,所以我只是尝试传递一个带有该query属性的对象,认为它永远不会起作用,但它确实起作用了。
| 归档时间: |
|
| 查看次数: |
124 次 |
| 最近记录: |