GraphQLSharp 2.5.0

dotnet add package GraphQLSharp --version 2.5.0
                    
NuGet\Install-Package GraphQLSharp -Version 2.5.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="GraphQLSharp" Version="2.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GraphQLSharp" Version="2.5.0" />
                    
Directory.Packages.props
<PackageReference Include="GraphQLSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GraphQLSharp --version 2.5.0
                    
#r "nuget: GraphQLSharp, 2.5.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.
#addin nuget:?package=GraphQLSharp&version=2.5.0
                    
Install as a Cake Addin
#tool nuget:?package=GraphQLSharp&version=2.5.0
                    
Install as a Cake Tool

Build NuGet

GraphQLSharp is a modern .NET library to work with GraphQL.

GraphClient - A modern and performant GraphQL client

Examples:


private GraphQLClient _client;

[TestInitialize]
public void Initialize()
{
    _client = new GraphQLClient(new GraphQLRequestOptions
    {
        Uri = new Uri($"https://example.com/graphql.json"),
        ThrowOnGraphQLErrors = true,
        //HttpClient = myHttpClient, optionally provide your own HttpClient instance
        //Interceptor = myInterceptor, optionally provide an interceptor for logging, automatic retires, etc...
        ConfigureHttpRequestHeaders = headers =>
        {
            headers.Add("X-Access-Token", "<INSERT_TOKEN>");
        },
    });
}

[TestMethod]
public async Task RequestSimple()
{
    var query = """
        query {
            products(first: 10)
            {
                nodes
                {
                    id
                    title
                }
            }
        }
        """;

    //response is strongly typed
    var response = await _client.ExecuteAsync<QueryRoot>(query);
    Assert.IsNotNull(response.data.products.nodes.FirstOrDefault()?.id);
}


[TestMethod]
public async Task RequestSimpleWithVariables()
{
    var query = """
        query ($first: Int!){
            products(first: $first)
            {
                nodes
                {
                    id
                    title
                }
            }
        }
        """;

    var request = new GraphQLRequest
    {
        query = query,
        variables = new Dictionary<string, object>
        {
            { "first", 10 }
        }
    };

    //response is strongly typed
    var response = await _client.ExecuteAsync<QueryRoot>(request);
    Assert.IsNotNull(response.data.products.nodes.FirstOrDefault()?.id);
}

[TestMethod]
public async Task RequestWithMultipleOperations()
{
    var query = """
        query myQuery($first: Int!) {
            products(first: $first)
            {
                nodes
                {
                    id
                    title
                }
            }
        }
        query myQuery2 {
            products(first: 10)
            {
                nodes
                {
                    id
                    title
                }
            }
        }
        """;

    var request = new GraphQLRequest
    {
        query = query,
        operationName = "myQuery",
        variables = new Dictionary<string, object>
        {
            { "first", 10 }
        }
    };

    var response = await _client.ExecuteAsync<QueryRoot>(request);
    Assert.IsNotNull(response.data.products.nodes.FirstOrDefault()?.id);
}

[TestMethod]
public async Task RequestWithAliases()
{
    var query = """
        query ($first: Int!) {
            myProducts: products(first: $first)
            {
                nodes
                {
                    id
                    title
                }
            }
            myOrders: orders(first: $first)
            {
                nodes
                {
                    id
                    name
                }
            }
        }
        """;

    var request = new GraphQLRequest
    {
        query = query,
        variables = new Dictionary<string, object>
        {
            { "first", 10 }
        }
    };

    var response = await _client.ExecuteAsync(request);
    //response.data is JsonElement
    var myProducts = response.data.GetProperty("myProducts")
                                    .Deserialize<ProductConnection>(Serializer.Options);
    var myOrders = response.data.GetProperty("myOrders")
                                    .Deserialize<OrderConnection>(Serializer.Options);
    Assert.IsNotNull(myProducts.nodes.FirstOrDefault()?.title);
    Assert.IsNotNull(myOrders.nodes.FirstOrDefault()?.name);
}

Note: GraphClient is thread-safe and can be used as a long lived shared instance.

GraphQLTypeGenerator - Generate .NET types from any GraphQL endpoint

async Task<JsonDocument> SendGraphQLQueryAsync(string graphqlQuery)
{
  //Call your GraphQL endpoint and return the result of the given introspection query
}
var generator = new GraphQLTypeGenerator();
var options = new GraphQLTypeGeneratorOptions
{
    Namespace = "MyNamespace",
    ScalarTypeNameToDotNetTypeName = new Dictionary<string, string>
        {
            { "UnsignedInt64", "ulong" },
            { "Money", "decimal" }
        },
    EnumMembersAsString = true
};
var csharpCode = generator.GenerateTypesAsync(options, async introspectionQuery => await SendGraphQLQueryAsync(introspectionQuery));
File.WriteAllText("types.cs", csharpCode);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on GraphQLSharp:

Package Downloads
ShopifyNet

.NET utilities for Shopify GraphQL

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.5.0 131 6/25/2025
2.4.0 131 6/25/2025
2.3.0 277 6/19/2025
2.2.0 399 5/22/2025
2.1.0 232 5/12/2025
2.0.0 211 5/12/2025
1.0.1 729 6/26/2019
1.0.0 577 6/25/2019