创建列表<a> as GraphQLObjectType

时间:2017-04-12 09:00:21

标签: java graphql-java

I want to return a list of class A objects from my GraphQLDatafetcher. I know I can return a single A like this:

GraphQLObjectType a = GraphQLAnnotations.object(A.class);
...
GraphQLFieldDefinition.newFieldDefinition().type(a); // omitted boilerplate from query object

Now, I want to return a list of A so I tried something like this:

GraphQLObjectType aList = GraphQLAnnotations.object(List.class);
...
GraphQLFieldDefinition.newFieldDefinition().type(aList); // omitted 

And this:

GraphQLObjectType aList = GraphQLAnnotations.object(new GraphQLList(A.class));
...
GraphQLFieldDefinition.newFieldDefinition().type(aList); // omitted 

Class A is annotated like this:

@GraphQLType
public class A {...}

First attempt returns null and the second attempt does not compile with GraphQLList cannot be applied to java.lang.Class<A>

A workaround is to create a wrapper object that holds the list but it seems like an ugly hack to me. How can I return a list of A using GraphQL java 2.3.0?

2 个答案:

答案 0 :(得分:1)

列表类型不是对象类型的子类型,因此您永远不能创建表示列表的对象类型。这两种类型都是输出类型(列表也是输入类型)。这应该是你需要的。

GraphQLObjectType a = GraphQLAnnotations.object(A.class);
GraphQLOutputType listOfA = new GraphQLList(a);

然后,您可以将此类型用作字段类型,就像使用GraphQLObjectType

一样
GraphQLFieldDefinition.newFieldDefinition().type(listOfA);

答案 1 :(得分:0)

我相信您需要执行以下操作:

GraphQLFieldDefinition.newFieldDefinition().type(new GraphQLList(A));