AddKeyedTraxGraphQLClient
Package Trax.Api.GraphQL.ClientNamespace Trax.Api.GraphQL.ClientSource ServiceExtensions.cs
Registers a GraphQL client under a DI service key, so clients for different servers live in one
container. Each key gets its own configuration, HttpClient, schema provider and validator cache.
Declared on the static class ServiceExtensions.
Signature
public static TraxGraphQLClientBuilder AddKeyedTraxGraphQLClient(
this IServiceCollection services,
object serviceKey,
Uri baseAddress)Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
services | IServiceCollection | Yes | The service collection |
serviceKey | object | Yes | The key that names the downstream server, usually a string or an enum value. Compared with Equals |
baseAddress | Uri | Yes | The GraphQL endpoint for this key |
Returns
TraxGraphQLClientBuilder. Every builder method applies to this key's registration only.
Behavior
- Throws
ArgumentNullExceptionwhen any argument isnull. - Registers the same four services as
AddTraxGraphQLClient,
each as a keyed singleton under
serviceKey. Each keyed service resolves its own dependencies by the same key, so a keyed executor validates against its own server's schema. - The
IHttpClientFactoryclient is named after the key's type and value (Trax.Api.GraphQL.Client:<key type>:<key>), so the string key"Billing"and an enum valueBillingget different clients. - Validation for a keyed client (
UseStartupValidationon its builder, or the keyed overload of ValidateGraphQLClientAssembliesAsync) checks only the request types marked[GraphQLClient(key)]with its key. The unkeyed client's validation checks only the unmarked ones. - A keyed and an unkeyed client can be registered side by side. Two calls with the same key behave
like two
AddTraxGraphQLClientcalls: the later registration wins. - Resolve without attributes with
GetRequiredKeyedService<IGraphQLClientExecutor>(key).
Example
services.AddKeyedTraxGraphQLClient("billing", new Uri("https://billing.example.com/graphql"))
.UseFileSchema("billing.graphql")
.UseStartupValidation(typeof(Program).Assembly);
services.AddKeyedTraxGraphQLClient("shipping", new Uri("https://shipping.example.com/graphql"))
.UseFileSchema("shipping.graphql")
.UseStartupValidation(typeof(Program).Assembly);
public class OrderSync(
[FromKeyedServices("billing")] IGraphQLClientExecutor billing,
[FromKeyedServices("shipping")] IGraphQLClientExecutor shipping)
{
public async Task<(Invoice, Shipment)> Load(string invoiceId, string orderId, CancellationToken ct) =>
(
await billing.Run(new GetInvoiceRequest { Id = invoiceId }, ct),
await shipping.Run(new GetShipmentRequest { OrderId = orderId }, ct)
);
}