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

[2.0.2] apollo-client returns empty data during mutation | bug #2496

Closed
seeden opened this issue Nov 6, 2017 · 10 comments
Closed

[2.0.2] apollo-client returns empty data during mutation | bug #2496

seeden opened this issue Nov 6, 2017 · 10 comments
Assignees

Comments

@seeden
Copy link
Contributor

seeden commented Nov 6, 2017

I am not sure if it is related to apollo-client or react-apollo but I have this issue. During mutation I will get data without cached fields.

Here is steps how to reproduce it

  1. I have one component with graphql query which is loading user by his username. It is working great. data.user contains all information.
@graphql(gql`
  query getQuestionContainerData($username: String!) {
    user(username: $username) {
      id
      isViewer
      cover
    }
  }
`, {
  options: ({ match: { params: { username } } }) => ({
    variables: {
      username,
    },
  }),
})
  1. Second component is changing users photo (it has one mutation)
@graphql(gql`
  mutation ($signed: String!) {
    user: submitUserCoverS3DirectUpload(signed: $signed) {
      id
      canEdit
      cover
    }
  }
`, {
    name: 'submitUserCoverS3DirectUpload',
  })
  1. First component will receive empty data.user (user is undefined) when second component starts mutation.
  2. After success mutation first component will receive correct data.user with all data.

My question is why I receive empty data.user ? Apollo client already have this data and I do not any clean cache.

My current deps:

    "apollo-cache-inmemory": "^1.1.0",
    "apollo-client": "^2.0.2",
    "apollo-link": "^1.0.0",
    "apollo-link-http": "^1.1.0",
    "apollo-link-batch-http": "^1.0.0",
    "apollo-link-retry": "^1.0.0",
    "apollo-link-dedup": "^1.0.0",
    "react-apollo": "^2.0.0",
@seeden seeden changed the title [2.0.2] apollo-client returns empty data after mutation | bug [2.0.2] apollo-client returns empty data during mutation | bug Nov 6, 2017
@jbaxleyiii
Copy link
Contributor

@seeden I'll try to make a reproduction and figure this out!

@jbaxleyiii jbaxleyiii added this to the Post 2.0 bugs milestone Nov 13, 2017
@jbaxleyiii jbaxleyiii self-assigned this Nov 13, 2017
@bogdansoare
Copy link

@jbaxleyiii I have the same issue, after mutation the data is empty

@bogdansoare
Copy link

For me it happens when the component with the query is unmounted and then mounted again and after that I run the mutation

@jbaxleyiii
Copy link
Contributor

This should be fixed on master!

@nicholas-l
Copy link

Is this published? Because I am getting a similar error, below is before and after a mutation that happens in a deeply nested child component from the query component. Loading is false and no error on data.

First is check if object is null (node)
Second line is props.data
Third line is props.data.error.

nodeviewer.jsx:74 data.node is null: false
nodeviewer.jsx:75 {variables: {…}, refetch: ƒ, fetchMore: ƒ, updateQuery: ƒ, startPolling: ƒ, …}error: (...)fetchMore: ƒ ()loading: falsenetworkStatus: 7node: { id: "R1RlY2hfQWRkcmVzczo1MzYxNDgwNw==", …} refetch: ƒ ()startPolling: ƒ ()stopPolling: ƒ ()subscribeToMore: ƒ ()updateQuery: ƒ ()variables: {objectId: "R1RlY2hfQWRkcmVzczo1MzYxNDgwNw=="}get error: ƒ 
nodeviewer.jsx:76 undefined

Mutation happened

nodeviewer.jsx:74 data.node is null: true
nodeviewer.jsx:75 {variables: {…}, refetch: ƒ, fetchMore: ƒ, updateQuery: ƒ, startPolling: ƒ, …}error: (...)fetchMore: ƒ ()loading: falsenetworkStatus: 7refetch: ƒ ()startPolling: ƒ ()stopPolling: ƒ ()subscribeToMore: ƒ ()updateQuery: ƒ ()variables: {objectId: "R1RlY2hfQWRkcmVzczo1MzYxNDgwNw=="}get error: ƒ ()__proto__: Object
nodeviewer.jsx:76 undefined

@vincentdesmares
Copy link

I got the same issue and struggled a lot to find the source of it.

I traced it back to this part of the code which swallowed an error:

try {
  var data = this.dataStore.getCache().read({
    query: query,
    variables: variables,
    previousResult: lastResult ? lastResult.data : undefined,
    optimistic: true
  })
  return maybeDeepFreeze({ data: data, partial: false })
} catch (e) {
  return maybeDeepFreeze({ data: {}, partial: true })
}

The result of my mutation was missing a field that was present in the original request.

query test {
   house {
      id
      length
      width
   }
}
mutation test {
   updateHouse(id: 1) {
      id
      length
   }
}

Width being missing, an error was thrown, catch by the previous code, and the object house ended undefined.

I'm not sure of what I have done wrong.

I'm using the 2.1.0, so the code related to this PR is at least not covering all the cases.

@jbaxleyiii Can you reopen this issue?

@juanhenriquez
Copy link

@vincentdesmares Same happens for me. I was missing a field in my mutation that was declared in the original query and this was causing that when the mutation completed successfully and the query gets called(the second time), the data that comes in the query was an empty object.

So the solution was put both the mutation and the query with the same fields.

Can you please take a look at this @jbaxleyiii ?

@zebulonj
Copy link

I've spent the last four hours trying to resolve a bug caused by the issue discussed in the last few comments here: a mutation returns a subset of the fields previously queried and cached on an object. Seems that this should be perfectly acceptable, especially if I know that my mutation isn't going to change the omitted fields. Commenting here for visibility, but I've opened a separate issue (this really is different from the original issue in this ticket): #3468.

@asia653
Copy link

asia653 commented Jun 7, 2019

I have spent multiple hours on this as well.

It was fixed by using a shared fragment. I had forgotten to add a field to my mutation that was on my query.

@NareshVadlamani
Copy link

NareshVadlamani commented Jun 24, 2019

Me too getting the same issue , i am saving the data and returning the id to use as a foreign key to the another table . But i am not getting any response , the data is coming undefined . I am not sure is this issue or i did some mistake (I am using hasura.io for graphql server). Even i checked the network tab, the response is coming fine.Any suggestions will be appreciated.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 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

9 participants