Reference

TraxGraphQLClientBuilder

Package Trax.Api.GraphQL.ClientNamespace Trax.Api.GraphQL.ClientSource Builder.cs, Builder.Schema.cs, Builder.Options.cs

The fluent builder returned by AddTraxGraphQLClient and AddKeyedTraxGraphQLClient. Each method changes the registration in place and returns the builder, so the registration is consistent with the calls made so far. On a keyed builder every method applies to that key only. Trax.Api.GraphQL.Client.Trax adds UseAssemblySchema and UseStartupValidation as extension methods.

Signature

public sealed partial class TraxGraphQLClientBuilder
{
    public IHttpClientBuilder HttpClientBuilder { get; }
    public IServiceCollection Services { get; }
 
    public TraxGraphQLClientBuilder UseIntrospection();
    public TraxGraphQLClientBuilder UseFileSchema(string sdlPath);
    public TraxGraphQLClientBuilder WithStrictness(ResponseStrictness strictness);
    public TraxGraphQLClientBuilder ConfigureHttpClient(HttpClient httpClient);
    public TraxGraphQLClientBuilder DisposeHttpClient(bool dispose = true);
    public TraxGraphQLClientBuilder ConfigureJson(JsonSerializerOptions options);
    public TraxGraphQLClientBuilder Configure(Action<GraphQLClientConfigurationBuilder> mutate);
}

Members

MemberDescription
HttpClientBuilderThe named IHttpClientFactory client this GraphQL client sends through, one per client. Add handlers, resilience or timeouts to it the standard way. Its primary handler is a SocketsHttpHandler whose pooled connections are replaced every two minutes. Not used once ConfigureHttpClient has supplied a client
ServicesThe IServiceCollection the client was registered against
UseIntrospection()Validate against the schema the endpoint reports by introspection. The default
UseFileSchema(sdlPath)Validate against an SDL file
WithStrictness(strictness)How strictly responses are checked
ConfigureHttpClient(httpClient)Send through an HttpClient you own
DisposeHttpClient(dispose)Dispose that HttpClient with the client
ConfigureJson(options)Replace the options responses are deserialized with
Configure(mutate)Set any option on the underlying GraphQLClientConfigurationBuilder

UseIntrospection

Replaces the schema provider with IntrospectingSchemaProvider, which sends a standard introspection query to the endpoint. It is the provider registered when no schema method is called. The schema methods (UseIntrospection, UseFileSchema, UseAssemblySchema) replace one another: the last one called decides. A Trax server answers introspection only in Development unless its host admits the client with AllowIntrospection. See ISchemaProvider.

UseFileSchema

ParameterTypeDescription
sdlPathstringAn absolute path, or one relative to the process's working directory, of an SDL file such as schema.graphql

Replaces the schema provider with FileSchemaProvider. The file is read when the schema is first requested, not at registration. A missing, empty or invalid file throws GraphQLSchemaIntrospectionException from the validation that asked for it. Throws ArgumentException when sdlPath is null, empty or whitespace.

WithStrictness

Sets ResponseStrictness, how the executor treats a response whose fields differ from the response type's properties. Default Lenient. See ResponseStrictness.

ConfigureHttpClient

Sends requests and introspection through httpClient instead of the IHttpClientFactory client. The client is not changed: its BaseAddress is neither read nor set, and requests go to the URI the client was registered with, so one HttpClient can serve several GraphQL clients. It is not disposed with the client unless DisposeHttpClient() is called. Throws ArgumentNullException when httpClient is null.

DisposeHttpClient

ParameterTypeDefaultDescription
disposebooltrueWhether disposing the client's configuration also disposes the HttpClient passed to ConfigureHttpClient

Without the call, only the GraphQL.Client wrapper around the supplied HttpClient is disposed.

ConfigureJson

Replaces the JsonSerializerOptions responses are deserialized with. The supplied options replace the defaults (case-insensitive property names, a JsonStringEnumConverter with JsonNamingPolicy.SnakeCaseUpper, a DateOnly converter) rather than adding to them. Response-shape checking matches JSON fields to properties ignoring case and names properties through PropertyNamingPolicy when one is set. Throws ArgumentNullException when options is null.

Configure

Runs mutate immediately against the client's GraphQLClientConfigurationBuilder, for options with no method of their own: GraphQLClientOptions, WebsocketJsonSerializer and RemoveSubscriptionsFromSchema. Its properties are listed on IGraphQLClientConfiguration. Throws ArgumentNullException when mutate is null.

Examples

services
    .AddTraxGraphQLClient(new Uri("https://api.example.com/graphql"))
    .UseFileSchema("schema.graphql")
    .WithStrictness(ResponseStrictness.WarnOnDrift)
    .HttpClientBuilder.AddHttpMessageHandler(() => new AuthHeaderHandler());
using System.Text.Json;
using System.Text.Json.Serialization;
 
var shared = new HttpClient { Timeout = TimeSpan.FromSeconds(10) };
 
services
    .AddTraxGraphQLClient(new Uri("https://api.example.com/graphql"))
    .ConfigureHttpClient(shared)
    .ConfigureJson(new JsonSerializerOptions
    {
        PropertyNameCaseInsensitive = true,
        Converters = { new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseUpper) },
        NumberHandling = JsonNumberHandling.AllowReadingFromString,
    })
    .Configure(o => o.RemoveSubscriptionsFromSchema = false);

See also