Skip to content

Commit eda74ac

Browse files
committed
Add documentation for Hibernate example
1 parent d12c423 commit eda74ac

File tree

5 files changed

+41
-42
lines changed

5 files changed

+41
-42
lines changed

hibernate-example/readme.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
# graphql-java-http-example
1+
# graphql-java-hibernate-example
22

3-
An example of using graphql-java in a HTTP application.
3+
An example of a simple graphql-java implementation backed by a Hibernate repository (via Spring Data JPA) and
4+
integrated with Spring Boot.
45

5-
It demonstrates the use of `Schema Definition Language (SDL)` to define a schema in a textual way.
6-
7-
It also shows how to use `data loader` to ensure that most efficient way to load
8-
inside a graphql query
6+
It shows how data fetchers can use Hibernate repositories to query the database.
97

10-
Finally it shows how to combine graphql-java `Instrumentation` to create performance tracing
11-
and data loader effectiveness statistics.
8+
In memory H2 is being used as the database, SQL scripts are execute during startup to add some sample data to
9+
the database. You can find those scripts in [data.sql](src/resources/data.sql).
1210

11+
# Running the example
1312
To build the code type
1413

1514
./gradlew build

hibernate-example/src/main/java/com/graphql/java/hibernate/example/StarWarsWiring.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414

1515
/**
1616
* This is our wiring used to put fetching behaviour behind a graphql field.
17+
*
18+
* Data fetchers declared here use {@link CharacterRepository} to query data from the database.
1719
*/
1820
@Component
1921
public class StarWarsWiring {
22+
/**
23+
* The autowired {@link CharacterRepository}, that will be used by the data fetcher to access the database.
24+
*/
2025
private final CharacterRepository characterRepository;
2126

2227
@Autowired
@@ -28,18 +33,30 @@ private CharacterRepository getCharacterRepository() {
2833
return characterRepository;
2934
}
3035

36+
/**
37+
* Fetches one record from the database based on the type and id.
38+
*
39+
*/
3140
DataFetcher humanDataFetcher = environment -> {
3241
String id = environment.getArgument("id");
3342

3443
return getCharacterRepository().findByTypeAndId(Human.class, Long.valueOf(id));
3544
};
3645

46+
/**
47+
* Fetches one record from the database based on the type and id.
48+
*/
3749
DataFetcher droidDataFetcher = environment -> {
3850
String id = environment.getArgument("id");
3951

4052
return getCharacterRepository().findByTypeAndId(Droid.class, Long.valueOf(id));
4153
};
4254

55+
/**
56+
* Fetches all characters that are associated with a given episode or a list of all characters if episode is not
57+
* specified.
58+
*
59+
*/
4360
DataFetcher charactersDataFetcher = environment -> {
4461
Episode episode = environment.getArgument("episode");
4562

@@ -48,6 +65,9 @@ private CharacterRepository getCharacterRepository() {
4865
getCharacterRepository().findByEpisode(episode);
4966
};
5067

68+
/**
69+
* Fetches a list of friends of a given character.
70+
*/
5171
DataFetcher friendsDataFetcher = environment -> {
5272
FilmCharacter character = environment.getSource();
5373

hibernate-example/src/main/java/com/graphql/java/hibernate/example/data/model/FilmCharacter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public abstract class FilmCharacter {
2424

2525
private String name;
2626

27+
/**
28+
* Eagerly fetches a list of friends ids.
29+
*/
2730
@ElementCollection(fetch = FetchType.EAGER)
2831
@CollectionTable(name = "CharacterFriend",
2932
joinColumns = @JoinColumn(name = "CharacterId"))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* This package contains the Hibernate code - entities and repository.
3+
*
4+
* There's absolutely nothing GraphQL specific about anything in this package. It could very well be replace with
5+
* other database library tools like QueryDSL, JDBC a NoSQL data repository, etc.
6+
*
7+
* Some might find that the entity relationship is not very trivial. We have an abstract entity `FilmCharacter` with
8+
* two concrete implementations: `Droid` and `Human`. This is to make this example support the same Star Wars GraphQL
9+
* Schema used on other examples in this repo.
10+
*/
11+
package com.graphql.java.hibernate.example.data;

hibernate-example/src/main/java/com/graphql/java/hibernate/example/package-info.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)