我正在将我的 GraphQL 版本从 3.1.3
升级到 4.2.0
。当我编译我的代码时,我看到以下三个错误 -
找不到类型或命名空间名称“EnterLeaveListener”(您是否缺少 using 指令或程序集引用?)
“IServiceProvider”不包含“GetRequiredService”的定义,并且找不到接受“IServiceProvider”类型的第一个参数的可访问扩展方法“GetRequiredService”(您是否缺少 using 指令或程序集引用?)
>“ExecutionOptions”不包含“FieldMiddleware”的定义,并且无法找到接受“ExecutionOptions”类型的第一个参数的可访问扩展方法“FieldMiddleware”(您是否缺少 using 指令或程序集引用?)>
这是我的 InputValidationRule
类,其中使用了 EnterLeaveListener
类:
public class InputValidationRule : IValidationRule
{
public Task<INodeVisitor> ValidateAsync(ValidationContext context)
{
return Task.FromResult((INodeVisitor)new EnterLeaveListener(_ =>
{
}));
return Task.FromResult((INodeVisitor)) new
}
}
这是我的 GraphQLMiddleware
课:
using GraphQL;
using GraphQL.Instrumentation;
using GraphQL.NewtonsoftJson;
using GraphQL.Types;
using GraphQL.Utilities;
using GraphQL.Validation;
public class GraphQLMiddleware
{
private readonly RequestDelegate _next;
private readonly GraphQLSettings _settings;
private readonly IDocumentExecuter _executer;
private readonly IDocumentWriter _writer;
public GraphQLMiddleware(
RequestDelegate next,
GraphQLSettings settings,
IDocumentExecuter executer,
IDocumentWriter writer)
{
_next = next;
_settings = settings;
_executer = executer;
_writer = writer;
}
public async Task Invoke(HttpContext context, ISchema schema)
{
if (!IsGraphQLRequest(context))
{
await _next(context);
return;
}
await ExecuteAsync(context, schema);
}
private bool IsGraphQLRequest(HttpContext context)
{
return context.Request.Path.StartsWithSegments(_settings.Path)
&& string.Equals(context.Request.Method, "POST", StringComparison.OrdinalIgnoreCase);
}
private async Task ExecuteAsync(HttpContext context, ISchema schema)
{
var request = Deserialize<GraphQLRequest>(context.Request.Body);
var result = await _executer.ExecuteAsync(_ =>
{
_.Schema = schema;
_.Query = request?.Query;
_.OperationName = request?.OperationName;
_.Inputs = request?.Variables.ToInputs();
_.UserContext = _settings.BuildUserContext?.Invoke(context);
_.ValidationRules = DocumentValidator.CoreRules.Concat(new[] { new InputValidationRule() });
_.EnableMetrics = _settings.EnableMetrics;
if (_settings.EnableMetrics)
{
_.FieldMiddleware.Use<InstrumentFieldsMiddleware>();
}
});
await WriteResponseAsync(context, result);
}
private async Task WriteResponseAsync(HttpContext context, ExecutionResult result)
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = result.Errors?.Any() == true ? (int)HttpStatusCode.BadRequest : (int)HttpStatusCode.OK;
await _writer.WriteAsync(context.Response.Body, result);
}
public static T Deserialize<T>(Stream s)
{
using (var reader = new StreamReader(s))
using (var jsonReader = new JsonTextReader(reader))
{
var ser = new JsonSerializer();
return ser.Deserialize<T>(jsonReader);
}
}
}
这是我的 TitanSchema
课:
using GraphQL;
using GraphQL.Types;
using GraphQL.Utilities;
public class TitanSchema : Schema
{
public TitanSchema(IServiceProvider provider) : base(provider)
{
Query = provider.GetRequiredService<TitanQuery>();
this.RegisterTypeMapping<ProtoValue, ProtoValueType>();
}
}
所有这三个类都显示出以上三个问题。我尝试在网上查找,但无法弄清楚如何解决这些问题。也没有看到任何使用最新版本 GraphQL 的 dotnet GraphQL
中间件示例。任何帮助将不胜感激。
答案 0 :(得分:1)
来自graphql-dotnet upgrade guide:
<块引用>GraphQL.Utilities.ServiceProviderExtensions 已成为内部。这会影响其扩展方法 GetRequiredService 的使用。相反,请参考 Microsoft.Extensions.DependencyInjection.Abstractions NuGet 包并使用 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions 类中的扩展方法。
所以您需要做的就是添加对 Microsoft.Extensions.DependencyInjection.Abstractions
包的 nuget 引用(如果您还没有它)并添加
using Microsoft.Extensions.DependencyInjection;
到您的 TitanSchema
类。