One of the nice thing about graphql is that it provides a sensible way to resolve cyclic references between objects. For example, if I have a many-to-many relationship between two object types House and Resident then I should be able to query either one from the other:
query {
house(address: "foo") {
residents {
name
age
}
}
}
or
query {
resident(name: "bar") {
houses {
address
}
}
}
However, using this library, if I try to create two objects with such a circular reference then I get a panic:
hosueType := &graphql.Object{...}
residentType := &graphql.Object{...}
houseType.Fields["residents"] = &graphql.Field{Type: graphql.NewList(residentType)}
residentType.Fields["houses"] = &graphql.Field{Type: graphql.NewList(houseType)}
Can you give me an example of how to construct such circular dependencies with this library?
You could try using AddFieldConfig
https://godoc.org/github.com/graphql-go/graphql#Interface.AddFieldConfig
houseType := &graphql.Object{...}
residentType := &graphql.Object{...}
houseType.AddFieldConfig("residents", &graphql.Field{Type: graphql.NewList(residentType)})
residentType.AddFieldConfig("houses", &graphql.Field{Type: graphql.NewList(houseType)})
Hello,
You can create circular dependencies at the initialization of your package
houseType := &graphql.Object{...}
residentType := &graphql.Object{...}
func init() {
houseType.AddFieldConfig("residents", &graphql.Field{Type: graphql.NewList(residentType)})
residentType.AddFieldConfig("s", &graphql.Field{Type: graphql.NewList(houseType)})
}
Most helpful comment
You could try using AddFieldConfig
https://godoc.org/github.com/graphql-go/graphql#Interface.AddFieldConfig