How to refetch all queries after a mutation?

How to refetch all queries after a mutation?


0

Using apollo graphql, I have a mutation that modify the "Person" object in the database

All throughout the app, I have various queries to fetch that from "Person". Sometimes I need the whole object, sometimes only the name or the age. There is a list of persons (generated using the PERSON_IDS query) and when an item in the list is clicked, a dialog open with all the person’s data (fetched using PERSON_DATA)

queries:


export const PERSON_IDS = graphql(`
  query personNames($familyId: String!) {
    family(query: { id: $familyId }) {
      persons {
        id
      }
    }
  }
`)

export const PERSON_DATA = graphql(`
  query person($personId: String!) {
    person(query: { id: $personId }) {
      id
      name
      age
      height
      weight
    }
  }
`)

export const PERSON_NAMES = graphql(`
  query personNames($familyId: String!) {
    family(query: { id: $familyId }) {
      persons {
        name
      }
    }
  }
`)

(The actual app is more complex, there are many different mutations)

mutation


export const UPDATE_PERSON = graphql(`
  mutation updatePerson($personId: String!, $person: PersonUpdateInput!) {
    updateOnePerson(query: { id: $personId }, set: $person) {
      id
    }
  }
`)

I call the mutation using

const [updatePerson] = useMutation(UPDATE_PERSON, {
  refetchQueries: [PERSON_IDS]
})

After the person is updated, the person_ids list is refreshed, but the other queries return the old data!

I can’t add all the queries in the refetchQueries array because there are a lot. Furthermore, I get a warning if I add a query that has never been used before


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *