[英]How does Apollo-Client GraphQL refetchQueries works?
Any idea how do we get the response data from refetchQueries
?知道我们如何从refetchQueries
获取响应数据吗? I got the query response data from mutation.我从突变中获得了查询响应数据。
import { gql } from 'apollo-boost';
export const DONE_TASK = gql`
mutation DoneTask($taskId: ID!) {
doneTask(input: {
taskId: $taskId
}) {
task {
id
status
}
}
}
`;
import { gql } from 'apollo-boost';
export const GET_TASKS_BY_STATUS = gql`
query GetTasksByStatus($status: String!) {
getTasksByStatus(status: $status) {
edges {
node {
id
status
description
}
}
}
}
`;
const response = await client.mutate({
mutation: DONE_TASK,
variables: {
taskId: 1
},
refetchQueries: () => [{
query: GET_TASKS_BY_STATUS,
variables: {
status: "OPEN"
},
}]
});
console.log(response);
Output
Output
data: { doneTask: { task: { id: 1, status: 'DONE'} } }
But I expect a response data from GET_TASKS_BY_STATUS
.但我期望来自GET_TASKS_BY_STATUS
的响应数据。
Any queries you re fetch through refetchQueries
should already be used by some useQuery
hook, Query
component or graphql
HOC.您通过refetchQueries
重新获取的任何查询都应该已被某些useQuery
钩子、 Query
组件或graphql
HOC 使用。 In order to access the data inside the same component as your mutation, you need to utilize the query being refetched inside that same component:为了访问与突变相同的组件内的数据,您需要利用在同一组件内重新获取的查询:
const { data } = useQuery(GET_TASKS_BY_STATUS, { variables: { status: 'OPEN' } })
const [mutate] = useMutation(DONE_TASK,{
variables: {
taskId: 1,
},
refetchQueries: () => [{
query: GET_TASKS_BY_STATUS,
variables: {
status: 'OPEN',
},
}],
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.