0

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

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107

1 Answers1

0

When you return a parent metric object, include the arguments of parentMetrics call in that object.

The resolver for childMetrics will get the parent object and will be able to read its arguments from there.

A quick example in Javascript:

const resolvers = {
  parentMetrics: (parent, args) => {
    const metric = getParentMetric(args);
    return {
      ...metric,
      args: args,
    };
  },
  childMetrics: (parent) => {
    return getChildMetric(parent.args);
  }
}
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107