2016-07-07 8 views
2

互いに依存型を構築しようとすると、ここにコードがある一方で、私はレンガの壁にヒットしました:私はこれを取得として、このクラスをintantiateするgraphql-javaの巡回種類の依存関係

import graphql.schema.GraphQLObjectType; 
import static graphql.schema.GraphQLObjectType.newObject; 

import static graphql.Scalars.*; 
import graphql.schema.GraphQLFieldDefinition; 
import graphql.schema.GraphQLList; 

import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; 

public class GraphQLTypes { 

    private GraphQLObjectType studentType; 
    private GraphQLObjectType classType; 

    public GraphQLTypes() { 

     createStudentType(); 
     createClassType(); 
    } 

    void createStudentType() { 
     studentType = newObject().name("Student") 
       .field(newFieldDefinition().name("name").type(GraphQLString).build()) 
      .field(newFieldDefinition().name("currentClass").type(classType).build()) 
      .build(); 
    } 

    void createClassType() { 
     classType = newObject().name("Class") 
      .field(newFieldDefinition().name("name").type(GraphQLString).build()) 
      .field(newFieldDefinition().name("students").type(new GraphQLList(studentType)).build()) 
      .build(); 
    } 

} 

その不可能例外明らかにするClassTypeがまだ点createStudentType(AT intantiatedれない

Caused by: graphql.AssertException: type can't be null 
at graphql.Assert.assertNotNull(Assert.java:10) 
at graphql.schema.GraphQLFieldDefinition.<init>(GraphQLFieldDefinition.java:23) 
at graphql.schema.GraphQLFieldDefinition$Builder.build(GraphQLFieldDefinition.java:152) 
at graphql_types.GraphQLTypes.createStudentType(GraphQLTypes.java:26) 
at graphql_types.GraphQLTypes.<init>(GraphQLTypes.java:19) 

)は、それを参照しています。どのように私はこの問題を回避するには?

答えて

4

GraphQLTypeReferenceは確かに答えです。これでいいはずです:

import graphql.schema.GraphQLList; 
import graphql.schema.GraphQLObjectType; 
import graphql.schema.GraphQLTypeReference; 

import static graphql.Scalars.GraphQLString; 
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; 
import static graphql.schema.GraphQLObjectType.newObject; 

public class GraphQLTypes { 

    private GraphQLObjectType studentType; 
    private GraphQLObjectType classType; 

    public GraphQLTypes() { 
     createStudentType(); 
     createClassType(); 
    } 

    void createStudentType() { 
     studentType = newObject().name("Student") 
       .field(newFieldDefinition().name("name").type(GraphQLString).build()) 
       .field(newFieldDefinition().name("currentClass").type(new GraphQLTypeReference("Class")).build()) 
       .build(); 
    } 

    void createClassType() { 
     classType = newObject().name("Class") 
       .field(newFieldDefinition().name("name").type(GraphQLString).build()) 
       .field(newFieldDefinition().name("students").type(new GraphQLList(studentType)).build()) 
       .build(); 
    } 

} 
+0

Presto !!!ありがとう。 – Peace