Skip to content

Instantly share code, notes, and snippets.

@ghstahl
Created December 23, 2016 19:32
Show Gist options
  • Save ghstahl/2b37327e63a8557526787e4d7a98e5e4 to your computer and use it in GitHub Desktop.
Save ghstahl/2b37327e63a8557526787e4d7a98e5e4 to your computer and use it in GitHub Desktop.
Autofac registration for the GraphQL StarWars types
// 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;
};
});
}
}
@christianlegault
Copy link

Thanks for this!

@Shrulik
Copy link

Shrulik commented Jan 10, 2018

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));
            });

@ltouro
Copy link

ltouro commented Mar 9, 2018

@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));
            });

@denisedelbando
Copy link

denisedelbando commented Sep 7, 2018

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

@misicnenad
Copy link

misicnenad commented Mar 15, 2019

@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.

Exception

I solved it by registering the said type, so in my Autofac I added another type registration

builder.RegisterType<GuidGraph>().AsSelf();

@koenvanderlinden
Copy link

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment