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

Comments

Projects
None yet
3 participants
@peyoquintero

peyoquintero commented Apr 5, 2018

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

This comment has been minimized.

Member

joemcbride commented Apr 5, 2018

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 label Apr 5, 2018

@peyoquintero

This comment has been minimized.

peyoquintero commented Apr 6, 2018

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

@joemcbride

This comment has been minimized.

Member

joemcbride commented Apr 6, 2018

2.0 is currently the master branch

@fiyazbinhasan

This comment has been minimized.

fiyazbinhasan commented Apr 9, 2018

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

This comment has been minimized.

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 joemcbride closed this Jun 15, 2018

@joemcbride

This comment has been minimized.

Member

joemcbride commented Aug 13, 2018

Updates related to this in #780.

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