Search code examples
c#asp.net-core-3.0graphql-dotnetentity-framework-core-3.0

'No parameterless constructor defined for type SiteResourceGraphType' message, when injecting repository into SiteResourceGraphType


I have a SiteResourceGraphType class, which is inherited from ObjectGraphType. SiteResourceGraphType class accepts IGenericRepository<Site, Guid> in order to resolve items field.

When I start my application, I'm getting error 'No parameterless constructor defined for type SiteResourceGraphType'.

Startup

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ServiceDbContext>(options => options
            .UseLazyLoadingProxies()
            .UseInMemoryDatabase("GraphQLServiceDatabase"));

        services.AddScoped(typeof(IGenericRepository<,>), typeof(GenericRepository<,>));

        services.AddScoped<IServiceProvider>(x => new FuncServiceProvider(type => x.GetRequiredService(type)));
        services.AddScoped<GraphQLServiceSchema>();
        services.AddScoped<SiteResourceGraphType>();
        services.AddScoped<SiteGraphType>();
        services.AddScoped<LocationGraphType>();
        services.AddScoped<LocationInputGraphType>();

        services
            .AddGraphQL(x =>
            {
                x.ExposeExceptions = true;
            })
            .AddGraphTypes(ServiceLifetime.Scoped)
            .AddDataLoader();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseGraphQL<GraphQLServiceSchema>();
        app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());
    }
}

Schema

public class GraphQLServiceSchema : Schema
{
    public GraphQLServiceSchema(IServiceProvider serviceProvider) : base(serviceProvider)
    {
        Query = (IObjectGraphType) serviceProvider.GetService(typeof(GraphQLServiceQuery));
        Mutation = (IObjectGraphType) serviceProvider.GetService(typeof(GraphQLServiceMutation));
    }
}

Query I have used Query Organization technique from graphql-dotnet docs

public class GraphQLServiceQuery : ObjectGraphType
{
    public GraphQLServiceQuery()
    {
        Field<SiteResourceGraphType>("sites", resolve: context => new { });
    }
}

Target GraphType

public class SiteResourceGraphType : ObjectGraphType
{
    public SiteResourceGraphType(IGenericRepository<Site, Guid> siteRepository)
    {
        Field<StringGraphType>("resourceCode",
            resolve: context => { return "RCODE1"; });

        Field<ListGraphType<SiteGraphType>>("items",
            resolve: context => siteRepository.GetAll());
    }
}

The purpose of this SiteResourceGraphType is to provide response structure like this:

{
   "sites": {
        "resourceCode": "CODE1",
        "items": [...]
    },
    ...
}

I've reviewed a lot of posts from stackoverflow, github about DI in graphql-dotnet, I've tried different versions of this library (right now I'm using "3.0.0-preview-1271" version).

But still the issue about parameterless ctor remains. I'm really tired figuring out what I am missing. Please help!


Solution

  • I must to apologize for this post. The issue was on my side in external service, which registers my GraphQL service. I couldn't figure out this, since the error didn't point to external service and that was not obvious for me.