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" />
<PackageReference Include="GraphQLSharp" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=GraphQLSharp&version=2.5.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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 | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
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.