ricky32
ricky32

Reputation: 107

Net Core - unable to resolve dependency

I'm using GraphQL 2.4.0 with .NET Core 3.1 solution and Microsoft DI. From within mutation, I need to perform additional processing (shown in MyMutation class below).

My Schema.cs looks like:

public class MySchema : GraphQL.Types.Schema
{
    private ISchema _schema { get; set; }
    public ISchema GraphQLSchema
    {
        get
        {
            return this._schema;
        }
    }

    public MySchema()
    {
        this._schema = Schema.For(@"
            type MyObj{
                id: ID
                name: String,
                type: String,
                createdAt: String
            }

            type Mutation {
                objSync(type: String): MyObj
            }

            type Query {
                myobj: MyObj
            }
        ", _ =>
        {
            _.Types.Include<MyQuery>();
            _.Types.Include<MyMutation>();
        });
    }
}

MyMutation.cs looks like:

[GraphQLMetadata("MyMutation")]
public class MyMutation
{
    private readonly ISomeType someType;

    public MyMutation(ISomeType someType)
    {
        this.someType= someType;
    }

    [GraphQLMetadata("objSync")]
    public Document SyncObj(string type)
    {
        byte[] _bytes = null;
        _bytes = someType.Process(type).Result;
        return new Obj { File = Encoding.UTF8.GetString(_bytes , 0, _bytes .Length) };
    }
}

ISomeType looks like:

public interface ISomeType
{
    Task<byte[]> Process(string type);
}

and SomeManager.cs would be:

public class SomeManager: ISomeType
{
    private readonly IConfiguration configuration;
    private readonly Func<string, IOtherTypeManager> otherType;

    public SomeManager(IConfiguration configuration, Func<string, IOtherTypeManager> otherType)
    {
        this.configuration = configuration;
        this.otherType= otherType;
    }

    public async Task<byte[]> Process(string type)
    {
       ...
    }
}

As you may notice, I need to resolve ISomeType with DI. So Startup.cs looks like:

...
public static void AddServices(this IServiceCollection services)
    {
        services.AddSingleton<IDependencyResolver>(_ => new FuncDependencyResolver(_.GetRequiredService));
        services.AddSingleton<IDocumentExecuter, DocumentExecuter>();
        services.AddSingleton<IDocumentWriter, DocumentWriter>();
        services.AddSingleton<ISomeType, SomeManager>();
        ...
    }
...

When executed the program, it throws the following exception:

Error trying to resolve objSync.
Failed to call Activator.CreateInstance. Type: MyNameSpace.Graphql.MyMutation

No parameterless constructor defined for type 'MyNameSpace.Graphql.MyMutation'.

at System.RuntimeType.CreateInstanceDefaultCtorSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type)
at GraphQL.DefaultDependencyResolver.Resolve(Type type)

Do I need to resolve the ISomeType differently? Please guide.

Update

Getting rid of ISomeType from MyMutation.cs solves this problem... but I need it to call someType.Process(type).Result.

Upvotes: 0

Views: 501

Answers (2)

Gaurav Madaan
Gaurav Madaan

Reputation: 499

services.AddScoped<MyMutation>();
services.AddScoped<ISchema, MySchema>();

Basically I will follow this example: https://github.com/graphql-dotnet/examples/tree/master/src/StarWars

Upvotes: 0

Noymul Islam Chowdhury
Noymul Islam Chowdhury

Reputation: 141

MyMutation class also need to be registered in DI container. Can you try this out?

services.AddSingleton<MyMutation>();

Upvotes: 2

Related Questions