Mt.GraphQL.Api.Client
1.1.0
See the version list below for details.
dotnet add package Mt.GraphQL.Api.Client --version 1.1.0
NuGet\Install-Package Mt.GraphQL.Api.Client -Version 1.1.0
<PackageReference Include="Mt.GraphQL.Api.Client" Version="1.1.0" />
<PackageVersion Include="Mt.GraphQL.Api.Client" Version="1.1.0" />
<PackageReference Include="Mt.GraphQL.Api.Client" />
paket add Mt.GraphQL.Api.Client --version 1.1.0
#r "nuget: Mt.GraphQL.Api.Client, 1.1.0"
#:package Mt.GraphQL.Api.Client@1.1.0
#addin nuget:?package=Mt.GraphQL.Api.Client&version=1.1.0
#tool nuget:?package=Mt.GraphQL.Api.Client&version=1.1.0
Introduction
Mt.GraphQL contains libraries for exposing and querying APIs using GraphQL. Client side, a request can be built using Linq-like expressions. Server-side, the query can be applied directly to IQueryables<>
from EntityFramework.
Client
For the client side, package Mt.GraphQL.Api.Client is used. A client class can be created to expose queries on entities. Using those queries, you can partially select the entity, filter it, sort it and apply paging. Results can be received in arrays, lists or as an int by calling CountAsync()
.
class GraphQlClient : ClientBase
{
public ClientQuery<Contact> Contacts => CreateQuery<Contact>();
}
var client = new GraphQlClient();
var result = await client.Contacts
.Select(x => new { x.Id, x.Name, x.DateOfBirth })
.Where(x => x.Name.StartsWith("Contact 1"))
.OrderByDescending(x => x.Name)
.Skip(1)
.ToArrayAsync();
// Uses query string: ?select=Id,Name,DateOfBirth&filter=startsWith(Name,'Contact 1')&orderBy=Id desc&skip=1
Server
For the server side, package Mt.GraphQL.Api.Server is used. To receive the GraphQL query, a generic Query class is used as an argument for the GET methods. This query can be applied directly to a IQueryable<>
coming from EntityFramework's data context, or a hard-coded filter can be applied to the IQueryable<>
first.
ASP.Net Core
[ApiController, Route("Contact")]
public class ContactController : ControllerBase
{
[HttpGet]
public ActionResult Get([FromQuery] Query<Contact> query)
{
// This part is only needed when the controller has no ApiController attribute.
if (!ModelState.IsValid)
return ModelState.ToBadRequest(ControllerContext);
try
{
using var context = new DbContext();
return Ok(context.Contacts.Apply(query));
}
catch (QueryException ex)
{
return BadRequest(ex.Message);
}
}
}
.NET Framework MVC 5
[HttpGet, Route("Contact")]
public ActionResult GetContacts(Query<Contact> query)
{
if (!ModelState.IsValid)
return ModelState.ToBadRequest(ControllerContext);
try
{
using (var context = new DbContext())
{
return Json(context.Contacts.Apply(query));
}
}
catch (QueryException ex)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Content(ex.Message);
}
}
.NET Framework API
[HttpGet, Route("Contact")]
public IHttpActionResult GetContacts([FromUri]Query<Contact> query)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
try
{
using (var context = new DbContext())
{
return Ok(context.Contacts.Apply(query));
}
}
catch (QueryException ex)
{
return BadRequest(ex.Message);
}
}
Server configuration
To customize the way entities can be queried, they can be configured in the startup of the API. You can configure
- the maximum page size, globally or per entity
- which columns can be used for fitering and sorting (to avoid poorly performing queries which filter on columns that aren't indexed)
- which serialization attributes should be applied to properties
- which properties should be hidden.
GraphqlConfiguration.DefaultMaxPageSize = 200;
GraphqlConfiguration.ConfigureBase<ModelBase>()
.AllowFilteringAndSorting(x => x.Id);
GraphqlConfiguration.Configure<Customer>()
.AllowFilteringAndSorting(x => x.Name)
.ExcludeProperty(x => x.Contacts.First().Customer)
.ExcludeProperty(x => x.Contacts.First().Customer_Id);
GraphqlConfiguration.Configure<Contact>()
.AllowFilteringAndSorting(x => x.Name)
.AllowFilteringAndSorting(x => x.Customer_Id)
.ApplyAttribute(
x => x.DateOfBirth,
() => new JsonDateTimeConverterAttribute { Format = "yyyy-MM-dd" })
.ExcludeProperty(x => x.Customer_Id)
.ExcludeProperty(x => x.Customer.Contacts);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.