0

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?

  • You also can share the documentation you referred. I am willing to help you investigate the issue. – Jason Pan Apr 10 '23 at 13:58
  • Thank you for you answer, I referred [this link](https://www.youtube.com/watch?v=XtdUExto_24) but I ran up into this issue and now the whole Schema is failing to connect. – questioner9928 Apr 10 '23 at 14:14

1 Answers1

0

The issue in your code is you are using the wrong middleware order.

The correct order should be like below.

var app = builder.Build();
app.UseCors("localhost");
app.UseMiddleware<AuthorizationMiddleware>();

app.UseAuthentication();
    
app.MapControllers();
app.MapGraphQL("/graphql");
app.Run();

If the code above not works for you, please check the repo.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
  • Thank you for you answer. The code inside that github repo is almost 90% the same like mine. I copy-pasted this code you posted here into my Program.cs file but it's still the same :(. – questioner9928 Apr 10 '23 at 14:19
  • @questioner9928 if you want I am willing to upgrade this repo to .net7. Hope it could help you. – Jason Pan Apr 10 '23 at 16:17
  • I am using .net6 too but I don't think the version of the .net is causing any trouble. It's either the middleware order or I am missing something else. Also I think it's worth to mention that I have an .net web Api + Graphql, not web app* +graphql. So all my middleware lines are in Program.cs and not in Startu.cs. Can I contact you personally in Discord/Twitter/Gmail chat or anywehere? – questioner9928 Apr 10 '23 at 21:19
  • @questioner9928 I mean I could refer this repo and create sample with same env/version for you. – Jason Pan Apr 10 '23 at 23:17
  • 1
    Actually I fixed it. It was an dumb mistake that I made while creating the GraphQLServer. But now I am having an error with the roles... Even though the logged in user has the right role, the method with the `[Authorize(Roles = new[] { "Admin" })]` attribute refuses the user to access it. – questioner9928 Apr 11 '23 at 13:46