leangen / graphql-spqr

Build a GraphQL service in seconds
Apache License 2.0
1.09k stars 179 forks source link

Polymorphism type on input #487

Open valeriu-vi opened 5 months ago

valeriu-vi commented 5 months ago

I'm having a problem with using polymorphism in the input for the setFigure method. Despite the fact that I used .abstractInputTypeResolution() option and provided the type disambiguator in the query, I still get an error.

Error Message:

Validation error of type WrongType: argument 'figure' with value 'ObjectValue{objectFields=[ObjectField{name='area', value=IntValue{value=50}}, ObjectField{name='_type_', value=EnumValue{name='Rectangle'}}, ObjectField{name='width', value=IntValue{value=10}}]}' contains a field not in 'FigureInput': 'width' @ 'figureService_setFigure

Java types with SPQR-Annotations the following manner:

@GraphQLInterface(name = "Figure", implementationAutoDiscovery = true)
public interface FigureInterface {
    float getArea();
}

public class Figure implements FigureInterface {

    protected float area = 5;

    @Override
    public float getArea() {
        return area;
    }

    public void setArea(float area) {
        this.area = area;
    }

}

public class Rectangle extends Figure {
    protected float width;

    public float getWidth() {
        return width;
    }

    public void setWidth(float width) {
        this.width = width;
    }

}

public class Circle extends Figure {

    protected float radius;

    public float getRadius() {
        return radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

}

@GraphQLApi
public class FigureService {

    @GraphQLQuery
    public List<FigureInterface> getSeveralFigures() {
        return List.of(new Circle(), new Rectangle());
    }

    @GraphQLQuery
    public FigureInterface setFigure(FigureInterface figure) {
        return figure;
    }

}

And here are the queries:


fragment f on Figure {
    __typename area
    ... on Circle {
      radius
    }
    ... on Rectangle {
      width
    }
}

query q1 {
  figureService_severalFigures {
    ... f
  }
}

query q2 {
  figureService_setFigure(figure: { area: 50, _type_: Rectangle, width: 10}) {
    ...f
  }
}

While q1 works as expected, q2 is resulting in an error. I have provided the type disambiguator (type: Rectangle) in the query, but it seems the input isn't being correctly interpreted, leading to the validation error.

Could you please provide guidance on how to make polymorphism work for input in the setFigure method without encountering this error?