Reputation: 607
I am upgrading my GraphQL version from 3.1.3
to 4.2.0
. When I compile my code I am seeing below three errors -
The type or namespace name 'EnterLeaveListener' could not be found (are you missing a using directive or an assembly reference?)
'IServiceProvider' does not contain a definition for 'GetRequiredService' and no accessible extension method 'GetRequiredService' accepting a first argument of type 'IServiceProvider' could be found (are you missing a using directive or an assembly reference?)
'ExecutionOptions' does not contain a definition for 'FieldMiddleware' and no accessible extension method 'FieldMiddleware' accepting a first argument of type 'ExecutionOptions' could be found (are you missing a using directive or an assembly reference?)
Here is my InputValidationRule
class where EnterLeaveListener
class is being used:
public class InputValidationRule : IValidationRule
{
public Task<INodeVisitor> ValidateAsync(ValidationContext context)
{
return Task.FromResult((INodeVisitor)new EnterLeaveListener(_ =>
{
}));
return Task.FromResult((INodeVisitor)) new
}
}
And here is my GraphQLMiddleware
class:
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);
}
}
}
And here is my TitanSchema
class:
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>();
}
}
All these three classes are showing above three problems. I tried looking online but couldn't figure it out on how to resolve these. Also not seeing any dotnet GraphQL
Middleware example which uses newest version of GraphQL. Any help will be greatly appreciated.
Upvotes: 1
Views: 1310
Reputation: 5532
From graphql-dotnet upgrade guide:
GraphQL.Utilities.ServiceProviderExtensions has been made internal. This affects usages of its extension method GetRequiredService. Instead, reference the Microsoft.Extensions.DependencyInjection.Abstractions NuGet package and use the extension method from the Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions class.
So all you need to do is to add nuget reference to Microsoft.Extensions.DependencyInjection.Abstractions
package if you don't have it yet and add
using Microsoft.Extensions.DependencyInjection;
to your TitanSchema
class.
Upvotes: 2