Apollo-client: refetchQueries causes error if query isn't active

Created on 29 Aug 2016  路  9Comments  路  Source: apollographql/apollo-client

If I use the refetchQueries option on a mutation and pass in a name of a query that hasn't run yet, I get an APOLLO_MUTATION_ERROR:

image

This is my mutation:

export const createLook = (input) => ({
  mutation: gql`
    mutation createLook($input: LookCreateType) {
      createLook(input: $input) {
        _id
        mode
        name
        description
        cover
        character {
          _id
          name
          mode
        }
        show {
          _id
          name
          mode
        }
      }
    }
  `,
  variables: {
    input
  },
  refetchQueries: ['getOwnLooks']
});

So for this, the getOwnLooks query hasn't been run yet. If I make sure the query has run/exists in the apollo store, I get no error. Note, the mutation itself is successful, it's just that it gives this error when trying to refetchQueries.

馃悶 bug

Most helpful comment

I think its a fairly common use case. For example, lets say there is a "profile" page that has a list of all the user's posts, and also a "create post" screen for writing and creating a new post. If the user doesn't visit the profile page first, the "getProfile" query isn't run. But on the "create post" screen, the mutation for creating a new post should update the list of all the users posts with refetchQueries.

All 9 comments

Thanks for reporting this. I don't think we expected that someone would try to update queries that hadn't run yet. I think we need to have some sort of warning, but maybe printing a warning to the console is better than throwing an error. Thoughs @stubailo ?

I think its a fairly common use case. For example, lets say there is a "profile" page that has a list of all the user's posts, and also a "create post" screen for writing and creating a new post. If the user doesn't visit the profile page first, the "getProfile" query isn't run. But on the "create post" screen, the mutation for creating a new post should update the list of all the users posts with refetchQueries.

Agreed, this is a common thing that shouldn't error.

For the use case noted by @hammadj, we have two additional requirements that seem to be problematic with current refetchQueries api:

  1. Conditionally refetching queries based on mutation result

We have mutations, that return { result errors }. If there are server-side validation errors, errors array is populated, and result is null. In case of errors, profiles query does not have to be invalidated.

  1. Clearing of query cache instead of refetching

If list of profiles is rendered after adding profile, it would be sufficient to just clear previous profiles query instead of refetching it - it would be automatically fetched again when mounting profiles component.

Maybe updateQuery could be extended to cover these two cases (and could also be used instead of refetchQueries), e.g. returning undefined from reducer could clear cache for query, and returning true from reducer would trigger refetch?

@jesenko you are right with second point. We need to have a support for clearing cache

@hammadj Can you check if this bug has been fixed in 0.5.0-1? I think it should be fixed.

Closing this, since it sounds like this has been fixed with #700.

I'm getting the unknown query with name {Query} asked to refetch warning whenever a mutation is run that has a refetchQuery that hasn't been run yet (which occurs often in our app, since users can start on different routes as @hammadj mentioned in https://github.com/apollographql/apollo-client/issues/594#issuecomment-246463834).

image

Is there a way to tell a refetchQuery to only run if the query has run? Or to remove the warning and just do this in the library?

I'm using the query name as a string for the refetchQueries arg like:

  graphql(
    gql`
      mutation {
        someMutation {
          someField
        }
      }
    `,
    {
      options: {
        refetchQueries: ['SomeQuery', 'AnotherQuery'],
      },
    }
  ),
Was this page helpful?
0 / 5 - 0 ratings

Related issues

NeoPhi picture NeoPhi  路  3Comments

canercandan picture canercandan  路  3Comments

gregorskii picture gregorskii  路  3Comments

helfer picture helfer  路  3Comments

stubailo picture stubailo  路  3Comments