Skip to content

Relationships

Igor Dianov edited this page May 29, 2024 · 2 revisions

GraphQL JPA Query engine will introspect JPA Entity Metadata entity class relationships to support ORM queries, i.e. given one to many relationship between Author and Book entities,

@Entity
public class Author {

    @Id
    Long id;

    String name;

    @OneToMany(mappedBy = "author", fetch = FetchType.LAZY)
    Set<Book> books;
}

@Entity
public class Book {

    @Id
    Long id;

    String title;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    Author author;

The generated query schema will add nested query fields on Books and Authors query types to map JPA ORM relationships:

query {
  Books {
    select {
      id
      title
      author {
        name
      }
    }
  }
  Authors {
    select {
      id
      name
      books {
        title
      }
    }
  }
}