将refetchQueries与apollo一起使用并做出反应

时间:2017-11-15 07:12:23

标签: reactjs apollo react-apollo

我无法让refetchQueries工作。

我有一个表格"添加一个东西",但当我导航回"事物列表" - 只有刷新页面时才会显示新创建的内容。 所以(作为中间步骤)我也希望在成功之后更新事物列表"添加"。

我尝试了以下但是它不起作用。 docs很简单,所以我觉得我必须遗漏一些简单的东西:

  // Called successfully
  const updateThing = gql`
  mutation AddThing($id: ID, $title: String!) {
    problem(id: $id, title: $title) {
      id
      title
    }
  }

  // NOT CALLED :(
  const listThings = gql`
  query ListThings($includeArchived: Boolean = false) {
    problems(includeArchived: $includeArchived) {
      id,
      archived
    }
  }

  // Form (simplified version of Formik form)
  const ThingForm = props => (
    <form onSubmit={e=> {
      e.preventDefault();
      e.handleSave({
        variables: {
          title: 'test'
        }
      })
    }}
    >
      <button type="submit">Add thing</button>
    </form>
  )

  // Connect
  export const ThingAdd = compose(
    graphql(
      updateThing,
      {
        name: 'handleSave',
        options: ({values}) => ({...values }),
        refetchQueries: [{
          query: listThing,
          variables: ({includeArchived: true})
        }]
      }
    )
  )(
    ThingForm
  );

相关代表:

"apollo-cache-inmemory": "^1.1.0",
"apollo-client": "^2.0.2",
"apollo-fetch": "^0.6.0",
"apollo-link-core": "^0.5.4",
"apollo-link-http": "^1.1.0",
"graphql": "0.10.5",
"graphql-tag": "^2.5.0",
"graphql-tools": "^2.7.2",
"react": "^16.0.0",
"react-apollo": "^2.0.0",

1 个答案:

答案 0 :(得分:3)

refetchQueries应该是您传递给options HOC的graphql对象的属性:

options: ({values}) => ({
  refetchQueries: [{
    query: listThing,
    variables: ({includeArchived: true})
  }],
  ...values
}),