您可以使用graphql-spqr,它允许您根据服务类自动生成模式。在您的情况下,它看起来像这样:
public class Pojo {
private Long id;
private String name;
// whatever Ext is, any (complex) object would work fine
private List<Ext> exts;
}
public class Ext {
public String something;
public String somethingElse;
}
大概,您有一个包含您的业务逻辑的服务类:
public class PojoService {
//this could also return List<Pojo> or whatever is applicable
@GraphQLQuery(name = "pojo")
public Pojo getPojo() {...}
}
要公开此服务,您只需执行以下操作:
GraphQLSchema schema = new GraphQLSchemaGenerator()
.withOperationsFromSingleton(new PojoService())
.generate();
然后,您可以触发查询,例如:
query test {
pojo {
id
name
exts {
something
somethingElse
} } }
不需要奇怪的包装器或任何类型的自定义代码,也不需要牺牲类型安全。适用于泛型、依赖注入或您项目中可能拥有的任何其他爵士乐。
全面披露:我是graphql-spqr的作者。