apollo icon indicating copy to clipboard operation
apollo copied to clipboard

After mutation, cache was updated but not result

Open Vilsafur opened this issue 2 years ago • 2 comments

Describe the bug I request back with useQuery, store result in variable with useResult. When I mutate for create an entity, apollo's cache is updated but not my variable used by the useResult.

To Reproduce

const QUERY = gql`
  query getAllOfMyEntity($cursor: String) {
    entity(first: 100, after: $cursor) {
      pageInfo {
        endCursor
        hasNextPage
      }
      edges {
        node {
          _id
          id
          libelle
        }
      }
    }
  }
`

const { result, loading, fetchMore, refetch } = useQuery<{
  entities: Collection<Entity>
}>(QUERY)

const entities = useResult(result)

const MUTATION = gql`
  mutation createEntity(
    $input: createEntityInput!
  ) {
    createEntity(input: $input) {
      entity{
        libelle
        id
        _id
      }
      clientMutationId
    }
  }
`

const { mutate } = useMutation(MUTATION)

mutate({
  input: {
    libelle: form.libelle
  }
})

after I mutate, the entities is not updated, with or witout update's function un useMutation

Expected behavior After a mutate, I expect to my variable entities was updated with the created entity

Versions vue: 3.1.4 vue-apollo: 4.0.0-alpha.14 @apollo/client: 3.5.10

Additional context

Vilsafur avatar Apr 06 '22 14:04 Vilsafur

I believe what you are looking for is a watchQuery which is not implemented in this library. You might be able to get it directly from the apollo client.

https://www.apollographql.com/docs/react/api/core/ApolloClient#ApolloClient.watchQuery

micksi avatar Apr 07 '22 09:04 micksi

@micksi useQuery uses apolloClient.watchQuery

Akryum avatar May 12 '22 12:05 Akryum