Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
React Apollo GraphQL what is the best way to fetch a partial data after CUD(Creating, Updating, Deleting)?

I get todo list with useQuery.

const { data, refetch } = useQuery(GET_TODOS);

After creating a todo, I get todo list with refetch like below.

const [ addTodo ] = useMutation(ADD_TODO, {
  onComplete: () => refetch()
});

const handleAddTodo = useCallback((todoArgs) => {
  addTodo({ variables: todoArgs });
}, []);

But It is obviously wasted time.

I tried to update only in an updated part. for that, I saved todos into a state and I changed this.

const [todos, setTodos] = useState([]);

...

const [ addTodo ] = useMutation(ADD_TODO, {
  onComplete: (updatedData) => {
    setTodos((prevTodos) => {
      const newTodos = prevTodos.map((todo) => todo.id === updatedData.id ? updatedData : todo);
      return newTodos;
    });
  }
}

...

useEffect(() => {
  setTodos(data);
}, [data]);

...

But I'm not sure It is a right way. I think there may be an official way for updating a part of data.

What's the best way to fetch a partial data after Creating, Updating, Deleting?


I'm using 'no-cache' as a default option in the project.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Managing the query response in a new state seems a bit overkill to me.

In fact, Apollo GraphQL client automatically refetch the updated data, as long as you are returning the updated data id field in the mutation result.

For other cases, you may want to use a custom update function option.

You can read more about that here:

https://www.apollographql.com/blog/apollo-client/caching/when-to-use-refetch-queries/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!