I want to have a GraphQL schema like the following:
scalar Timestamp
input StatsArgs{
start: Timestamp!
end: Timestamp
}
input PaginationArgs{
page: Int!
limit: Int!
}
type Station{
id: ID!
name: String!
revenue(
statsArgs: StatsArgs!
): Float!
}
type Product{
id: ID!
name: String!
stations: [Station!]!
}
type PaginatedProducts{
pages: Int!
total: Int!
items: [Product!]!
}
type Shop{
id: ID!
name: String!
stations: [Station!]!
products(
paginationArgs: PaginationArgs!
): PaginatedProducts!
}
type Query{
shops(
shopID: String
): [Shop!]
}
schema{
query: Query
}
and query it like so:
query {
shops {
products(paginationArgs: { page: 1, limit: 20 }) {
id
name
stations {
id
name
revenue(statsArgs: { start: "2023-01-01", end: "2023-01-31" })
}
}
}
}
The problem is that, using gqlgen and, probably, any other library in any language, it's difficult to make the server fetch the revenue of a station in regard to a product.
In order to make the server conscious that the client asks for the revenue of some station in regard to a product, I have 2 options:
- I make the productResolver.stations resolver ALWAYS fetch the revenue of a station in respect to each product
- I add a new Query field of the form
productStations(productIDs: [String!]!): [ProductStation!]!
I want to avoid both of these solutions. The first is bad for performance and the second is just ugly and impractical.
What are any other possible solutions? I am open to suggestions regarding golang libraries (code generation is not required) or schema changes.