graphql-platform icon indicating copy to clipboard operation
graphql-platform copied to clipboard

How to resolve service registered in IServiceCollection?

Open featherbits opened this issue 8 years ago • 15 comments

When using ASP.NET Core and registering services in IServiceCollection from Startup.ConfigureServices method, those services then are not available in IResolverContext when I use T Service<T>().

Is this by design or a bug?

featherbits avatar Nov 29 '18 11:11 featherbits

Are you using our middleware?

michaelstaib avatar Nov 29 '18 12:11 michaelstaib

Here is our middleware example with dependency injection: https://github.com/ChilliCream/hotchocolate-aspnetcore/blob/next/examples/StarWars/Startup.cs

michaelstaib avatar Nov 29 '18 12:11 michaelstaib

I'm trying to resolve service from context of IMiddlewareContext. My middleware is registered with IMiddlewareConfiguration.Use(FieldMiddleware middleware). I have not registered any service provider on schema configuration. GraphQL is added from GraphQLApplicationBuilderExtensions.

featherbits avatar Nov 29 '18 12:11 featherbits

let me do a quick test with the middleware context ....

michaelstaib avatar Nov 29 '18 12:11 michaelstaib

It works with preview.3

I have added this to the posted startup.cs

c.Use(next => context =>
                {
                    if (context.Service<CharacterRepository>() == null)
                    {
                        throw new NotSupportedException();
                    }
                    return next(context);
                });

michaelstaib avatar Nov 29 '18 12:11 michaelstaib

Can you give feedback once you figured it out so I can close this issue or we can investigate further.

michaelstaib avatar Nov 29 '18 12:11 michaelstaib

I made upgrade to 0.7.0-preview.3 and still getting the same issue. Can it be related to the fact that I'm using schema first approach? It looks like my setup is the same as in https://github.com/ChilliCream/hotchocolate-aspnetcore/blob/next/examples/StarWars/Startup.cs

featherbits avatar Nov 29 '18 12:11 featherbits

Should not make a difference.... can you share some code?

michaelstaib avatar Nov 29 '18 12:11 michaelstaib

We could create a small StarWars schema first project...

michaelstaib avatar Nov 29 '18 12:11 michaelstaib

Could you set up a small schema first project that replicates and share that repo?

michaelstaib avatar Nov 29 '18 12:11 michaelstaib

Could you set up a small schema first project that replicates and share that repo?

Working on it

featherbits avatar Nov 29 '18 12:11 featherbits

On fresh project it works as expected. I will add more code to it in order to replicate setup.

featherbits avatar Nov 29 '18 13:11 featherbits

The issue actually arrises when services are registered in same action that configures schema. When they are registered before or after AddGraphQL is called, then it works fine.

Fore example, this would not work

services.AddGraphQL(sp => Schema.Create(@"
    type Mutation {
        authenticate(email: String!, password: String!): String
    }
", c => {
    services.AddSingleton<IMySvc, MySvc>();
    c.BindType<Mutation>();
    c.Use(next => ctx => {
        if (ctx.Service<IMySvc>() == null) {
            throw new System.NotSupportedException();
        }
        return next(ctx);
    });
}));

featherbits avatar Nov 29 '18 13:11 featherbits

No problem....

we have a slack group ... so if you like to join use the following link:

https://join.slack.com/t/hotchocolategraphql/shared_invite/enQtNDkxMjA1MzkxNzMwLTFlMjUxMWQ2YWQ4ODRkYmI5MWJiMmNhNzI2MjQ4MzRmZjlhY2Y0Yjg0MzM1YTRhMDc4YWUwOTViMjNhNzY1ZGI

michaelstaib avatar Nov 29 '18 14:11 michaelstaib

Adding services within the schema configure action is not supported since when this action is called the IServiceProvider instance is already created and cannot be modified.

michaelstaib avatar Nov 29 '18 14:11 michaelstaib