I have a nested GraphQL query with the following structure
query{
parentMetrics(cityNames: ["Cali"], orderingDateFrom: "2021-01-01"){
sales
orders
childMetrics(cityNames: ["Cali"], orderingDateFrom: "2021-01-01"){
aov
}
}
}
As you can see I am repeating the arguments because in the backend we run different queries to get parentMetrics and childMetrices but they require the same set of args which is redundant.
Can I do something like this instead?
query{
parentMetrics(cityNames: ["Cali"], orderingDateFrom: "2021-01-01"){
sales
orders
childMetrics{
aov
}
}
}
I am using github.com/graphql-go/graphql and currently this is what my code looks like
"parentMetrics": &graphql.Field{
Type: partnerGQL.ParentMetrics,
Args: graphql.FieldConfigArgument{
"cityNames": &graphql.ArgumentConfig{
Type:graphql.NewList(graphql.String),
}
"orderingDateFrom":&graphql.ArgumentConfig{
Type: graphql.String,
}
},
Resolve: partnerResolver.ResolveOrderItemMetrics,
}
The parentMetrics type has the nested Resolver for childMetrics
When you return a parent metric object, include the arguments of
parentMetricscall in that object.The resolver for
childMetricswill get the parent object and will be able to read its arguments from there.A quick example in Javascript: