Need some guidance on setting up GraphQL as a beginner. I am facing challenges in efficiently setting up resolvers for my schema, especially when dealing with fields from different backend APIs.
type User {
name: String
address: String
dateOfBirth: String
department: String
function: String
manager: String
}
Fields like name, address, and dateOfBirth are retrieved from a basic administration system, while the other fields come from an organizational database.
Query {
User(parent, args, ctx) {
return {
name: '....',
address: '...',
dateOfBirth: '.....'
}
}
}
If each subfield is resolved individually, it would result in multiple requests when all fields are requested. To optimize this, I want to structure my schema in a way where similar fields can be fetched together in one API call.
type User {
name: String
address: String
dateOfBirth: String
organisation: Organisation
}
type Organisation {
department: String
function: String
manager: String
}
This approach reduces the number of requests made to the Organization API. However, it does make the schema look a bit unusual as these fields should ideally belong to a nested object structure. Additionally, any future changes in the data sources could potentially break the schema.
I experimented with using a dataloader to batch the fields, but it seems more suited for resolving n+1 problems rather than batching different field types.
Looking for suggestions on how to tackle this challenge effectively. Any advice would be greatly appreciated.