Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating objects with circular references between fields #322

Closed
alexflint opened this issue May 1, 2018 · 2 comments
Closed

Creating objects with circular references between fields #322

alexflint opened this issue May 1, 2018 · 2 comments

Comments

@alexflint
Copy link
Contributor

alexflint commented May 1, 2018

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?

@jenarvaezg
Copy link

jenarvaezg commented May 15, 2018

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

@dohr-michael
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants