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

refetchQueries causes error if query isn't active #594

Closed
META-DREAMER opened this issue Aug 29, 2016 · 9 comments
Closed

refetchQueries causes error if query isn't active #594

META-DREAMER opened this issue Aug 29, 2016 · 9 comments
Labels
Milestone

Comments

@META-DREAMER
Copy link
Contributor

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.

@helfer
Copy link
Contributor

helfer commented Sep 6, 2016

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 ?

@helfer helfer added the 🐞 bug label Sep 6, 2016
@META-DREAMER
Copy link
Contributor Author

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.

@stubailo
Copy link
Contributor

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

@jesenko
Copy link
Contributor

jesenko commented Sep 19, 2016

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.

  2. 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?

@seeden
Copy link
Contributor

seeden commented Sep 30, 2016

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

@helfer
Copy link
Contributor

helfer commented Oct 21, 2016

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

@helfer
Copy link
Contributor

helfer commented Oct 24, 2016

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

@helfer helfer closed this as completed Oct 24, 2016
@trevordmiller
Copy link

trevordmiller commented Sep 5, 2017

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 #594 (comment)).

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'],
      },
    }
  ),

@trevordmiller
Copy link

Moved to #2143

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants