Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doubt: Removing Trainer Section in AddPokemonCard.js After Adding forcefetch field in Pokedex.js #70

Closed
gdad-s-river opened this issue Apr 14, 2017 · 2 comments

Comments

@gdad-s-river
Copy link

gdad-s-river commented Apr 14, 2017

In the tutorial it is written that after adding

// Pokedex.js

const PokedexWithData = graphql(TrainerQuery, {
  options: {
    variables: {
      name: 'Arihant Verma '
    },
    forceFetch: true,  <---- # this
  }
}
)(Pokedex)

Why could we remove

// AddPokemonCard.js

const createPokemonMutation = gql`
  mutation createPokemon($name: String!, $url: String!, $trainerId: ID) {
    createPokemon(name: $name, url: $url, trainerId: $trainerId) {
      trainer {     <---- # this trainer section
        id
        ownedPokemons {
          id
        }
      }
    }
  }
`

and replace it just with id.

Doubt 1: Earlier we had to explicitly tell that we wanted to fetch trainer object after mutation is done, what is being returned now, with only id being returned after mutation? What does it exactly mean?

It's written for the trainer object that

What this does, is telling Apollo Client to refetch the trainer object and all its related pokemons whenever we create a new pokemon. Apollo Store identifies the trainer object by its id and it basically merges the known pokemons with all the pokemons in the mutation response. Even though this is highly inefficient, this works in our example.

Doubt 2: Does the above holds true only after we have added dataIdFromObject: o => o.id in ApoloClient instantiation ?

@marktani
Copy link

Doubt 2: Does the above holds true only after we have added dataIdFromObject: o => o.id in ApoloClient instantiation ?

Yes 🙂

Before, we would query not only the newly created pokemon id, but actually the trainer and all the pokemon he owns. By adding the forceFetch option (this is by the way renamed to forcePolicy in the latest Apollo version), we are querying the whole trainer whenever we visit the route again.

Hope that helps! I tried to explain it in more detail in this excursion: https://www.learnapollo.com/excursions/excursion-02/

@gdad-s-river
Copy link
Author

gdad-s-river commented May 8, 2017

Thanks, @marktani, this is helpful! Understood everything now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants