我正在使用graphql-spqr所以我不必创建schema.graphql
文件。
我有一个基类,我的大多数其他类都继承自其中。例如
@GraphQLInterface(name = "BaseResponse", implementationAutoDiscovery = true)
@ToString
@Data
public class BaseResponse {
@JsonInclude(Include.NON_NULL)
protected String responseCode;
@JsonInclude(Include.NON_NULL)
protected String responseDescription;
protected String hash;
}
其他类继承自上述基类如下
@ToString(callSuper = true)
@Data
@AllArgsConstructor
public class GetAllGroupResponse extends BaseResponse {
private List<Group> groups;
}
GetAllGroupResponse
当我设置如下的父值时
getAllGroupResponse.setResponseCode(ResponseCodeEnum.SH_97.getRespCode());
getAllGroupResponse.setResponseDescription(ResponseCodeEnum.SH_97.getRespDescription());
我希望从我的 graphql 查询请求中检索 responseCode 的值
{
getAllPagable(pageNumber : 1, numberOfRecords : 3) { responseCode groups { groupName } }
}
但它在下面抛出一个错误,告诉我它看不到 responseCode 变量,因为它不是类的直接属性GetAllGroupResponse
FieldUndefined 类型的验证错误:“GetAllGroupResponse”类型中的字段“responseCode”未定义@“getAllPagable/responseCode”
PS。BaseResponse
来自导入到当前项目的不同包/库项目
更新
基于提出的 GitHub 问题和提供的解决方案。我创建了一个如下的bean
@Bean
public ExtensionProvider<GeneratorConfiguration, ResolverBuilder> resolverBuilderExtensionProvider() {
String[] packages = {"com.sheeft.microservices.library", "com.sheeft.mircoservice.groups"};
return (config, current) -> {
List<ResolverBuilder> resolverBuilders = new ArrayList<>();
//add a custom subtype of PublicResolverBuilder that only exposes a method if it's called "greeting"
resolverBuilders.add(new PublicResolverBuilder() {
@Override
public PublicResolverBuilder withBasePackages(String... basePackages) {
return super.withBasePackages(packages); //To change body of generated methods, choose Tools | Templates.
}
});
//add the default builder
resolverBuilders.add(new AnnotatedResolverBuilder().withBasePackages(packages));
return resolverBuilders;
};
}
现在我收到一个错误,上面写着以下内容
对象类型“GetAllGroupResponse”未实现接口“BaseResponse”,因为缺少字段“responseCode”
所以我决定getResponseCode
手动添加getter方法,它运行成功。当我调用/graphql
端点时,它会引发异常
对象需要不为空”,“trace”:“graphql.AssertException:对象需要不为空