0

so I am trying to inject dependencies into classes that extend these types and I am getting runtime errors when I request a schema refresh saying that it cannot create my object. Heres what I am trying to do:

public class SomeResultType : ObjectType<SomeResult>
    {
        private readonly IMyDependency _md;
        public SomeResult(IMYDependency md) 
        {
            _md= md;
        }
        protected override void Configure(IObjectTypeDescriptor<QuoteResult> descriptor)
        {
           md.domSomething(); // this dependency is meant to dynamically set the class and field descriptions based on database tables so we can remove schema description from out codebase.
        }
    }

this is the error I get. . Unable to create instance of type SomeResultType.

I recognize that hotchocolate documentation advises against injecting dependencies into these ObjectType extensions, but I feel my use case is acceptable. Please tell me if this is just outright impossible because the documentation simply advises against it does not say you cannot do this. I suspect I am making a mistake somewhere in my Startup.cs file. Here is the Startup.cs file (note i have removed some dependencies because i cant share the entirety of the app):

    services.AddDbContext<AbcContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("abc")).UseLazyLoadingProxies());
            
            services.AddScoped<ISomeDependency, SomeDependency()>();
            services.AddGraphQLServer()
                .AddDefaultTransactionScopeHandler()
                .AddHttpRequestInterceptor<HttpRequestInterceptor>()
                .AddErrorFilter<GraphQLErrorFilter>()
                .AddType<SomeResultType>()
                .AddFiltering()
                //.AddAuthorization()
                .AddProjections()
                .AddMutationType<PurchaseMutation>();

Thanks for reading, Im fairly new to graphql so I greatly appreciate your time and insights.

victor
  • 802
  • 7
  • 12

1 Answers1

1

This does not work as the object type descriptors are reserved.

If you want to inject a service you can do so in the resolver

descriptor.Field("foo").Resolve(ctx => ctx.Service<IService>().DoSomething());

In case you use the Annotation Based Approach you have support for dependency injection.

If you use Resolvers or Methods though, we only resolve the required services for the query. So all of the services that are not used in this query are not instantiated.

Pascal Senn
  • 1,712
  • 11
  • 20