Reference

AddTraxGraphQLClient

Package Trax.Api.GraphQL.ClientNamespace Trax.Api.GraphQL.ClientSource ServiceExtensions.cs

Registers a GraphQL client against one endpoint and returns a TraxGraphQLClientBuilder for configuring it. Declared on the static class ServiceExtensions.

Signature

public static TraxGraphQLClientBuilder AddTraxGraphQLClient(
    this IServiceCollection services,
    Uri baseAddress)

Parameters

ParameterTypeRequiredDescription
servicesIServiceCollectionYesThe service collection
baseAddressUriYesThe GraphQL endpoint every request is sent to, for example https://api.example.com/graphql

Returns

TraxGraphQLClientBuilder. The registration is complete when this returns; chaining is optional.

Behavior

  • Throws ArgumentNullException when services or baseAddress is null.
  • With no chained calls the client introspects baseAddress for its schema, uses ResponseStrictness.Lenient, sends through an IHttpClientFactory client, and deserializes with the default JSON options listed on IGraphQLClientConfiguration.
  • The configuration is built the first time it is resolved, so builder calls made before that take effect and calls made after it do not.
  • A second AddTraxGraphQLClient call does not add a second client: its registrations come later and win. A client per server is AddKeyedTraxGraphQLClient.
  • The executor is a singleton and safe to use concurrently.
  • A Trax server outside Development refuses introspection unless its host admits the client with AllowIntrospection, so against one the schema comes from UseFileSchema or UseAssemblySchema.

Registered services

ServiceLifetimeDefault
IGraphQLClientConfigurationSingletonBuilt from the builder's options when first resolved
ISchemaProviderSingletonIntrospectingSchemaProvider, which introspects baseAddress on first use
IGraphQLClientValidatorSingletonValidates each query against the schema and caches the result per query text
IGraphQLClientExecutorSingletonValidates, sends and extracts. Resolve this to run requests
Named HttpClient Trax.Api.GraphQL.ClientIHttpClientFactoryA SocketsHttpHandler whose pooled connections are replaced every two minutes, with an infinite handler lifetime. Reachable through TraxGraphQLClientBuilder.HttpClientBuilder

Example

builder.Services
    .AddTraxGraphQLClient(new Uri("https://api.example.com/graphql"))
    .UseFileSchema("schema.graphql")
    .WithStrictness(ResponseStrictness.ThrowOnDrift);
 
public class PlayerLookup(IGraphQLClientExecutor graphql)
{
    public Task<PlayerProfile> Get(string id, CancellationToken ct) =>
        graphql.Run(new GetPlayerRequest { Id = id }, ct);
}

See also