I have a schema that looks like the following, each city and town both have unique ID and belong to a state.
type City {
id: ID!
population: Int
name: String
state: State
}
type Town {
id: ID!
population: Int
name: String
state: State
}
type State {
id: ID!
population: Int
name: String
}
The queries to fetch the associated info can take either of the following
query getTownAndState($townId: ID!) {
town(townId: $townId) {
population
name
state {
name
population
}
}
}
query getCityAndState($cityId: ID!) {
city(cityId: $cityId) {
population
name
state {
name
population
}
}
}
The resolvers to fetch for either a city or a two are straightforward:
resolvers = {
Query: {
city: () => Api.getCity(args.id),
town: () => Api.getTown(args.id),
state: () => Api.getState(args.id),
},
However fetching the city and/or town's associated state is ambiguous because the parentTownOrCity could be either a city, or a town. And the APIs for fetching each respectively is Api.getCity and Api.getTown. Api.getCityOrTownById doesn't exist.
State: {
name: (parentTownOrCity) => {
Api.getCityOrTownById(parent.id); // <-- what goes here? How to tell if it's a town or a city
}
}
}