graphql-java循环类型依赖项

时间:2016-07-07 08:15:32

标签: java graphql graphql-java

在尝试构建彼此依赖的类型时,我碰到了一堵砖墙,这里是代码:

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)

它无法实例化这个类,因为我得到了这个异常

Sub FindAndModify()

    Dim cel As Range
    Dim LookupValue As Double
    Dim intRow As Integer
    Dim intCol As Integer
    Dim celValue As Double

    LookupValue = "0.5" 'Define your Lookup Value here or read it from a cell

    For Each cel In Sheet1.UsedRange

        If cel.Value = LookupValue Then

            intRow = cel.Row
            intCol = cel.Column

            celValue = cel.Value

            celValue = celValue + Sheet1.Cells(intRow, 1).Value
            celValue = celValue + Sheet1.Cells(1, intCol).Value

            MsgBox "Value is " & celValue & " and address is " & cel.Address

            'Do stuff with your values

        End If

    Next cel

End Sub

很明显,classType尚未在createStudentType()引用它时被实例化。如何解决这个问题?

2 个答案:

答案 0 :(得分:6)

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();
    }

}

答案 1 :(得分:1)

您是否尝试使用new GraphQLTypeReference("ForwardType")?我正在谈论这个https://github.com/graphql-java/graphql-java#recursive-type-references