GraphQl.QueryBuilder 1.1.0

dotnet add package GraphQl.QueryBuilder --version 1.1.0                
NuGet\Install-Package GraphQl.QueryBuilder -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GraphQl.QueryBuilder" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GraphQl.QueryBuilder --version 1.1.0                
#r "nuget: GraphQl.QueryBuilder, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GraphQl.QueryBuilder as a Cake Addin
#addin nuget:?package=GraphQl.QueryBuilder&version=1.1.0

// Install GraphQl.QueryBuilder as a Cake Tool
#tool nuget:?package=GraphQl.QueryBuilder&version=1.1.0                

graphql-client

GraphQL query generator for .NET

Allows you to automatically generate GraphQL queries from C# variables and types

Usage

Instantiate a GraphQlQueryBuilder from the GraphQlClient namespace (you can also use the IGraphQlQueryBuilder interface for service pattern and to support mocking)

using GraphQlClient;

class User //Example query type
{
    public int Id;
    public string Name;
}

IGraphQlQueryBuilder queryBuilder = new GraphQlQueryBuilder();
var query = queryBuilder.Build<User>();

//query.GetQuery() is the string "{id name}"

Supports anonymous types for inline definitions (this is particularly useful when writing tests)

Example 1

var request = queryBuilder.Build
(
    new 
    {
        User = new 
        {
            Id = default(int),
            Name = default(string)
        }
    }
);

Example 2

var expectedResult = new 
{
    Users = new [] 
    {
        new 
        {
            Name = "Tom"
        },
        new 
        {
            Name = "Harry"
        }
    }
};

var query = queryBuilder.Build(expectedResult);

var actual = await SendRequest(query.GetQuery());

// assert expected equal to actual

Fluent interface for parameters and field aliases


class UserQuery
{
    public User User;
}

public async Task<User> GetUser(int userId)
{
    var query = queryBuilder.Build<UserQuery>();

    query
        .Field(q => q.User)
        .AddParameter("id", userId);

    return (await SendRequest<UserQuery>(query.GetQuery())).User;
}

public async Task<User> GetTom()
{
    var tomShape = new { Tom = default(User)};
    var query = queryBuilder.Build();

    query.Field(q => q.Tom)
            .IsAliasFor("user")
            .AddParameter("name", "Tom");

    //query.GetQuery() is the string: {tom:user(name:"Tom"){ ... }}

    return (await SendRequest(query.GetQuery(), shape: tomShape)).Tom;
}

Dependencies

Written in .NET 5, Newtonsoft.JSON used for serialising parameters, in future the serialisation will be exposed for more flexibility

Uses xUnit and FluentAssertions for the test suite

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 479 4/10/2021
1.0.1 327 4/5/2021