Graphql-dotnet: Issue Resolving Dependencies in context.GetArgument<T>

Created on 7 May 2018  路  3Comments  路  Source: graphql-dotnet/graphql-dotnet

I am running into an error when trying to get an argument from a mutation

```namespace Performance.GraphQl
{
public class PerformanceMutation :ObjectGraphType
{
public PerformanceMutation(PerformanceService service, Core core)
{
Field(
"createSession",
arguments: new QueryArguments(
new QueryArgument> { Name = "session" }
),
resolve: context =>
{
var session = context.GetArgument("session"); //System.MissingMethodException: 'No parameterless constructor defined for this object.'
var test = new PerformanceSession(core);//this works fine
return service.AddSession(test); //As does this.
}
);
}
}

}


The PerformanceSession Object by my company's design needs a constructor that takes a Core object

namespace Performance.Data
{
public class PerformanceSession : ModelBase
{
public PerformanceSession(Core core) : base(core)
{
}

    public override IModelPersistenceBase GetPersistence()
    {
        return new PerformanceSessionPersistence(UlineCore);
    }

///OTHER LOGIC///
}


Is having a DI dependent class in the context.GetArguments<T> method not supported?  

Here is my Schmea in case that is needed to help, but I pretty much copied from the StarWars example.

namespace Performance.GraphQl
{
public class PerformanceSchema : Schema
{
public PerformanceSchema(IDependencyResolver resolver): base(resolver)
{
Query = resolver.Resolve();
Mutation = resolver.Resolve();

    }
}

}
```

And the Resolver
```namespace Performance
{
public class UnityResolver : IDependencyResolver
{
protected IUnityContainer container;

    public UnityResolver(IUnityContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException("container");
        }
        this.container = container;
    }

    public object GetService(Type serviceType)
    {
        try
        {
            return container.Resolve(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return container.ResolveAll(serviceType);
        }
        catch (ResolutionFailedException)
        {
            return new List<object>();
        }
    }

    public IDependencyScope BeginScope()
    {
        var child = container.CreateChildContainer();
        return new UnityResolver(child);
    }

    public void Dispose()
    {
        Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        container.Dispose();
    }
}

}```

Thanks for any help you can provide

question

Most helpful comment

Argument are just a Dictionary<string, object>, so you could write your own mapper if you really like, though having a DTO is a bit easier.

All 3 comments

Arguments are not setup to support DI. They are designed to use basic DTOs. So you would need to create a DTO with a parameterless constructor that represents your arguments.

Thats what I suspected, but thanks for the confirmation.

Argument are just a Dictionary<string, object>, so you could write your own mapper if you really like, though having a DTO is a bit easier.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

phoenixjun picture phoenixjun  路  4Comments

joemcbride picture joemcbride  路  4Comments

fiyazbinhasan picture fiyazbinhasan  路  3Comments

dharmeshtailor picture dharmeshtailor  路  4Comments

ManuelDeLeon picture ManuelDeLeon  路  3Comments