GraphQL声明Mutation的Input类型时报错IdInput不是GraphQLInputType
GraphQL Mutation Input类型报错解决方案
报错核心原因
你使用的graphql-java-kickstart工具栈要求Schema中定义的所有input类型,必须对应Java实体类并标注专属的@InputType注解,否则框架会将其识别为普通GraphQL对象类型,无法匹配输入类型的要求。
排查与解决步骤
- 步骤1:给
IdInput对应的Java类添加@InputType注解,同时保证类包含无参构造、完整的getter/setter方法:
import graphql.kickstart.tools.boot.annotation.InputType; @InputType public class IdInput { private String id; // 必须保留无参构造 public IdInput() {} public String getId() { return id; } public void setId(String id) { this.id = id; } }
- 步骤2:给
TestTypesInput对应的Java类也添加@InputType注解,所有Schema中声明的input类型都需要对应添加该注解:
import graphql.kickstart.tools.boot.annotation.InputType; import java.util.List; @InputType public class TestTypesInput { private List<IdInput> testTypes; public TestTypesInput() {} public List<IdInput> getTestTypes() { return testTypes; } public void setTestTypes(List<IdInput> testTypes) { this.testTypes = testTypes; } }
- 步骤3:确认输入类所在的包在框架扫描范围内,如果输入类不在启动类的扫描子包下,可在application配置中添加扫描路径:
# application.yml 配置示例 graphql: tools: schema-parser-options: package-prefix: - 你的输入类所在包路径,例如com.yourproject.graphql.input
- 步骤4:全局检索所有Schema定义文件,确认没有重复定义名为
IdInput的其他type/input类型,重名会导致类型识别冲突。
调用验证
调整完成后,你可以按照以下格式传入输入对象列表:
Mutation请求示例:
mutation CreateTest($label: String!, $testTypes: TestTypesInput) { createTest(label: $label, testTypes: $testTypes) { id label } }
对应参数示例:
{ "label": "测试内容", "testTypes": { "testTypes": [ {"id": "1001"}, {"id": "1002"} ] } }
内容的提问来源于stack exchange,提问作者Barbi
相关产品推荐
相关产品推荐

