【发布时间】:2017-11-15 09:57:17
【问题描述】:
我正在尝试使用 GraphQL Java Annotations 创建递归模式,但会引发异常。
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.ThreadLocalRandom;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.annotations.GraphQLAnnotations;
import graphql.annotations.GraphQLDataFetcher;
import graphql.annotations.GraphQLDescription;
import graphql.annotations.GraphQLField;
import graphql.annotations.GraphQLName;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
public class RecursiveSchemaTest {
@GraphQLDescription("TestObject object")
@GraphQLName("TestObject")
public static class TestObject {
@GraphQLField
private Integer id;
@GraphQLField
@GraphQLDataFetcher(TestObjectDataFetcher.class)
private TestObject child;
public TestObject(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public TestObject getChild() {
return child;
}
public void setChild(TestObject child) {
this.child = child;
}
}
public static class TestObjectDataFetcher implements DataFetcher<TestObject> {
@Override
public TestObject get(DataFetchingEnvironment environment) {
return new TestObject(ThreadLocalRandom.current().nextInt());
}
}
@Test
public void test() {
GraphQLObjectType graphQLObjectType = GraphQLAnnotations.object(TestObject.class);
GraphQLObjectType rootQuery = GraphQLObjectType.newObject().name("data").field(
newFieldDefinition().name(graphQLObjectType.getName()).type(graphQLObjectType)
.dataFetcher(new TestObjectDataFetcher()).build()).build();
GraphQLSchema schema = GraphQLSchema.newSchema().query(rootQuery).build();
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
ExecutionResult result = graphQL.execute("{ TestObject { id, child { id , child { id }}");
Assert.assertFalse(result.getErrors() != null && !result.getErrors().isEmpty());
Assert.assertNotNull(result.getData());
}
}
解析类顺利进行,但创建架构会引发以下异常(此行:GraphQLSchema schema = GraphQLSchema.newSchema().query(rootQuery).build();):
graphql.AssertException: All types within a GraphQL schema must have unique names. No two provided types may have the same name.
No provided type may have a name which conflicts with any built in types (including Scalar and Introspection types).
You have redefined the type 'TestObject' from being a 'GraphQLObjectTypeWrapper' to a 'GraphQLObjectTypeWrapper'
at graphql.schema.SchemaUtil.assertTypeUniqueness(SchemaUtil.java:86)
at graphql.schema.SchemaUtil.collectTypesForObjects(SchemaUtil.java:122)
at graphql.schema.SchemaUtil.collectTypes(SchemaUtil.java:56)
at graphql.schema.SchemaUtil.collectTypesForObjects(SchemaUtil.java:128)
at graphql.schema.SchemaUtil.collectTypes(SchemaUtil.java:56)
at graphql.schema.SchemaUtil.collectTypesForObjects(SchemaUtil.java:128)
at graphql.schema.SchemaUtil.collectTypes(SchemaUtil.java:56)
at graphql.schema.SchemaUtil.allTypes(SchemaUtil.java:153)
at graphql.schema.GraphQLSchema.<init>(GraphQLSchema.java:42)
at graphql.schema.GraphQLSchema$Builder.build(GraphQLSchema.java:130)
at graphql.schema.GraphQLSchema$Builder.build(GraphQLSchema.java:125)
at RecursiveSchemaTest.test(RecursiveSchemaTest.java:74)
任何想法为什么没有正确创建架构?我正在使用最新版本的 graphql-java (3.0.0) 和 graphql-java-annotations (0.14.0)
【问题讨论】:
-
我相信这是一个正在处理的 graphql-java-annotation 错误(一位团队成员提到即将发布一个版本)。 graphql-java 的先前版本允许复制类型名称,但从 3.0.0 开始,这是一个错误,并且注释库还没有跟上。顺便说一句,看看我的库,graphql-spqr,它允许更自动化的模式生成。
-
啊,这太糟糕了。在 Github 上是否记录了此问题?我找不到它,如果需要,我可以创建一个。 Graphql-spqr 看起来非常棒,我会带它去试驾:)
-
将我的评论变成了答案并链接到跟踪升级到 3.0.0 的问题,这也解决了您的问题。至于 graphql-spqr,如果您需要帮助,请联系 Gitter room,至少在我完成文档编写之前(我目前正在处理它)。
-
在我的回答中添加了一个例子
标签: java recursion graphql graphql-java