Loading query overwrites proxy.writeQuery
See original GitHub issueThis is a re-issue of #1223 (updated using apollo-client
v1.4.0, react-apollo
v1.4.2 and proxy.writeQuery
)
I have a login
mutation with an update
option that updates my current user query:
const currentUserQuery = gql`
query currentUser {
viewer {
user {
id
username
}
}
}
`;
const LoginWithMutation = graphql(loginUserMutation, {
props: ({ mutate }) => ({
login: input =>
mutate({
variables: { input },
update: (proxy, { data }) => {
proxy.writeQuery({
query: currentUserQuery,
data: {
viewer: {
__typename: 'Viewer',
user: data.loginUser.user,
},
},
});
},
}),
}),
})(Login);
This works fine in isolation, the currentUser
query is updated with the user id
and username
and my User
React component gets the new current user data exactly as is should. However, when I have a current user, my User
component renders a UserInfo
component which has a currentUserInfo
query:
query currentUserInfo($orderBy: [UserInfoOrderByArgs]) {
viewer {
user {
id
info(orderBy: $orderBy) {
edges {
node {
id
title
...
}
}
}
}
}
}
When this query runs it first has data.loading: true
, as it should, but it also updates the result of the currentUser
query so the user
is null
(note that loading for the currentUser
query is false
, so it’s saying it’s finished loading and has a null
user
), which gets passed into my User
component and causes problems.
Note that this is all happening synchronously: login
mutation result updates currentUser
query with logged in user > User
component gets new current user data and renders the UserInfo
component > UserInfo
component runs currentUserInfo
query > currentUser
query is updated with user
as null
> User
component gets new current user data with null
user
> User
component un-mounts UserInfo
component.
If I change it so the UseInfo
component is rendered asynchronously (with a setTimeout
of 0), everything works as it should and currentUser
is not updated with user
as null
.
Also, if the currentUser
query is first run because the User
component is rendered (and there is a current user, say on a page reload), then the synchronous process works fine. It’s only when it starts with update
proxy.writeQuery(...)
from the login
mutation that the issue arises.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (1 by maintainers)
Top GitHub Comments
This issue has been automatically closed because it has not had recent activity after being marked as stale. If you belive this issue is still a problem or should be reopened, please reopen it! Thank you for your contributions to Apollo Client!
@helfer Was this issue resolved. I’ve come across many issues here that have been closed due to inactivity, but not resolved. It helps keep the issue count down, but many issues are getting lost into the ether it seems.