Mt.GraphQL.Api.Client 1.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Mt.GraphQL.Api.Client --version 1.3.0
                    
NuGet\Install-Package Mt.GraphQL.Api.Client -Version 1.3.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="Mt.GraphQL.Api.Client" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mt.GraphQL.Api.Client" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Mt.GraphQL.Api.Client" />
                    
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 Mt.GraphQL.Api.Client --version 1.3.0
                    
#r "nuget: Mt.GraphQL.Api.Client, 1.3.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.
#:package Mt.GraphQL.Api.Client@1.3.0
                    
#: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=Mt.GraphQL.Api.Client&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Mt.GraphQL.Api.Client&version=1.3.0
                    
Install as a Cake Tool

Introduction

Using GraphQL queries, the client of your API can control how entities are returned by applying custom filtering, sorting, paging and/or selecting just one or a few of the entity's properties, all specified in the request. The Mt.GraphQL.Api.Client package allows developers to call a Mt.GraphQL enabled API using intuitive Linq-like expressions.

Use the Mt.GraphQL.Api.Server package and check it's README to set up the server.

Setting up a client class

Set up the client class by deriving from the ClientBase class. By default, the client reads an ApiUrl, ApiKey and ApiKeyHeaderName setting from the environment variables. A configuration name can be specified to change this to <configurationname>:ApiUrl, or a custom Configuration object can be created. The default ApiKeyHeaderName is 'Api_Key'. If a different kind of authentication is required, configuration member CreateHttpRequestMessageHandler can be set to create a custom HTTP request. Configuration member ProcessRequestHandler can be set to create custom request handling.

The CreateQuery() method is used to create a query on a client entity. The endpoint name will be the name of the model class by default, but this can be overridden using argument entity. When the client model contains less fields than the server model, you can set argument restrictToModel to true to make sure only the necessary properties are returned.

use Mt.GraphQL.Api;

class GraphQlClient : ClientBase
{
    public ClientQuery<Contact> Contacts => CreateQuery<Contact>();
}

Retrieving the data

You retrieve data in an extended array class which will also contain the query parameters. The number of results could be limited by the server configuration. In that case you need to use paging (see below) to retrieve more than one page of data.

var client = new GraphQlClient();
var data = await client.Contacts
    .ToArrayAsync(); // query will be: /Contact

Selecting a part of the model

You can select only the data needed for your application, instead of the complete model. You can also select fields of related entities. The result type will be an array of the specified anonymous type.

var client = new GraphQlClient();
var data = await client.Contacts
    .Select(x => new { x.Id, x.Name, x.VisitAddress.ZipCode })
    .ToArrayAsync(); // query will be: /Contact?select=Id,Name,VisitAddress.ZipCode

Filtering

You can filter data on properties which are set up for filtering and sorting on the server by adding a Where clause.

var client = new GraphQlClient();
var data = await client.Contacts
    .Where(x => x.ModifiedDate >= new DateTime(2023, 3, 29))
    .ToArrayAsync(); // query will be: /Contact?filter=ModifiedDate ge '2023-03-29'

More examples of filters are:

  • x => x.Id <= 5
  • x => x.Date > new DateTime(2023, 3, 29)
  • x => x.Name == "A"
  • x => x.Description.StartsWith("ABC")
  • x => !x.IsCustomer
  • x => new []{ 1, 2, 3 }.Contains(x.Id)
  • x => x.Id == _id || x.Id == 4 || _ids.Contains(x.Id)
  • x => (x.Id > _id && x.Description.Contains("A")) || x.Description.EndsWith("B")

Sorting

You can sort data on properties which are set up for filtering and sorting on the server by adding one or more OrderBy or OrderByDescending clauses.

var client = new GraphQlClient();
var data = await client.Contacts
    .OrderBy(x => x.LastName)
    .OrderBy(x => x.FirstName)
    .ToArrayAsync(); // query will be: /Contact?orderBy=LastName,FirstName

Paging

You can page your results using Skip() and Take().

Counting

You can count the entities by calling CountAsync().

Extensions

Server side, navigation properties of entities can be marked as Extensions. This means that those navigation properties will not be returned with the entity by default. If you do want these properties included, you can specify this by calling Extend(). This will also tell the server which properties the client type has, so nothing is returned unnecessarily.

var client = new GraphQlClient();
var data = await client.Contacts
    .Extend(x => x.VisitAddress)
    .ToArrayAsync(); // query will be /Contact?extend=VisitAddress(Street,HouseNumber,Zipcode,City)
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.0 117 12/18/2024
1.3.0 134 12/13/2024
1.2.8 145 7/10/2024
1.2.7 152 3/1/2024
1.2.6 140 3/1/2024
1.2.5 144 2/29/2024
1.2.4 147 2/28/2024
1.2.3 142 2/26/2024
1.2.2 149 2/22/2024
1.2.1 146 2/20/2024
1.2.0 154 2/15/2024
1.1.0 188 1/9/2024
1.0.1 160 1/5/2024