Taylor Austin
Taylor Austin

Reputation: 5987

How to pass an Id to a query that is not being called in the component during refetchQuery in apollo/graphql/react

I am trying to fetch a query that needs an id to be passed to it. However I am not calling the query in the component. I tried passing it in the export with options, but couldn't get that to work.

Here is what I have:

 this.props
  .addLyricToSong({
    variables: {
      content: this.state.content,
      songId: this.props.songId
    },
    refetchQueries: [{query: fetchSong}]
  });

The error I get as I said is that the query fetchSong needs an id to be passed to it.

I tried this at the bottom:

options: props => {
  return { variables: { id: props.songId } };
}

but as I said I don't call this query in the component and I think that is why it is not working.

Upvotes: 1

Views: 366

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84687

As outlined in the docs, each object that's part of the array passed to refetchQueries should consist of two properties:

query: Query is a required property that accepts a GraphQL query created with graphql-tag’s gql template string tag. It should contain a single GraphQL query operation that will be executed once the mutation has completed.

[variables]: Is an optional object of variables that is required when query accepts some variables.

So if your fetchSong query takes any variables, you'll need to pass in those variables as well:

refetchQueries: [{query: fetchSong, variables: { id: 'someID' }}]

Upvotes: 2

Related Questions