I have a question below:
Let's say I have a product type like this
type Option {
id: Int!
value: String!
}
type Product{
id: Int!
name: String!
price: Float!
options: Option
}
If I have a schema like that, every time I need product options (I have productID from the request), I need to query the entire product (with id, name, price) and I will have 2 Mysql queries to Database (1 to get product and 1 to get product options).
Should I have an additional standalone field in Query object like this to get product option base on productID? And if I need to keep nested schema like above, is there any way to get product options without executing it's parent(Product) resolver?
product_options(productId: Int!) : Option
Thanks
This is more of a data relationship question I think, but nonetheless...
IMO the table for the
Product Options
in the SQL database (if it doesn't already) should have a column that is a foreign key to theProduct
that it belongs to / is associated with. Doing that allows you to easily have nested GraphQL types and field level resolvers.Once that's done you won't need a separate query for the options structure, your field level resolver for
Product.options
would suffice by just querying:You can use the already fetched
Product.id
field in your code to run the field level query for their options row.