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
| Parameter | Type | Required | Description |
|---|---|---|---|
services | IServiceCollection | Yes | The service collection |
baseAddress | Uri | Yes | The 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
ArgumentNullExceptionwhenservicesorbaseAddressisnull. - With no chained calls the client introspects
baseAddressfor its schema, usesResponseStrictness.Lenient, sends through anIHttpClientFactoryclient, 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
AddTraxGraphQLClientcall 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 fromUseFileSchemaorUseAssemblySchema.
Registered services
| Service | Lifetime | Default |
|---|---|---|
IGraphQLClientConfiguration | Singleton | Built from the builder's options when first resolved |
ISchemaProvider | Singleton | IntrospectingSchemaProvider, which introspects baseAddress on first use |
IGraphQLClientValidator | Singleton | Validates each query against the schema and caches the result per query text |
IGraphQLClientExecutor | Singleton | Validates, sends and extracts. Resolve this to run requests |
Named HttpClient Trax.Api.GraphQL.Client | IHttpClientFactory | A 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);
}