1

I currently have two available schema's that resolve to completely different queries. At this time the azure function startup file when going through the process of dependency injection it only takes the most recent schema added. So the query only resolves to the most recently added Schema. I believe it's a naming conflict with the interface being used but I don't actually know how to get around it.

I tried adding the services as both a transient and scoped.

builder.Services.AddTransient<ISchema>(_ => new SchemaOne(new FuncDependencyResolver(type => (IGraphType)_.GetRequiredService(type))));
builder.Services.AddTransient<ISchema>(_ => new SchemaTwo(new FuncDependencyResolver(type => (IGraphType)_.GetRequiredService(type))));

or

builder.Services.AddScoped<ISchema>(_ => new SchemaOne(new FuncDependencyResolver(type => (IGraphType)_.GetRequiredService(type))));
builder.Services.AddScoped<ISchema>(_ => new SchemaTwo(new FuncDependencyResolver(type => (IGraphType)_.GetRequiredService(type))));

The goal is to be able to have both queries resolve to their respective schemas. At this time { queryOne { name } } returns the error "message": "Cannot query field \"queryOne\" on type \"QueryTwoType\".",

Jacq
  • 105
  • 2
  • 14
  • You can implement/inject a factory and through that factory required schema instance can be created. – user1672994 Oct 09 '19 at 15:42
  • Sorry I'm a bit new to c# and design patterns in general, is the factory just another class that will then be instantiated come time? – Jacq Oct 09 '19 at 15:49
  • Yes, you can check the similar discussion in https://stackoverflow.com/questions/39174989/how-to-register-multiple-implementations-of-the-same-interface-in-asp-net-core thread – user1672994 Oct 09 '19 at 15:51
  • I'll take a look and report back. – Jacq Oct 09 '19 at 16:02
  • You don't have to depend on interfaces could just register SchemaOne and SchemaaTwo as concrete classes right? – awright18 Oct 09 '19 at 17:32
  • When attempting to register as concrete classes I get an error when I attempt to hit the azure function. `unable to resolve service for type GraphQL.Types.Schema while attempting to activate $azEndpoint` this is how I registered the schema concretely(?) `builder.Services.AddTransient(_ => new SchemaOne(new FuncDependencyResolver(type => (IGraphType)_.GetRequiredService(type))));` – Jacq Oct 09 '19 at 19:50

1 Answers1

0

After taking a look at the link provided by @user1672994, I opted to create my own interface and then inject that into the azure function. The reason I opted to do this was because I wanted to provide the azure function with the opportunity to choose the schema that is used. I was unable to find a solution to that use case in the link provided.

Azure Function Dependency injection code

builder.Services.AddTransient<ISchemaMapper>(_ => new SchemaMapper(new Dictionary<string,ISchema> {
{"SchemaOne", new SchemaOne(new FuncDependencyResolver(type => (IGraphType)_.GetRequiredService(type))) },
{"SchemaTwo", new SchemaTwo(new FuncDependencyResolver(type => (IGraphType)_.GetRequiredService(type))) } }));

Interface Used

public interface ISchemaMapper
{
  ISchema GetSchema(string schema);
}

Implementation of interface

public class SchemaMapper : ISchemaMapper
{
   public Dictionary<string, ISchema> mappedSchema;
   public SchemaMapper(Dictionary<string, ISchema> mappedSchemas)
   {
      this.mappedSchema = mappedSchemas;
   }

   public ISchema GetSchema(string schema)
   {
      return mappedSchema[schema];
   }
}

At this point in time it is a hacky way around conflicting dependency injections, but I will update if I either find a better way or it is chugging along strong in the future.

Jacq
  • 105
  • 2
  • 14