graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.86k stars 838 forks source link

How to solve the error of "cyclic definition detected" #603

Closed LeonYanghaha closed 3 years ago

LeonYanghaha commented 3 years ago

type Nodes struct {
    Id        string    `json:"uid"`
    Status    string    `json:"Node.status"`
    Note      string    `json:"Node.note"`
    Exec      string    `json:"Node.exec"`
    Condition Condition `json:"Node.condition"`
    To        []*Nodes  `json:"Node.to"`
}

Just like the above code, I defined a node whose to field is a node of the same type. However, when I describe the struct with graphql. Newobject, it prompts me to cycle definition detected. How can I solve this error. Who can tell me. thank you very much. This is my code to use graphql. Newobject


var NodesType = graphql.NewObject(
    graphql.ObjectConfig{
        Name: "Nodes",
        Fields: graphql.Fields{
            // ....
            "To": &graphql.Field{
                Type:        NodesType,
                Description: "",
            },
        },
    },
)
LeonYanghaha commented 3 years ago

I found a solution. It looks good. Write it all down, in case someone comes across it later.