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

client#query doesn't return updated data after performing a mutation #162

Closed
goje87 opened this issue May 1, 2016 · 2 comments
Closed
Assignees
Milestone

Comments

@goje87
Copy link

goje87 commented May 1, 2016

For now I have a simple schema. And in connectors I used mongoose to deal with mongodb.

type User {
  _id: String!
  mobileNum: String,
  fullName: String
}

type RootQuery {
  user(mobileNum: String): User
}

type RootMutation {
  updateUser(mobileNum: String, fullName: String): User
}

At client side I simply do:

var networkInterface = createNetworkInterface('http://localhost:8000', {
  credentials: 'include'
});

var client = new ApolloClient({
  networkInterface: networkInterface
});

var subs = client.watchQuery({query: `query { user(mobileNum: "${mobileNum}") { _id, mobileNum, fullName }}`})
  .subscribe({
    next: function(result) { console.log('next: ', result); },
    error: function(err) { console.err(err); } 
  });

Lets assume that initial user fetched has fullName as Name One. Now, if I run a mutation

client.mutate({
  mutation: `mutation {updateUser(mobileNum: "${mobileNum}", fullName: "Name Two") { fullName }}`
  })
.then((result) => { console.log(result) });

I see that subs#next is called twice. Both the time it prints Name One in the console. Then I find the response of the mutation call being printed with fullName as Name Two. Now, even if I call client#query on user(mobileNum: "${mobileNum}"), it returns Name One. But, if I call subs.refetch(), local store is updated. Both query and watchQuery are now printing Name Two.

Is there something that I'm doing wrong? Would appreciate your help in debugging this issue.

@stubailo
Copy link
Contributor

stubailo commented May 2, 2016

Hey, right now the recommended pattern is to call refetch after the mutation. This is the result of a few related issues:

  1. The callback is fired because we haven't implemented Add basic guards to avoid firing observer unnecessarily #124, so we sometimes fire unnecessary change notifications
  2. We don't document well what happens after a mutation, which is also mentioned in watchQuery doesn't get the new result after a mutation #144

I'm also working on documenting a better way to get refetches after a mutation, because right now that's not clear at all. I'm planning on documenting how to use id fields on your objects to incorporate the mutation result directly into the store.

Going to close this because I think the other issues cover the stuff you brought up - let me know if there is something I missed, and please post on there if you have thoughts about how these should be implemented!

@stubailo stubailo self-assigned this May 2, 2016
@stubailo stubailo added this to the 5/10 cycle milestone May 2, 2016
@stubailo stubailo closed this as completed May 2, 2016
@goje87
Copy link
Author

goje87 commented May 5, 2016

Thanks @stubailo!! Sure.. would do so...

jbaxleyiii pushed a commit that referenced this issue Oct 17, 2017
jbaxleyiii pushed a commit that referenced this issue Oct 18, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants