I have created an GraphQL api with .net core and entity framework and now I want to set up the [Authorize] attribute to some of my methods. Without this attribute it works all fine, I can execute my queries and everything, but as soon as I put [Authorize] on top of any method, the whole schema fails to connect and I cannot run anything at all anymore.
My GraphQL resolver looks like this:
[HotChocolate.Authorization.Authorize(Policy = "Admin")]
public async Task<List<TestModel>> GetAll([Service] DataContext context)
{
return await context.TestModel.ToListAsync();
}
My Program.cs look like this:
builder.Services.AddTransient<AuthService>().AddTransient<CRUDService> ().AddGraphQLServer().AddQueryType<CRUDService>().AddMutationType<AuthService>();
builder.Services.AddAuthorization();
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("Admin", policy => policy.RequireClaim("role", "Admin"));
});
var app = builder.Build();
app.UseAuthentication();
app.MapControllers();
app.UseCors("localhost");
app.MapGraphQL("/graphql");
app.UseMiddleware<AuthorizationMiddleware>();
app.Run();
The error I get "Unable to infer or resolve a schema type from the type reference @AuthorizeDirective."
What should I do, what am I missing and how can I solve this issue?