Reference

IGraphQLClientExecutor

Package Trax.Api.GraphQL.ClientNamespace Trax.Api.GraphQL.ClientSource IGraphQLClientExecutor.cs, GraphQLClientExecutor.cs

Runs an IGraphQLClientRequest: validates its query, sends it and reads the result. GraphQLClientExecutor is the implementation AddTraxGraphQLClient registers as a singleton (keyed, for AddKeyedTraxGraphQLClient).

Signature

public interface IGraphQLClientExecutor
{
    Task<TReturn> Run<TReturn>(
        IGraphQLClientRequest<TReturn> request,
        CancellationToken cancellationToken = default);
}
 
public class GraphQLClientExecutor : IGraphQLClientExecutor
{
    public GraphQLClientExecutor(
        IGraphQLClientValidator validator,
        IGraphQLClientConfiguration configuration,
        ILogger<GraphQLClientExecutor>? logger = null);
}

Returns

The result of the request's Extract.

Behavior

Run:

  1. Throws ArgumentNullException for a null request, and OperationCanceledException when the token is already cancelled.
  2. Validates Query with the client's validator. The first validation of a query text loads the schema when needed; later ones hit the cache.
  3. Sends the request as a query or a mutation, according to the operation, through the configuration's GraphQLHttpClient.
  4. Throws when the server returned errors or no data.
  5. Checks the response shape when the client's strictness is not Lenient and the request's UsesDefaultExtractor is true. Drift is logged at Warning under ILogger<GraphQLClientExecutor> for WarnOnDrift.
  6. Returns request.Extract(data, options).
ExceptionWhen
GraphQLValidationExceptionThe query does not validate, has no operation or more than one, or is a subscription against a schema without a subscription type (the default). Nothing is sent
GraphQLSchemaIntrospectionExceptionThe schema could not be loaded
GraphQLExecutionExceptionThe server returned GraphQL errors (Errors), the response had no data, or Extract produced null (Errors is empty)
JsonExceptionThe default extractor could not read the data as TReturn. Not wrapped
GraphQLResponseShapeExceptionThe response shape differs from TReturn under ThrowOnDrift
NotSupportedExceptionThe operation is a subscription and the schema kept its subscription type (RemoveSubscriptionsFromSchema = false)

Transport failures surface as the exceptions GraphQL.Client and HttpClient throw.

Example

public class PlayerLookup(IGraphQLClientExecutor graphql)
{
    public async Task<PlayerProfile?> Find(string id, CancellationToken ct)
    {
        try
        {
            return await graphql.Run(new GetPlayerRequest { Id = id }, ct);
        }
        catch (GraphQLExecutionException)
        {
            return null;
        }
    }
}

See also