Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDependencyResolver obsolete, how do I properly setup schema using .net core 2.0 DI and GraphQL ? #616

Closed
peyoquintero opened this issue Apr 5, 2018 · 6 comments
Labels
question Developer asks to help him deal with some problem

Comments

@peyoquintero
Copy link

I am setting up my .net core environment to use .net core DI with GraphQL . The sample code (starwars) does this:

services.AddSingleton( s => new StarWarsSchema(new FuncDependencyResolver(type => (IGraphType)s.GetRequiredService(type))));

However, the IDependencyResolver interface is not available in the GraphQL version I am using (latest at this time: 0.17.3) so I can't use that code. Is there any example of how to do this with this latest version of graphQL or should I revert to some previous version instead?.

@joemcbride
Copy link
Member

That example is using the 2.0 pre-release which is newer than 0.17.3. So you can upgrade to the pre-release or use the setup that is required in 0.17.3, which is just removing the FuncDependencyResolver type.

2.x

services.AddSingleton(
  s => new StarWarsSchema(new FuncDependencyResolver(type => (IGraphType)s.GetRequiredService(type))));

0.x

services.AddSingleton( 
  s => new StarWarsSchema((type) => (IGraphType)s.GetRequiredService(type)));

@joemcbride joemcbride added the question Developer asks to help him deal with some problem label Apr 5, 2018
@peyoquintero
Copy link
Author

Thanks Joe. Is 2.0 pre-release the TypeResolve branch?

@joemcbride
Copy link
Member

2.0 is currently the master branch

@fiyazbinhasan
Copy link
Contributor

Hi @joemcbride , what if I configure the default container the following way,

services.AddSingleton<StartWarsQuery>();
services.AddSingleton<ISchema, StarWarsSchema();

And apply constructor injection to pass in the query or mutation,

public class StarWarsSchema : Schema 
{
    public StarWarsSchema(StartWarsQuery query)
    {
        Query = query;
    }
}

Will it be any different from,

services.AddSingleton(s => new StarWarsSchema(new FuncDependencyResolver(type => (IGraphType)s.GetRequiredService(type))));

@joemcbride
Copy link
Member

joemcbride commented Apr 9, 2018

@fiyazbinhasan Yes, if you do not provide a dependency resolver to your Schema then if you attempt to do constructor injection in any other GraphTypes in the graph then it will fail. By default, if no dependency resolver is provided, it attempts to use Activator.CreateInstance to create the graph types.

@joemcbride
Copy link
Member

Updates related to this in #780.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Developer asks to help him deal with some problem
Projects
None yet
Development

No branches or pull requests

3 participants