Skip to content

Commit 34ad719

Browse files
committed
Change example to use Starwars entity schema
fixed #3
1 parent 1976a4d commit 34ad719

File tree

7 files changed

+254
-35
lines changed

7 files changed

+254
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.introproventures.graphql.jpa.query.example.model;
18+
19+
import java.util.Set;
20+
21+
import javax.persistence.ElementCollection;
22+
import javax.persistence.Entity;
23+
import javax.persistence.EnumType;
24+
import javax.persistence.Enumerated;
25+
import javax.persistence.Id;
26+
import javax.persistence.JoinColumn;
27+
import javax.persistence.JoinTable;
28+
import javax.persistence.ManyToMany;
29+
import javax.persistence.OrderBy;
30+
31+
import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;
32+
33+
import lombok.EqualsAndHashCode;
34+
import lombok.Getter;
35+
import lombok.Setter;
36+
import lombok.ToString;
37+
38+
@Entity
39+
@GraphQLDescription("Abstract representation of an entity in the Star Wars Universe")
40+
@Getter
41+
@Setter
42+
@ToString
43+
@EqualsAndHashCode(exclude={"appearsIn","friends"}) // Fixes NPE in Hibernate when initializing loaded collections #1
44+
public abstract class Character {
45+
46+
@Id
47+
@GraphQLDescription("Primary Key for the Character Class")
48+
String id;
49+
50+
@GraphQLDescription("Name of the character")
51+
String name;
52+
53+
@GraphQLDescription("Who are the known friends to this character")
54+
@ManyToMany
55+
@JoinTable(name="character_friends",
56+
joinColumns=@JoinColumn(name="source_id", referencedColumnName="id"),
57+
inverseJoinColumns=@JoinColumn(name="friend_id", referencedColumnName="id"))
58+
Set<Character> friends;
59+
60+
@GraphQLDescription("What Star Wars episodes does this character appear in")
61+
@ElementCollection(targetClass = Episode.class)
62+
@Enumerated(EnumType.ORDINAL)
63+
@OrderBy
64+
Set<Episode> appearsIn;
65+
66+
Character() {}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.introproventures.graphql.jpa.query.example.model;
18+
19+
import javax.persistence.Entity;
20+
import javax.persistence.Id;
21+
import javax.persistence.JoinColumn;
22+
import javax.persistence.ManyToOne;
23+
24+
import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;
25+
26+
import lombok.Data;
27+
28+
@Entity
29+
@GraphQLDescription("Database driven enumeration")
30+
@Data
31+
public class CodeList {
32+
33+
@Id
34+
@GraphQLDescription("Primary Key for the Code List Class")
35+
Long id;
36+
37+
String type;
38+
String code;
39+
Integer sequence;
40+
boolean active;
41+
String description;
42+
43+
@ManyToOne
44+
@JoinColumn(name = "parent_id")
45+
CodeList parent;
46+
47+
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@
1616

1717
package com.introproventures.graphql.jpa.query.example.model;
1818

19-
import java.util.Collection;
20-
2119
import javax.persistence.Entity;
22-
import javax.persistence.Id;
23-
import javax.persistence.OneToMany;
20+
21+
import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;
2422

2523
import lombok.Data;
24+
import lombok.EqualsAndHashCode;
2625

27-
@Data
2826
@Entity
29-
public class Author {
30-
@Id
31-
Long id;
27+
@GraphQLDescription("Represents an electromechanical robot in the Star Wars Universe")
28+
@Data
29+
@EqualsAndHashCode(callSuper=true)
30+
public class Droid extends Character {
3231

33-
String name;
32+
@GraphQLDescription("Documents the primary purpose this droid serves")
33+
String primaryFunction;
3434

35-
@OneToMany(mappedBy="author")
36-
Collection<Book> books;
3735
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616

1717
package com.introproventures.graphql.jpa.query.example.model;
1818

19-
public enum Genre {
20-
NOVEL, PLAY
19+
public enum Episode {
20+
21+
PHANTOM_MENACE,
22+
ATTACK_OF_THE_CLONES,
23+
REVENGE_OF_THE_SITH,
24+
A_NEW_HOPE,
25+
EMPIRE_STRIKES_BACK,
26+
RETURN_OF_THE_JEDI,
27+
THE_FORCE_AWAKENS
28+
2129
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,26 @@
1717
package com.introproventures.graphql.jpa.query.example.model;
1818

1919
import javax.persistence.Entity;
20-
import javax.persistence.EnumType;
21-
import javax.persistence.Enumerated;
22-
import javax.persistence.Id;
20+
import javax.persistence.FetchType;
21+
import javax.persistence.JoinColumn;
2322
import javax.persistence.ManyToOne;
2423

2524
import lombok.Data;
25+
import lombok.EqualsAndHashCode;
2626

27+
@Entity(name = "Human")
2728
@Data
28-
@Entity
29-
public class Book {
30-
@Id
31-
Long id;
29+
@EqualsAndHashCode(callSuper=true)
30+
public class Human extends Character {
3231

33-
String title;
32+
String homePlanet;
3433

35-
@ManyToOne
36-
Author author;
34+
@ManyToOne(fetch = FetchType.LAZY)
35+
@JoinColumn(name = "favorite_droid_id")
36+
Droid favoriteDroid;
37+
38+
@ManyToOne
39+
@JoinColumn(name = "gender_code_id")
40+
CodeList gender;
3741

38-
@Enumerated(EnumType.STRING)
39-
Genre genre;
4042
}

graphql-jpa-query-example/src/main/resources/application.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ spring:
88
graphql:
99
jpa:
1010
query:
11-
name: GraphQLJpaQueryExample
12-
description: GraphQL Jpa Query Example Books Schema Description
11+
name: GraphQLJpaQueryStarwars
12+
description: GraphQL Jpa Query Starwars Schema Example
1313
enabled: true
Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,104 @@
1-
-- Books
2-
insert into author (id, name) values (1, 'Leo Tolstoy');
3-
insert into book (id, title, author_id, genre) values (2, 'War and Peace', 1, 'NOVEL');
4-
insert into book (id, title, author_id, genre) values (3, 'Anna Karenina', 1, 'NOVEL');
5-
insert into author (id, name) values (4, 'Anton Chekhov');
6-
insert into book (id, title, author_id, genre) values (5, 'The Cherry Orchard', 4, 'PLAY');
7-
insert into book (id, title, author_id, genre) values (6, 'The Seagull', 4, 'PLAY');
8-
insert into book (id, title, author_id, genre) values (7, 'Three Sisters', 4, 'PLAY');
1+
-- Insert Code Lists
2+
insert into code_list (id, type, code, description, sequence, active, parent_id) values
3+
(0, 'org.crygier.graphql.model.starwars.Gender', 'Male', 'Male', 1, true, null),
4+
(1, 'org.crygier.graphql.model.starwars.Gender', 'Female', 'Female', 2, true, null);
5+
6+
-- Insert Droids
7+
insert into character (id, name, primary_function, dtype) values
8+
('2000', 'C-3PO', 'Protocol', 'Droid'),
9+
('2001', 'R2-D2', 'Astromech', 'Droid');
10+
11+
-- Insert Humans
12+
insert into character (id, name, home_planet, favorite_droid_id, dtype, gender_code_id) values
13+
('1000', 'Luke Skywalker', 'Tatooine', '2000', 'Human', 0),
14+
('1001', 'Darth Vader', 'Tatooine', '2001', 'Human', 0),
15+
('1002', 'Han Solo', NULL, NULL, 'Human', 0),
16+
('1003', 'Leia Organa', 'Alderaan', NULL, 'Human', 1),
17+
('1004', 'Wilhuff Tarkin', NULL, NULL, 'Human', 0);
18+
19+
-- Luke's friends
20+
insert into character_friends (source_id, friend_id) values
21+
('1000', '1002'),
22+
('1000', '1003'),
23+
('1000', '2000'),
24+
('1000', '2001');
25+
26+
-- Luke Appears in
27+
insert into character_appears_in (character_id, appears_in) values
28+
('1000', 3),
29+
('1000', 4),
30+
('1000', 5),
31+
('1000', 6);
32+
33+
-- Vader's friends
34+
insert into character_friends (source_id, friend_id) values
35+
('1001', '1004');
36+
37+
-- Vader Appears in
38+
insert into character_appears_in (character_id, appears_in) values
39+
('1001', 3),
40+
('1001', 4),
41+
('1001', 5);
42+
43+
-- Solo's friends
44+
insert into character_friends (source_id, friend_id) values
45+
('1002', '1000'),
46+
('1002', '1003'),
47+
('1002', '2001');
48+
49+
-- Solo Appears in
50+
insert into character_appears_in (character_id, appears_in) values
51+
('1002', 3),
52+
('1002', 4),
53+
('1002', 5),
54+
('1002', 6);
55+
56+
-- Leia's friends
57+
insert into character_friends (source_id, friend_id) values
58+
('1003', '1000'),
59+
('1003', '1002'),
60+
('1003', '2000'),
61+
('1003', '2001');
62+
63+
-- Leia Appears in
64+
insert into character_appears_in (character_id, appears_in) values
65+
('1003', 3),
66+
('1003', 4),
67+
('1003', 5),
68+
('1003', 6);
69+
70+
-- Wilhuff's friends
71+
insert into character_friends (source_id, friend_id) values
72+
('1004', '1001');
73+
74+
-- Wilhuff Appears in
75+
insert into character_appears_in (character_id, appears_in) values
76+
('1004', 3);
77+
78+
-- C3PO's friends
79+
insert into character_friends (source_id, friend_id) values
80+
('2000', '1000'),
81+
('2000', '1002'),
82+
('2000', '1003'),
83+
('2000', '2001');
84+
85+
-- C3PO Appears in
86+
insert into character_appears_in (character_id, appears_in) values
87+
('2000', 3),
88+
('2000', 4),
89+
('2000', 5),
90+
('2000', 6);
91+
92+
-- R2's friends
93+
insert into character_friends (source_id, friend_id) values
94+
('2001', '1000'),
95+
('2001', '1002'),
96+
('2001', '1003');
97+
98+
-- R2 Appears in
99+
insert into character_appears_in (character_id, appears_in) values
100+
('2001', 3),
101+
('2001', 4),
102+
('2001', 5),
103+
('2001', 6);
104+

0 commit comments

Comments
 (0)