Husqvik / GraphQlClientGenerator

GraphQL C# client generator
MIT License
213 stars 49 forks source link

Support for nested query parameters #145

Closed bawahakim closed 1 year ago

bawahakim commented 1 year ago

Hello,

Love the simplicity and usefulness of this generator, thank you!

Our server has nested query parameters to allow fine tuning of queries. For example, I can search a resource as such:

query Workouts {
    workouts(name: { contains: "Something" }) {
        id
        name
    }
}

I've tried the below and it seems no matter what I can't reproduce a query as the above. A StringFilter is a class that has various filtering fields such as contains, gt, etc, and is generated in the schema by introspection. I am unsure if I am doing the query parameters correctly.

var containsFilterQuery = new GraphQlQueryParameter<string>("contains", defaultValue: "Something");
var nameStringFilter = new StringFilter();
nameStringFilter.Contains = containsFilterQuery;
var stringFilterQuery = new GraphQlQueryParameter<StringFilter>("name", defaultValue: nameStringFilter);

var builder = new QueryQueryBuilder().WithParameter(stringFilterQuery);
builder.WithWorkouts(new WorkoutQueryBuilder().WithId().WithName(), name: stringFilterQuery);

This results in a query that looks like this

query (
  $name: StringFilter = {
    contains: $contains
  }) {
  workouts(name: $name) {
    id
    name
  }
}

If I have only a single layer, it works as expected.

Any idea on how to make it work? If not, is it something that is planned to be supported? And if not, any clues on where the source code should be modified so I can possible to a PR?

Thanks Hakim

bawahakim commented 1 year ago

Somehow figured it out: I have to set the name parameter to null, and not add WithParameter. This seems to add the query as is without using variables, and works as expected.

var containsFilterQuery = new GraphQlQueryParameter<string>(-->null<--, defaultValue: "Something");
var nameStringFilter = new StringFilter();
nameStringFilter.Contains = containsFilterQuery;
var stringFilterQuery = new GraphQlQueryParameter<StringFilter>(-->null<--, defaultValue: nameStringFilter);

var builder = new QueryQueryBuilder().WithParameter(stringFilterQuery);
builder.WithWorkouts(new WorkoutQueryBuilder().WithId().WithName(), name: stringFilterQuery);