Q:

Best practice loading entity with related entities

I am trying to figure what the best practice with loading data from web apis is.
Assume you have a model structure as per below:
class School {
private string name;
private List<Classroom> Rooms;
}

class Classroom {
private string Type;
private School school;
private List<Student> Students;
}

class Student {
private string Name;
private int Age;
private Classroom Room;
}

Is it better practice to load a single school entity and include all the related data (School + classrooms + students), then use the entity set at client side
or
is it better practice to just load the individual entities depending on what you need
i.e. get School when school info needed, get Classroom when classroom needed so forth.
I can see pros and cons to both but keen to know if there is best practice.

A:

Eager or Lazy Loading
If you know exactly how the data is used, you can easily decide, if eager loading (load everything at once) or lazy loading (do a second request if needed) gives better average performance.
Let the Consumer Decide
If your platform is more generic, so you don't have the information to optimize the data retrieval / transport, you should leave the decision to the API consumer.
Request Parameters
You could allow request parameters that specify, which related data is transmitted:
api/school/:name?with=rooms,rooms.students

GraphQL
Another way is GraphQL, which allows you to specify the requested data in the request body.
api/school/:name

and the body to retrieve everything
{
    school {
        name
        rooms {
            type
            students {
                name
                age
            }
        }
    }
}

