Global State

Global State lets you define properties on a per-request basis and makes them available to all resolvers and middleware.

Initializing Global State#

Add Global State using the SetProperty method on the OperationRequestBuilder. This method takes a key and a value as arguments. The key must be a string, and the value can be of any type.

Using an interceptor lets you initialize Global State before the request is executed.

C#
public class HttpRequestInterceptor : DefaultHttpRequestInterceptor
{
    public override ValueTask OnCreateAsync(HttpContext context,
        IRequestExecutor requestExecutor, OperationRequestBuilder requestBuilder,
        CancellationToken cancellationToken)
    {
        string userId =
            context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

        requestBuilder.SetProperty("UserId", userId);
        // requestBuilder.SetProperty("IntegerValue", int.Parse(userId));
        // requestBuilder.SetProperty("ObjectValue", new User { Id = userId });

        return base.OnCreateAsync(context, requestExecutor, requestBuilder,
            cancellationToken);
    }
}

Learn more about interceptors

Accessing Global State#

Access Global State in your resolvers as follows.

Next Steps#

Edit this page on GitHub
Last updated on by Tobias Tengler