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

Associations in mutation resolver - ability to commit deeply nested entities #1

Open
vadimbasko opened this issue Oct 9, 2020 · 0 comments

Comments

@vadimbasko
Copy link
Contributor

vadimbasko commented Oct 9, 2020

Relations between Car and Garage

Car.java

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "GARAGE_ID")
    protected Garage garage;

Garage.java

    @OneToMany(mappedBy = "garage")
    private List<Car> cars;

This allow us to make infinite nesting of relations in GraphQL queries

mutation {
	createGarage (
    garage: {
    	cars: [
        {
          garage: {
            cars: [
              {garage: {
                cars: []
              }}
            ]
          }
        }
      ]
    }) {
    name
  }
}

At this moment we are using a bit modified approach from REST API, which generates Entity java class and EntityImportView from client json. And then method walks through entity props tree navigated by generated view.

This approach is ok until we meet OneToMany relation with collection of objects. Each object in collection can contains separate set of properties and nesting level. See example below.

Example 1. Mutation that works fine

mutation {
	createGarage (garage: {
    name: "GG10",
    cars: [
      {manufacturer: "KIA", carType: "SEDAN"}, 
      {manufacturer: "ISUZU", carType: "SEDAN"}, 
    ]
  }) 
  
  # return
  {
    name
  }
}

json

{"name":"GG10","cars":[{"carType":"SEDAN","manufacturer":"KIA"},{"carType":"SEDAN","manufacturer":"ISUZU"}]}

entity import view

{"cars":{"carType":"","manufacturer":""},"name":""}

cars here is collection that can contains nested garage object.

Example 2. Mutation below is failed because we can not correctly calculate view for cars property

Each element of collection need to have each own view.

mutation {
  createGarage(garage: 
    {name: "GG10", 
      cars: [
        {manufacturer: "KIA", carType: "SEDAN"}, 
        {manufacturer: "ISUZU", 
          carType: "SEDAN", 
          garage: {name: "GG-Nested"}
        }]}) 
  
  # return
  {
    name
  }
}

json

{"name":"GG10","cars":[{"carType":"SEDAN","manufacturer":"KIA"},{"garage":{"name":"GG-Nested"},"carType":"SEDAN","manufacturer":"ISUZU"}]}

entity import view

{"cars":{"carType":"","garage":"","manufacturer":""},"name":""}
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

1 participant