2

我正在使用 reason-apollo 并在尝试设置时遇到问题refetchQueries我的设置与此处的 swapi 示例非常相似。我有一个列表和一个表单,可以通过突变添加新项目。突变成功后,我想重新获取列表中的项目。我创建新项目的代码如下所示(跳过一些不相关的 ui 代码):

module CreateFilm = [%graphql
  {|
  mutation createFilm($name: String!, $favourite: Boolean!, $rating: Int!, $ratedOn: DateTime!) {
      createFilm(name: $name, favourite: $favourite, rating: $rating, ratedOn: $ratedOn) {
          id
          favourite
          name
          rating
      }
  }
|}
];

module CreateFilmMutation = ReasonApollo.CreateMutation(CreateFilm);

type state = {
  favourite: bool,
  rating: int,
};

type action =
  | ToggleFavourite
  | UpdateRating(int);

let component = ReasonReact.reducerComponent("FilmNew");

let make = _children => {
  ...component,
  initialState: () => {favourite: true, rating: 5},
  reducer: action =>
    switch (action) {
    | ToggleFavourite => (
        state => ReasonReact.Update({...state, favourite: ! state.favourite})
      )
    | UpdateRating(newRating) => (
        state => ReasonReact.Update({...state, rating: newRating})
      )
    },
  render: ({state, send}) => {
    let now = Js.Date.make() |> Js.Date.toISOString |> Js.Json.string;
    let newFilm =
      CreateFilm.make(
        ~name="Temporary test",
        ~rating=state.rating,
        ~favourite=state.favourite,
        ~ratedOn=now,
        (),
      );
    <CreateFilmMutation>
      ...(
           (mutation, _result) =>
             <form
               onSubmit=(
                 event => {
                   ReactEventRe.Synthetic.preventDefault(event);
                   mutation(
                     ~refetchQueries=[|"allFilms"|],
                     ~variables=newFilm##variables,
                     (),
                   )
                   |> ignore;
                 }
               )>
               <input
                 className="toggle"
                 _type="checkbox"
                 checked=state.favourite
                 onChange=((_) => send(ToggleFavourite))
               />
               ("is favourite" |> ReasonReact.string)
               <input _type="submit" value="Submit" />
             </form>
         )
    </CreateFilmMutation>;
  },
};

allFilms电影被成功添加,当页面刷新时,我可以在列表中看到它(正在使用查询)。但refetchQueries似乎没有任何效果。我的后端使用的是 graph.cool,类似于文档中的 swapi 示例。任何帮助表示赞赏,谢谢!

4

1 回答 1

0

问题不在于突变,而在于我想重新获取的查询 - 它缺少其 graphql 操作名称 ‍♂️</p>

于 2018-06-13T16:48:23.867 回答