After the mutation, how do I update the affected data through the views?

enter image description here

I have both a request getMoviesand a mutation addMovie. However, when it addMoviehappens, I wonder how best to update the list of films in the "Edit Movies" and "My Profile" sections to reflect the changes. I just need a general / high-level overview, or even just a concept name, if it's simple, on how to do it.

My initial thought was to keep all the movies in my Redux store. When the mutation ends, she must return the newly added film, which I can associate with the films of my store.

After “Add Movie”, it will appear on the “Edit Movies” screen, where you can see the recently added movie, and then, returning to “My Profile”, it will also be there.

Is there a better way to do this than keep it all in my own Redux store? Is there some kind of Apollo magic that I don't know about, can this do it for me?


EDIT : I found an idea updateQueries: http://dev.apollodata.com/react/cache-updates.html#updateQueries I think this is what I want (please let me know if this is the wrong approach). This seems better than the traditional way of using my own Redux store.

// this represents the 3rd screen in my picture
const AddMovieWithData = compose(
  graphql(searchMovies, {
    props: ({ mutate }) => ({
      search: (query) => mutate({ variables: { query } }),
    }),
  }),
  graphql(addMovie, {
    props: ({ mutate }) => ({
      addMovie: (user_id, movieId) => mutate({
        variables: { user_id, movieId },
        updateQueries: {
          getMovies: (prev, { mutationResult }) => {
            // my mutation returns just the newly added movie
            const newMovie = mutationResult.data.addMovie;

            return update(prev, {
              getMovies: {
                $unshift: [newMovie],
              },
            });
          },
        },
      }),
    }),
  })
)(AddMovie);

addMovie " " , getMovies (woah)! " ", ? getMovies? getMovies , " ", ?


EDIT2: MyProfile EditMovies getMovies, , . addMovie - updateQueries getMovies. . , ?

, , : ?

+4
1

updateQueries, "" , , ( ).

-, , : .

, ( , - , , , ).

, "" , , . , updateQueries.

+1

Source: https://habr.com/ru/post/1658237/


All Articles