GraphQl.QueryBuilder 1.1.0

.NET 5.0
Install-Package GraphQl.QueryBuilder -Version 1.1.0
dotnet add package GraphQl.QueryBuilder --version 1.1.0
<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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: GraphQl.QueryBuilder, 1.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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 Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
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 183 4/10/2021
1.0.1 162 4/5/2021