Thursday, May 24, 2018

How does Apollo Client + Server handle duplicate related nodes in the same query?

Leave a Comment

How does Apollo Client + Apollo Server handle duplicate related nodes in the same query?

Say I have this query:

query {   parents {     id     name     child {       id       name       secondChild {         id         name        }     }   } } 

The response has a duplicate related node (the secondChild node with an ID of sc1). Is the data for that node sent over the network twice? Or is Apollo smart enough to just send it once?

res = [ {     id: p1     name: "Parent one"     child[     {         id: c1         name: "Child one"         secondChild: {          # This is duplicated below              id: sc1             name: "Second Child one"         }     },     {         id: c2         name: "Child two"         secondChild: {          # This is duplicated above             id: sc1             name: "Second Child one"           }     }     ] }] 

This example is very simple but imagine the payload of sc1 was larger and it was repeated many more times.

1 Answers

Answers 1

I followed Tarun suggestion and checked the network response to my apollo-server. It's looks like no, Apollo does not optimize you network calls when fetching duplicate records.

Although it would be a nice feature to be implemented. Maybe you can create an issue in their github with this suggestion to see if they put this is their dev pipeline. Or you can try to implement yourself and create a pull request to share it with the rest of us. :)

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment