Created
December 23, 2016 19:32
-
-
Save ghstahl/2b37327e63a8557526787e4d7a98e5e4 to your computer and use it in GitHub Desktop.
Autofac registration for the GraphQL StarWars types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}; | |
}); | |
} | |
} |
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
@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