Husqvik / GraphQlClientGenerator

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

Support `null` parameters. #113

Closed Jeroen-Sturm closed 2 years ago

Jeroen-Sturm commented 2 years ago

Used NuGet Packages:

GraphQlClientGenerator version 0.9.12 GraphQl.Client version 5.1.0

Used Language: net6.0

for query-parameters I would like to suggest to implement a possibility to add parameters without values so for example :

var homeIdParameter = new GraphQlQueryParameter<Guid>("homeId", "ID", null);

var builder =
  new TibberQueryBuilder()
    .WithViewer(
      new ViewerQueryBuilder()
        .WithHome(new HomeQueryBuilder().WithAllScalarFields(), homeIdParameter)
    )
    .WithParameter(homeIdParameter);

var variables = new {
    homeId = "SOME GUID",
};

So we can use it with GraphQlHttpClient which support Variables as seen here. Because in my case I have queries that have nullable parameters but when I fill in null I get a GraphQL error that says :

{
    "errors": [
        {
            "message": "Syntax Error GraphQL (2:28) Unexpected Name \"null\"\n\n1: query (\n2:   $parameterName: UUID = null,\n",
            "locations": [
                {
                    "line": 2,
                    "column": 28
                }
            ]
        }
    ]
}

query looks like :

query ($homeId: ID = null) {
  viewer{
    home(id: $homeId) {
      id
      timeZone
      appNickname
      appAvatar
      size
      type
      numberOfResidents
      primaryHeatingSource
      hasVentilationSystem
      mainFuseSize
    }
  }
}

I would like the builded query to look like(notice there is no parameter value) :

query ($homeId: UUID) {
  viewer{
    home(id: $homeId) {
      id
      timeZone
      appNickname
      appAvatar
      size
      type
      numberOfResidents
      primaryHeatingSource
      hasVentilationSystem
      mainFuseSize
    }
  }
}
Husqvik commented 2 years ago

Thanks. Very valid point. I'll hope to fix it during this week.

Husqvik commented 2 years ago

Hi. I added a new overload new GraphQlQueryParameter<T>("parameterName") without passing explicit value. The GQL type is by default resolved from the generic parameter but can be overridden using optional parameter. I believe it should solve the problem.

Husqvik commented 2 years ago

Would be good if you could test it before publishing new nuget package.

Jeroen-Sturm commented 2 years ago

Yes @Husqvik Thank you that seems to work nicely, so build it ship it 😄

Jeroen-Sturm commented 2 years ago

The only thing now is that : Variable "someString" of type "String" used in position expecting type "String!". Im using it like this : var someStringParameter = new GraphQlQueryParameter<string>("someString");

since this string is not a nullable int I would thing it should be "String!", or how does this works ?

Husqvik commented 2 years ago

There is yet another constructor that has been already there

new GraphQlQueryParameter<string>("someString", default, isNullable: false)

or in the newly added one you can explicitly specify the GraphQL type

new GraphQlQueryParameter<string>("someString", "String!")
Jeroen-Sturm commented 2 years ago

Yes I saw that but I would like another override like so. : new GraphQlQueryParameter<string>("someString", isNullable: false) But now I already added explicitly typed types. so it would be a nice to have not a bug ;-)

Husqvik commented 2 years ago

new GraphQlQueryParameter<string>("someString", isNullable: false) doesn't make sense as without passing the value the parameter must be nullable

Jeroen-Sturm commented 2 years ago

No its not, What if I want to use the variables I spoke about earlier ?

var homeIdParameter = new GraphQlQueryParameter<Guid>("homeId", "UUID!");

var builder =
  new TibberQueryBuilder()
    .WithViewer(
      new ViewerQueryBuilder()
        .WithHome(new HomeQueryBuilder().WithAllScalarFields(), homeIdParameter)
    )
    .WithParameter(homeIdParameter);

var variables = new {
    homeId = "SOME GUID",
};

Then it's possible to add "not nullable" parameters without a value. Because they're set in the variables.

Husqvik commented 2 years ago

Exactly, the value of the parameter is default value. Not actual. For not null parameters (types ending with ! ) the value is entirely omitted as it's required to be passed using the variables object.

Jeroen-Sturm commented 1 year ago

@Husqvik When will this be published to nuget?

Husqvik commented 1 year ago

https://www.nuget.org/packages/GraphQlClientGenerator.Tool/0.9.13

Jeroen-Sturm commented 1 year ago

Can you bump https://www.nuget.org/packages/GraphQlClientGenerator as well ?

Husqvik commented 1 year ago

I knew I forgot something :), sorry. https://www.nuget.org/packages/GraphQlClientGenerator/0.9.13