我想創建一個使用GraphQL Java Annotations的遞歸模式,但會引發異常。GraphQL Java註釋遞歸問題
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開始它是一個錯誤,並且註釋庫還沒有趕上。順便說一句,看看我的lib,[graphql-spqr](https://github.com/leangen/graphql-spqr),它允許更自動的模式生成。 – kaqqao
啊,這是一個無賴。在Github上是否存在這個問題?我找不到它,如果需要,我可以創建一個。 Graphql-spqr看起來非常棒,我會把它作爲測試騎:) :) – ifodor
將我的評論轉換爲答案並鏈接到跟蹤升級到3.0.0的問題,這也解決了您的問題。至於graphql-spqr,如果您需要幫助,請至[Gitter room](https://gitter.im/leangen/graphql-spqr)下載,至少在完成文檔編寫之前(我目前正在編寫該文檔)。 – kaqqao