0

I'm getting the following error message when testing queries on a test project:

"The feature Authorization is missing the state with the key HotChocolate.Authorization.UserState"

My TestExecutorHelper has the .AddAuthorization() and .AddAuthorizationPermissions() it still doesn't work.

Does anyone know how i can solve this?

Here's my TestExecutor.ExecuteAsync method

IRequestExecutorBuilder requestExecutor = serviceCollection
            .AddAuthorization()
            .AddLogging()
            .AddGraphQL()
            .AddAuthorization()
            .AddAuthorizePermissions()
            .AddErrorFilter<ErrorFilter>()
            .AddQueryType<QueryType>();
var serviceProvider = requestExecutor.Services.BuildServiceProvider();
var request = QueryRequestBuilder.New()
            .SetQuery(query)
            .SetServices(serviceProvider)
            .Create();
var executor = await requestExecutor.BuildRequestExecutorAsync();
return await executor.ExecuteAsync(request);

1 Answers1

1

As I can see, you have this problem because of calling .AddGraphQL() instead of .AddGraphQLServer().

State UserState is added inside DefaultHttpRequestInterceptor, and DefaultHttpRequestInterceptor is registered inside AddGraphQLServer, but not in AddGraphQL

Eugene
  • 169
  • 1
  • 6
  • 2
    Thanks, but it wasn't it, it turns out the issue was the DefaultAuthorizationHandler, in the GetUserState method, due to it being a test project there wasn't a user or claimsprincipal available so it always throws an exception, I solved it by making a custom AuthorizationHandler that adds a claimsprincipal – Miguel Morgado May 10 '23 at 05:01