【发布时间】:2018-09-02 13:08:51
【问题描述】:
我正在尝试使用 SPQR 从 Cayenne 生成的类生成 GraphQL 架构。 Cayenne 类是这样的
public class MyCayenneClass {
public static final Property<Integer> A_PROPERTY = Property.create("aProperty", Integer.class);
public static final Property<Integer> ANOTHER_PROPERTY = Property.create("anotherProperty", String.class);
public void setAProperty(Integer aProperty) {
writeProperty("aProperty", aProperty);
}
public Integer getAProperty() {
return (Integer)readProperty("aProperty");
}
public void setAnotherProperty(String anotherProperty) {
writeProperty("anotherProperty", anotherProperty);
}
public String getAnotherProperty() {
return (String)readProperty("anotherProperty");
}
}
由于该类不是简单的 POJO,SPQR 会引发异常并且不会生成架构。
Error: QUERY_ROOT fields must be an object with field names as keys or a function which returns such an object.
这里最好的方法是什么(不修改 cayenne 类(即注释方法)?
GraphQLEndPoing.java
@WebServlet(urlPatterns = "/graphql")
public class GraphQLEndpoint extends SimpleGraphQLServlet {
public GraphQLEndpoint() {
super(buildSchema());
}
//This method used SPQR
private static GraphQLSchema buildSchema() {
GraphQLSchema schemaGenerator = new GraphQLSchemaGenerator()
.withOperationsFromSingletons(myRepository) //register the beans
.generate();
return schemaGenerator;
}
private static final MyRepository myRepository;
static {
myRepository= new MyRepository ();
}
}
MyRepository.java
public class MyRepository{
private MyLibService libService;
@GraphQLQuery
public MyCayenneClass find(Integer id) {
List<MyCayenneClass> myList= libService.fetchById(new Integer[] {id});
return myList.get(0);
}
}
*仅供参考。如果我声明架构。代码可以正常工作
schema {
query: Query
}
type Query {
find(id: Int): MyCayenneClass
}
type ConcContract {
id: ID
aProperty: Int
anotherProperty: String
}
【问题讨论】:
-
请先试用最新版本的 SPQR(目前为 0.9.6)。这个类似乎应该可以正常工作。稍后我会尝试并为您提供更多信息。顺便说一句,这不是您在 SPQR 注册的顶级课程,对吧?
-
我不知道如何处理这个类... writeProperty 和 readProperty 来自哪里?你能提供一个更完整的例子吗,因为我对 Cayenne 完全不熟悉?看看这个类是用作输入还是输出,以及模式生成器是如何配置的,这一点很重要。
-
它们是 Cayenne 类。即 cayenne-server-4.0.B2.jar。我更新了代码
-
GraphQL 注释是我在此处粘贴代码时的错。我对这个问题的第一个猜测是 SPQR 找不到类中属性的 getter/setter。即 Property
A_PROPERTY //get/set public void setAProperty(Integer aProperty) { writeProperty("aProperty", aProperty); } public Integer getAProperty() { return (Integer)readProperty("aProperty"); } -
嗯,似乎无法复制...该错误声称没有在顶层映射任何内容。也许在
AnnotatedResolverBuilder#buildQueryResolvers中放置一个断点并检查它是否返回任何内容。如果您仍然遇到问题,请在Gitter 上联系我,我们可以深入调查。
标签: java graphql-java