-
-
Save ghstahl/2b37327e63a8557526787e4d7a98e5e4 to your computer and use it in GitHub Desktop.
// https://github.com/graphql-dotnet/graphql-dotnet | |
public static class GraphQLStarWarsExtension | |
{ | |
public static void RegisterGraphQLTypes(this ContainerBuilder builder) | |
{ | |
builder.RegisterInstance(new DocumentExecuter()).As<IDocumentExecuter>(); | |
builder.RegisterInstance(new DocumentWriter()).As<IDocumentWriter>(); | |
builder.RegisterInstance(new StarWarsData()).As<StarWarsData>(); | |
builder.RegisterType<StarWarsQuery>().AsSelf(); | |
builder.RegisterType<HumanType>().AsSelf(); | |
builder.RegisterType<DroidType>().AsSelf(); | |
builder.RegisterType<EpisodeEnum>().AsSelf(); | |
builder.RegisterType<CharacterInterface>().AsSelf(); | |
builder.RegisterType<StarWarsQuery>().AsSelf(); | |
builder.RegisterType<StarWarsSchema>().AsSelf(); | |
builder.Register<Func<Type, GraphType>>(c => | |
{ | |
var context = c.Resolve<IComponentContext>(); | |
return t => { | |
var res = context.Resolve(t); | |
return (GraphType)res; | |
}; | |
}); | |
} | |
} |
Will no longer work unless you switch
builder.Register<Func<Type, GraphType>>(c =>
{
var context = c.Resolve<IComponentContext>();
return t => {
var res = context.Resolve(t);
return (GraphType)res;
};
});
with :
builder.Register<FuncDependencyResolver>(c =>
{
var context = c.Resolve<IComponentContext>();
return new FuncDependencyResolver( type => context.Resolve(type));
});
@Shrulik thanks! For me, that is what works (register IDependencyResolver instead FuncDependencyResolver)
builder.Register<IDependencyResolver>(c =>
{
var context = c.Resolve<IComponentContext>();
return new FuncDependencyResolver(type => context.Resolve(type));
});
mine's a bit different for resolving the ISchema. Also, to emphasize IDependencyResolver is in GraphQL namespace
builder.RegisterType<CompanySchema>().As<ISchema>().InstancePerRequest();
builder.Register<GraphQL.IDependencyResolver>(c =>
{
var context = c.Resolve<IComponentContext>();
return new FuncDependencyResolver(type => context.Resolve(type));
});
Thank you @iTourO
@denisedelbando thank you, I got it working using your way of resolving the dependencies and schema.
For anyone using the GraphQL.EntityFramework library, I had a "Service GraphQL.EntityFramework.GuidGraph not registered" exception (see the image below) thrown from time to time when running GraphQL Tests.
I solved it by registering the said type, so in my Autofac I added another type registration
builder.RegisterType<GuidGraph>().AsSelf();
I needed to add
builder.RegisterType<InstrumentFieldsMiddleware>().AsSelf();
and registering schema still works with GraphQL.3.0.0-preview-1648:
builder.Register((c, t) => {
var context = c.Resolve<IComponentContext>();
var schema = new StarWarsSchema(new GraphQL.FuncServiceProvider(type => context.Resolve(type)));
return schema;
}).AsImplementedInterfaces().SingleInstance();
Thanks for this!