graphql-dotnet/graphql-dotnet

How rename field in input type?

Closed this issue · 5 comments

I am using the latest version of graphql-dotnet as well as the latest .NET version.
For now I have this class

public class RoomInputClass
{
    public HumanInputClass? Human { get; set; }
    public RobotInputClass? Robot { get; set; }
}
public class RoomInputType : InputObjectGraphType<RoomInputClass>
{
    public RoomInputType()
    {
        Field<HumanInputType>(nameof(RoomInputClass.Human));
        Field<RobotInputType>(nameof(RoomInputClass.Robot));
    }
}

The issue now is that I renamed the property Robot to Machine in the RoomInputClass. But to make this a non-breaking change for our front-end app I'd like for the GraphQL field to keep the same name. I tried something like this:

public class RoomInputClass
{
    public HumanInputClass? Human { get; set; }
    public MachineInputClass? Machine { get; set; }
}
public class RoomInputType : InputObjectGraphType<RoomInputClass>
{
    public RoomInputType()
    {
        Field<HumanInputType>(nameof(RoomInputClass.Human));
        Field<RobotInputType>(nameof("Robot"))
            .Resolve(x => x.Source.Machine)
    }
}

But this returns the error Resolvers are not allowed on non-output graph types. This is not my real code, but it illustrates the issue I'm having. I need the name to stay the same → Robot, but the value I get from the mutation should be mapped onto my RoomInputClass.Machine property. How can I do this?
I feel like there is a simple way to do this that I'm missing, am I wrong?

Thanks!

Field<HumanInputType>(x => x.Human);
Field<RobotInputType>("Robot", x => x.Machine);

And if you have CLR mappings configured:

Field(x => x.Human);
Field("Robot", x => x.Machine);
Field<HumanInputType>(x => x.Human);
Field<RobotInputType>("Robot", x => x.Machine);

Thank you for the response, but here I get the error Cannot implicitly convert type 'MachineInputClass' to 'RobotInputType'

Hmm, I usually use the simpler syntax. I think this is actually correct:

Field("Robot", x => x.Machine, type: typeof(RobotInputType));

Thank you! This works as expected!