我在 .NET Core 3.1 中有這樣的 graphql 查詢:
public class CommentQuery : ObjectGraphType
{
public CommentQuery(CommentRepository commentRepository, UserInfo userInfo)
{
Field<ListGraphType<CommentType>>(
"contentComments",
arguments: new QueryArguments(new QueryArgument<StringGraphType> {Name = "contentItemId"}),
resolve: x =>
{
var contentItemId = x.GetArgument<string>("contentItemId");
return Task.Run(() =>
commentRepository.ListAsync(
Builders<Comment>.Filter.Where(p => p.ContentItemId == contentItemId))).Result;
});
Field<ListGraphType<CommentType>>(
name: "Comments",
arguments: new QueryArguments(new
QueryArgument<StringGraphType> { Name = "contentItemId" }),
resolve: x =>
{
var commentId = x.GetArgument<string>("commentId");
return Task.Run(() =>
commentRepository.ListAsync(
Builders<Comment>.Filter.Where(p => p.ContentItemId == commentId))).Result;
}
);
}
}
我需要在沒有身份驗證的情況下呼叫此查詢。默認情況下,我在 startup.cs 中為所有 api 啟用身份驗證
services.AddAuthentication(...)
uj5u.com熱心網友回復:
經過搜索和嘗試,我找到了解決方案:在 Configure startup.cs 方法中添加一些行以允許這樣的特定路徑:
app.UseEndpoints(e =>
{
e.MapHealthChecks(BasePath "/customers/comments/graphql", new HealthCheckOptions()
{
Predicate = (_) => false
})
.WithMetadata(new AllowAnonymousAttribute());
});
它對我有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/395723.html
