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