Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Calling writeQuery does not update the props #2209

Closed
scottmcpherson opened this issue Jul 25, 2018 · 9 comments
Closed

Calling writeQuery does not update the props #2209

scottmcpherson opened this issue Jul 25, 2018 · 9 comments
Labels
has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository

Comments

@scottmcpherson
Copy link

scottmcpherson commented Jul 25, 2018

Intended outcome:
writeQuery should refresh the props data. You can see here that when a user logs in, this function gets called with the user. I'm trying to call writeQuery() to update the currentUser.

I've also tried calling writeQuery directly on the client and store, but no luck.

Actual outcome:
writeQuery does not refresh the props data

How to reproduce the issue:
You can clone this repo to see the issue: https://github.com/scottmcpherson/react-graphql-apollo-semanticui-starter and checking out commit 3be17f74071dc347164d1ac825c8503bc075ee8b

Version
"apollo-boost": "0.1.10",
"react-apollo": "2.1.9",

@scottmcpherson
Copy link
Author

scottmcpherson commented Jul 25, 2018

I got around this for the time being by calling refetch on the query. It's not ideal, but it does the trick

@captDaylight
Copy link

@scottmcpherson I've been struggling with the same issue.

I have a query GET_USER on page load to see if the user is logged in. If not, the header show log in/sign up... if so it shows log out. If the user is not logged in, then goes to the login page I tried doing something like this:

<Mutation
  mutation={LOGIN}
  variables={{ email, password }}
  update={(cache, { data }) => {
    cache.writeQuery({
      query: GET_USER,
      data: { user: data.login }
    });
  }}
> 
  // ... form

here are the queries that correspond:

import gql from 'graphql-tag';

export const GET_USER = gql`
  {
    user {
      id
      name
      email
      __typename
    }
  }
`;

export const LOGIN = gql`
  mutation LoginMutation($email: String!, $password: String!) {
    login(email: $email, password: $password) {
      id
      name
      email
      __typename
    }
  }
`;

However, when I log in successfully, that update does not update the query (as far as I can tell, assuming that the header should switch over to the logged out state as it's referencing the same query). The data that I have coming back after login in data.login is exactly the same format as my GET_USER query.

I'm going to try to refetch from the server for now too but this feels redundant as you said...

@captDaylight
Copy link

This thread is about refetching the user after log in, but fundamentally that feels like a redundant call.

@rosskevin
Copy link
Contributor

rosskevin commented Dec 26, 2018

possibly duplicate of #2099

@rosskevin rosskevin marked this as a duplicate of #2099 Dec 26, 2018
@rosskevin rosskevin reopened this Dec 26, 2018
@rosskevin
Copy link
Contributor

rosskevin commented Dec 26, 2018

#2099 is a mutate without writeQuery, but notes are there. Specifically, the writeQuery bug I just proved to be upstream in apollographql/apollo-client#2415 (comment)

I'm leaving this open because it is another permutation of what I think is the same upstream bug.

@rosskevin rosskevin added bug-upstream Bug confirmed to originate from a dependency confirmed has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository and removed bug-upstream Bug confirmed to originate from a dependency confirmed labels Dec 26, 2018
@rosskevin
Copy link
Contributor

I proved the bug upstream is not a bug, so back to looking at other cases to find it.

@danilobuerger
Copy link
Contributor

danilobuerger commented Dec 27, 2018

@scottmcpherson I don't think this has anything to do with writeQuery. When the site initially loads, it will fetch the CurrentUser query. The server returns:

{"data":{"currentUser":null},"errors":[{"message":"Invalid token","locations":[{"line":2,"column":3}],"path":["currentUser"],"extensions":{"code":"UNAUTHENTICATED","exception":{"stacktrace":[...]}}}]}

Since this is an error, the query isn't cached and is not observable. So then when you call writeQuery with the data the query is not "refetched" as nothing is triggering it as its not observable.

@danilobuerger
Copy link
Contributor

The only thing you have to do to get it working is:

diff --git a/server/data/resolvers.js b/server/data/resolvers.js
index b349a89..066f137 100644
--- a/server/data/resolvers.js
+++ b/server/data/resolvers.js
@@ -9,7 +9,7 @@ const resolvers = {
   Query: {
     currentUser: async (root, {}, { user }) => {
       if (!user) {
-        throw new AuthenticationError('Invalid token')
+        return null
       }
       const loginInUser = await User.findOne({ where: { id: user.id } })
       return loginInUser

@rosskevin
Copy link
Contributor

Thanks for digging in @danilobuerger. I'm closing given that solution/information.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has-reproduction ❤ Has a reproduction in a codesandbox or single minimal repository
Projects
None yet
Development

No branches or pull requests

4 participants