Skip to content
This repository has been archived by the owner on Jul 10, 2019. It is now read-only.

Updating cache in response to a query #324

Open
kocur4d opened this issue Oct 29, 2018 · 1 comment
Open

Updating cache in response to a query #324

kocur4d opened this issue Oct 29, 2018 · 1 comment
Assignees

Comments

@kocur4d
Copy link

kocur4d commented Oct 29, 2018

I've got a one use case I am having trouble with implementing using apollo-link-state.

Lets say I have a view which is displaying a list of images, in a sidebar, in a form of thumbnails and when you click on an image that image is shown enlarged in a main view.

I can accomplish this with a query to the server to get all images:

const GET_IMAGES = gql` 
  query getImages() { 
    images {
      id
      path
    }
  }
`

And display them in the sidebar.
Then I can attache the mutation:

const SELECT_IMAGE = gql` 
  mutation selectImage($path: String!) {
    selectImage(path: $path) @client 
  } 
`

to the onClick event of every image and have selectedImage being updated inside of the cache via the selectImage resolver.
Then the main component would query selectedImage to find out which image should it display enlarged.

This part works.

What I am trying to do is to set the cache to the first image on every new GET_IMAGES query.

I could do this:

const config = ({
  options: ({data: {images}}) => { 
    variables: { 
      path: images && images[0] && images[0].path 
    }
  }
})

export default compose(
  graphql(GET_DATA),
  graphgl(SELECT_IMAGE, config)
)(Sidebar)

This will not work the way I would expect it, maybe not a right word. graphgl(SELECT_IMAGE, config) is not invoking mutation it is simply passing mutate prop to the Sidebar. I need to call it to invoke the mutation, for example in componentDidMout.

So is there a way to invoke a mutation automatically?

My example is a simple case just to demonstrate. What I have tried to explain here is that the apollo-link-state is missing a way to update its cache as reaction to some changes - result of a query for example. Without a need to render the component and calling mutation method in one of the lifecycle hook.

Hope this make sense.

@kocur4d
Copy link
Author

kocur4d commented Oct 29, 2018

Another thing that comes to mind is implementation of options.update for the queries options when you use apollo-link-state. Then you could skip the mutation from the composition entirely and just use the options.update to specify what else you need updated in a cache as a result of the query.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants