GraphQLSharp 2.10.5
dotnet add package GraphQLSharp --version 2.10.5
NuGet\Install-Package GraphQLSharp -Version 2.10.5
<PackageReference Include="GraphQLSharp" Version="2.10.5" />
<PackageVersion Include="GraphQLSharp" Version="2.10.5" />
<PackageReference Include="GraphQLSharp" />
paket add GraphQLSharp --version 2.10.5
#r "nuget: GraphQLSharp, 2.10.5"
#:package GraphQLSharp@2.10.5
#addin nuget:?package=GraphQLSharp&version=2.10.5
#tool nuget:?package=GraphQLSharp&version=2.10.5
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 | Versions 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. |
-
net8.0
- Microsoft.CodeAnalysis.CSharp (>= 4.7.0)
- System.Interactive (>= 6.0.1)
-
net9.0
- Microsoft.CodeAnalysis.CSharp (>= 4.7.0)
- System.Interactive (>= 6.0.1)
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 |