Skip to content
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

Polymorphism type on input #487

Open
valeriu-vi opened this issue Jan 12, 2024 · 0 comments
Open

Polymorphism type on input #487

valeriu-vi opened this issue Jan 12, 2024 · 0 comments

Comments

@valeriu-vi
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant