Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

reactjs

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.

like image 772
Evanss Avatar asked Nov 07 '22 07:11

Evanss


1 Answers

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. :)

like image 110
Thiago Loddi Avatar answered Nov 15 '22 08:11

Thiago Loddi