A beginner question about setting up GraphQL. I'm struggling to find the right way to setup resolvers for my schema the most efficient way. Say in my schema I have a User type. Some field are resolved in one backend api, others in another.
type User {
name: String
address: String
dateOfBirth: String
department: String
function: String
manager: String
}
name, address and dateOfBirth are from a basic administration, the other fields are from an organisation database. Suppose this is my resolver:
Query {
User(parent, args, ctx) {
return {
name: '....',
address: '...',
dateOfBirth: '.....'
}
}
}
And resolvers for the specific subfields:
User {
department(parent, args, ctx) {
}
function(parent, args, ctx) {
}
manager(parent, args, ctx) {
}
}
This would result in 4 requests if the user requested all fields. The last three request could have been 1 request to get all fields in one go. I would of course like that to be 2 requests: one for the base information and one for the organisation API. That would perhaps lead to this schema:
type User {
name: String
address: String
dateOfBirth: String
organisation: Organisation
}
type Organisation {
department: String
function: String
manager: String
}
and my resolvers for the subfield:
User {
organisation {
return {
department: '...',
function: '...'
manager: '...'
}
}
}
Now the request to the Organisation API gets requested only once. However, the schema looks weird: those fields should be part of a sub-object. And if we e.g. were to move the manager data to it's own API, the schema would break if we move it out of the sub-object Organisation. I tried solving this with a dataloader, but after trying some code examples, I think dataloader is more about the n+1 problem, revolving around keys of the same object type, than batching different fields. So, what would be the right way to go about this?