biegehydra / Postman2CSharp

Postman2CSharp hosts the source code for https://postman2csharp.com, a website for converting Postman collections to C# ApiClients that can be seamlessly exported and imported into your project. Generated ApiClient projects contains no dependencies and highly customizable.

Home Page:https://postman2csharp.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Variables for GQL not creating parameters

comma-piotr opened this issue · comments

It seems that GraphQL Variables does not generate Parameters.

image

It wasn't previously creating a class for the parameters. I just started on a branch "graohql-parameters-class" that will change that. Here's what I'm thinking for the new generated code, let me know what you think. My experience with graph ql is limited so any feedback is valuable.

NEW CODE

public class GraphQLTestApiClient : IGraphQLTestApiClient
{
    private readonly HttpClient _httpClient;
    public GraphQLTestApiClient(HttpClient httpClient)
    {
        _httpClient = httpClient;
        _httpClient.BaseAddress = new Uri($"https://test.com/");
    }

    public async Task<ExampleResponse> Example(ExampleGraphQlVariables graphQlVariables)
    {
        var query = @"
query TestConnection ($limit: Int, $domain: [[ANY]]){
    ResUsers (limit: $limit, domain: $domain, test: ""Hello World"") {
        id,
        name
    }
}";
        var requestPayload = new
        {
            query,
            variables = graphQlVariables
        };
        return await _httpClient.PostJsonAsync<ExampleResponse>($"", requestPayload);
    }
}
// =====GENERATED CLASSESS======
public class ExampleGraphQlVariables 
{
    [JsonPropertyName("limit")]
    public int Limit { get; set; }

    [JsonPropertyName("domain")]
    public List<List<string>> Domain { get; set; }
}
public class ExampleResponse
{
    [JsonPropertyName("test")]
    public string Test { get; set; }
}

OLD CODE

public class GraphQLTestApiClient : IGraphQLTestApiClient
{
    private readonly HttpClient _httpClient;
    public GraphQLTestApiClient(HttpClient httpClient)
    {
        _httpClient = httpClient;
        _httpClient.BaseAddress = new Uri($"https://test.com/");
    }

    public async Task<ExampleResponse> Example()
    {
        var graphQlQuery = @"
        {
        	\"query\": \"query TestConnection ($limit: Int, $domain: [[ANY]]){\n    ResUsers (limit: $limit, domain: $domain, test: \"Hello World\") {\n        id,\n        name\n    }\n}\",
        	\"variables\": {\n    \"limit\" : 10,\n    \"domain\" : [[\"name\", \"=\", \"username\"]]\n}
        }";
        var httpContent = new StringContent(graphQlQuery, Encoding.UTF8, "application/json");
        return await _httpClient.PostFromJsonAsync<ExampleResponse>($"", httpContent);
    }
}
// =====GENERATED CLASSESS======
public class ExampleResponse
{
    [JsonPropertyName("test")]
    public string Test { get; set; }
}

Tested, worked fine.

@comma-piotr Just fyi, I plan on creating a GraphQlRequest class that will be used instead of the anonymous type before merging and republishing.