如何refetch查询具有args选项的查询

时间:2019-05-21 06:51:31

标签: reactjs graphql apollo

我想知道如何在阿波罗的 refetchQueries 期间传递自变量

代码

submitForm(e) {
        e.preventDefault();
        this.props.addAppleMutation({
            variables: {
                        color: "red"
                        weight: "0.33"
            },
            // stucked here
            refetchQueries: [{ query: loadApplesQuery, variables: {//argument i want to pass} }]
        })
    }

我的查询

const loadApplesQuery = gql`
    query($id: ID) {
        apple(id: $id) {
            color
            size
            id
        }
    }
`

1 个答案:

答案 0 :(得分:1)

是的,您可以执行此操作,也可以查看要解决的相同情况

https://www.apollographql.com/docs/react/api/react-apollo#graphql-mutation-options-refetchQueries

例如,您可以使用高阶组件(HOC)进行此操作,您可以执行类似的操作


const loadApplesQuery = gql`
    query($id: ID) {
        apple(id: $id) {
            color
            size
            id
        }
    }
`;

const addAppleMutation = gql`
    mutation($apple: Apple) {
        addApple(Apple: $apple)
    }
`;

export default graphql(addAppleMutation, {
  name: 'addAppleMutation',
  options: {
    refetchQueries: (mutationResult) => [{
        query: loadApplesQuery,
        variables: {
          id: mutationResult.id,
        }
      }]
  },
})(MyComponent);

,然后在SubmitForm方法中可以执行以下操作


submitForm(e) {
   this.props.addAppleMutation({ variables : { color: 'green', size: 'small'}});
}

希望对您有所帮助。