通过查询模式通过子对象访问父对象的受保护字段 [英] Access to protected field of parent object through child object via a query schema
问题描述
我正在使用 graphql-spqr 这样我就不必创建
I am using graphql-spqr So that I don't have to create schema.graphql
files.
我有一个基类,我的大多数其他类都在该基类中继承.例如
I have a base class in which most of my other classes inherit from. For example
@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;
}
其他类继承自上述基类,如下所示:
Other classes inherit from the above base class as below
@ToString(callSuper = true)
@Data
@AllArgsConstructor
public class GetAllGroupResponse extends BaseResponse {
private List<Group> groups;
}
当我像下面那样设置GetAllGroupResponse
的父值时
When I set the parent values of GetAllGroupResponse
like below
getAllGroupResponse.setResponseCode(ResponseCodeEnum.SH_97.getRespCode());
getAllGroupResponse.setResponseDescription(ResponseCodeEnum.SH_97.getRespDescription());
我希望从我的graphql查询请求中检索responseCode的值
I wish to retrieve the value of responseCode from my graphql query request
{
getAllPagable(pageNumber : 1, numberOfRecords : 3) { responseCode groups { groupName } }
}
{
getAllPagable(pageNumber : 1, numberOfRecords : 3) { responseCode groups { groupName } }
}
,但是它下面抛出一个错误,告诉我它看不到responseCode变量,因为它不是类GetAllGroupResponse
but it throws an error below which tells me it can't see the responseCode variable because it is not a direct property of the class GetAllGroupResponse
FieldUndefined类型的验证错误:类型为'GetAllGroupResponse'的字段'responseCode'未定义@'getAllPagable/responseCode'
Validation error of type FieldUndefined: Field 'responseCode' in type 'GetAllGroupResponse' is undefined @ 'getAllPagable/responseCode'
PS. BaseResponse
来自导入到当前项目中的其他程序包/库项目
PS. BaseResponse
is from a different package/library project that was imported into the current project
更新
基于GitHub提出的问题和提供的解决方案.我创建了一个如下所示的豆子
Based on the GitHub issues raised and solutions provided. I created a bean as below
@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;
};
}
现在我得到一个错误,提示以下内容
and now I get an error which says the following
对象类型"GetAllGroupResponse"没有实现接口"BaseResponse",因为缺少字段"responseCode"
object type 'GetAllGroupResponse' does not implement interface 'BaseResponse' because field 'responseCode' is missing
因此,我决定手动添加getResponseCode
getter方法,该方法成功运行.当我呼叫/graphql
端点时,它将引发异常
So I decided to add the getResponseCode
getter method manually and it runs successfully. When I the call the /graphql
endpoint it throws an exception
对象必须不为空", "trace":"graphql.AssertException:对象必须不为空
Object required to be not null", "trace": "graphql.AssertException: Object required to be not null
推荐答案
我认为您所需要的只是为您的项目设置基础软件包.例如
I think all you need is to set the base packages for your project. E.g.
generator.withBasePackages("your.root.package", "library.root.package")
或者,如果您使用的是SPQR Spring Starter,请添加
Or, if you're using SPQR Spring Starter, add
graphql.spqr.base-packages=your.root.package,library.root.package
到您的application.properties
文件,以达到相同的效果.
to your application.properties
file, for the same effect.
当您具有跨多个软件包(甚至是库,例如您的情况)的类的层次结构时,需要此,以便SPQR知道应公开哪些软件包,因为否则可能会意外地暴露一些框架代码甚至核心Java内容像 getClass .
This is needed when you have a hierarchy of classes across multiple packages (or even libraries, like in your case) to let SPQR know which packages should be exposed, because otherwise it might accidentally expose some framework code or even core Java things like getClass.
这篇关于通过查询模式通过子对象访问父对象的受保护字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!