graphql-dotnet: Type is abstract and can not be used to construct objects from dictionary values. Please register a conversion within the ValueConverter or for input graph types override ParseDictionary method

I have an error message that I cannot figure out. We are upgrading our .NET Console Apps (used as APIs) from GraphQL 2.4.0 using .NET Core 3.1 and .NET Standard 2.1 helper projects to GraphQL 7.7.2 with .NET 8. We also went from AutoMapper 9 to 12.

Originally we tried using the GraphQL documentation at https://graphql-dotnet.github.io/docs/migrations/ to go from v2 to v3 then v3 to v4 then v4 to v5 then v5 to v7. We punted that idea and just went all in from v2 to v7. We are in a state where our GraphQL Queries are working well but our Mutations are not.

Summary

We are using a Mutation for Inserts/Updates and our Inserts/Updates but we are popping a 400 error with the InnerException message of: “Type ‘IUser’ is abstract and can not be used to construct objects from dictionary values. Please register a conversion within the ValueConverter or for input graph types override ParseDictionary method.”

We googled the error message and I don’t get much. I do see that part of the error is shown on https://graphql-dotnet.github.io/docs/migrations/migration4/ but I’ll be honest that I’m new to GraphQL and I haven’t touched .NET in 5 years. If my answer is on the migration4 link, I wouldn’t know.

I don’t know what the error is saying. I thought maybe there was a problem with my Field statements. We had Field("isActive", x => x.IsActive, type: typeof(BooleanGraphType)); but changed to Field<BooleanGraphType>("IsActive"); thinking we were on to something. But, we have the same error.

Any pointers?

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

@Shane32 DUDE! MY DUDE! You saved me hours, at least!

        public override object ParseDictionary(IDictionary<string, object?> value)
        {
            return value;
        }

I must have some sort of Frankenstein input graph type even though not by specific name. Either way, that override satisfied the error. I would have never would have guessed the ParseDictionary signature let alone know to return value. The docs went clear over my head, too.

I don’t have an error and my data seems to have saved. I was too excited to even check the DB. I don’t have errors!

Thank you so much!

How the fields are configured have not really changed internally even though the recommended syntax is a bit different now. If you want to post the before/after, I can confirm that it is being configured the same.

What’s different (as it relates to your original issue here), is that the inputs are converted to their CLR types during validation rather than during execution. This helps catch parsing errors before execution begins. However, the GraphQL.NET engine has to guess at the T in GetArgument<T>, which it does so based on the T in InputObjectGraphType<T>.

For example:

public class User
{
    public string Name { get; set; }
}

public class UserType : InputObjectGraphType<User>
{
    public UserType
    {
        Field(x => x.Name);
    }

    public override object ParseDictionary(IDictionary<string, object?> value)
    {
        // this is called during validation in v4, which by default calls ToObject to convert value to User
        // if User was an interface (e.g. IUser), this would fail because ToObject cannot convert a dictionary
        // to an interface, as you can't create a new instance of IUser
        return base.ParseDictionary(value);

        // in v2/3, this method did not exist, and essentially the dictionary itself was stored
        // until GetArgument<T> was called, at which point ToObject converts the dictionary to T

        // the workaround for v4+ for is then to just return the dictionary, so it is stored until GetArgument is called
        return value;
    }
}

The issue would actually apply to both InputObjectGraphType<T> as well as AutoRegisteringInputObjectGraphType<T>. However, InputObjectGraphType does not call ToObject (since it does not know what T is) and so it would be immune to this problem.

Note that the v8 beta compiles the ToObject code during schema initialization for faster execution and would notice the error then. But not so for v7.