GraphQLSharp 2.10.5

dotnet add package GraphQLSharp --version 2.10.5
                    
NuGet\Install-Package GraphQLSharp -Version 2.10.5
                    
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.10.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GraphQLSharp" Version="2.10.5" />
                    
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.10.5
                    
#r "nuget: GraphQLSharp, 2.10.5"
                    
#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.
#:package GraphQLSharp@2.10.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GraphQLSharp&version=2.10.5
                    
Install as a Cake Addin
#tool nuget:?package=GraphQLSharp&version=2.10.5
                    
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);

Querying polymorphic types

When querying polymorphic types and deserializing to .NET types generated by the GraphQLTypeGenerator, you must ensure that the __typename field is specified and is positioned as the first field in the response. __typename__ is needed to hint what .NET type should be used for deserialization.

Good:

{
    search(text: "an") {
        __typename
        ... on Human {
            name
        }
        ... on Droid {
            name
        }
        ... on Starship {
            name
        }
    }
}

Bad (missing __typename):

{
    search(text: "an") {
        ... on Human {
            name
        }
        ... on Droid {
            name
        }
        ... on Starship {
            name
        }
    }
}

Bad (__typename is not the first field):

{
    search(text: "an") {
        id
        __typename
        ... on Human {
            name
        }
        ... on Droid {
            name
        }
        ... on Starship {
            name
        }
    }
}
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.10.5 57 8/1/2025
2.10.4 63 8/1/2025
2.10.3 78 8/1/2025
2.10.2 78 7/30/2025
2.10.1 77 7/29/2025
2.9.8 80 7/29/2025
2.9.7 79 7/29/2025
2.9.6 78 7/29/2025
2.9.5 81 7/29/2025
2.9.4 81 7/28/2025
2.9.3 77 7/28/2025
2.9.2 78 7/28/2025
2.9.1 423 7/25/2025
2.9.0 422 7/25/2025
2.8.0 441 7/24/2025
2.5.0 145 6/25/2025
2.4.0 139 6/25/2025
2.3.0 338 6/19/2025
2.2.0 408 5/22/2025
2.1.0 235 5/12/2025
2.0.0 214 5/12/2025
1.0.1 741 6/26/2019
1.0.0 589 6/25/2019