Skip to content

Commit

Permalink
Change example to use Starwars entity schema
Browse files Browse the repository at this point in the history
fixed #3
  • Loading branch information
igdianov committed Sep 21, 2017
1 parent 1976a4d commit 34ad719
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import java.util.Set;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;

import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Entity
@GraphQLDescription("Abstract representation of an entity in the Star Wars Universe")
@Getter
@Setter
@ToString
@EqualsAndHashCode(exclude={"appearsIn","friends"}) // Fixes NPE in Hibernate when initializing loaded collections #1
public abstract class Character {

@Id
@GraphQLDescription("Primary Key for the Character Class")
String id;

@GraphQLDescription("Name of the character")
String name;

@GraphQLDescription("Who are the known friends to this character")
@ManyToMany
@JoinTable(name="character_friends",
joinColumns=@JoinColumn(name="source_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="friend_id", referencedColumnName="id"))
Set<Character> friends;

@GraphQLDescription("What Star Wars episodes does this character appear in")
@ElementCollection(targetClass = Episode.class)
@Enumerated(EnumType.ORDINAL)
@OrderBy
Set<Episode> appearsIn;

Character() {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2017 IntroPro Ventures Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;

import lombok.Data;

@Entity
@GraphQLDescription("Database driven enumeration")
@Data
public class CodeList {

@Id
@GraphQLDescription("Primary Key for the Code List Class")
Long id;

String type;
String code;
Integer sequence;
boolean active;
String description;

@ManyToOne
@JoinColumn(name = "parent_id")
CodeList parent;

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@

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

import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import com.introproventures.graphql.jpa.query.annotation.GraphQLDescription;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@Entity
public class Author {
@Id
Long id;
@GraphQLDescription("Represents an electromechanical robot in the Star Wars Universe")
@Data
@EqualsAndHashCode(callSuper=true)
public class Droid extends Character {

String name;
@GraphQLDescription("Documents the primary purpose this droid serves")
String primaryFunction;

@OneToMany(mappedBy="author")
Collection<Book> books;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

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

public enum Genre {
NOVEL, PLAY
public enum Episode {

PHANTOM_MENACE,
ATTACK_OF_THE_CLONES,
REVENGE_OF_THE_SITH,
A_NEW_HOPE,
EMPIRE_STRIKES_BACK,
RETURN_OF_THE_JEDI,
THE_FORCE_AWAKENS

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,26 @@
package com.introproventures.graphql.jpa.query.example.model;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Entity(name = "Human")
@Data
@Entity
public class Book {
@Id
Long id;
@EqualsAndHashCode(callSuper=true)
public class Human extends Character {

String title;
String homePlanet;

@ManyToOne
Author author;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "favorite_droid_id")
Droid favoriteDroid;

@ManyToOne
@JoinColumn(name = "gender_code_id")
CodeList gender;

@Enumerated(EnumType.STRING)
Genre genre;
}
4 changes: 2 additions & 2 deletions graphql-jpa-query-example/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ spring:
graphql:
jpa:
query:
name: GraphQLJpaQueryExample
description: GraphQL Jpa Query Example Books Schema Description
name: GraphQLJpaQueryStarwars
description: GraphQL Jpa Query Starwars Schema Example
enabled: true
112 changes: 104 additions & 8 deletions graphql-jpa-query-example/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,104 @@
-- Books
insert into author (id, name) values (1, 'Leo Tolstoy');
insert into book (id, title, author_id, genre) values (2, 'War and Peace', 1, 'NOVEL');
insert into book (id, title, author_id, genre) values (3, 'Anna Karenina', 1, 'NOVEL');
insert into author (id, name) values (4, 'Anton Chekhov');
insert into book (id, title, author_id, genre) values (5, 'The Cherry Orchard', 4, 'PLAY');
insert into book (id, title, author_id, genre) values (6, 'The Seagull', 4, 'PLAY');
insert into book (id, title, author_id, genre) values (7, 'Three Sisters', 4, 'PLAY');
-- Insert Code Lists
insert into code_list (id, type, code, description, sequence, active, parent_id) values
(0, 'org.crygier.graphql.model.starwars.Gender', 'Male', 'Male', 1, true, null),
(1, 'org.crygier.graphql.model.starwars.Gender', 'Female', 'Female', 2, true, null);

-- Insert Droids
insert into character (id, name, primary_function, dtype) values
('2000', 'C-3PO', 'Protocol', 'Droid'),
('2001', 'R2-D2', 'Astromech', 'Droid');

-- Insert Humans
insert into character (id, name, home_planet, favorite_droid_id, dtype, gender_code_id) values
('1000', 'Luke Skywalker', 'Tatooine', '2000', 'Human', 0),
('1001', 'Darth Vader', 'Tatooine', '2001', 'Human', 0),
('1002', 'Han Solo', NULL, NULL, 'Human', 0),
('1003', 'Leia Organa', 'Alderaan', NULL, 'Human', 1),
('1004', 'Wilhuff Tarkin', NULL, NULL, 'Human', 0);

-- Luke's friends
insert into character_friends (source_id, friend_id) values
('1000', '1002'),
('1000', '1003'),
('1000', '2000'),
('1000', '2001');

-- Luke Appears in
insert into character_appears_in (character_id, appears_in) values
('1000', 3),
('1000', 4),
('1000', 5),
('1000', 6);

-- Vader's friends
insert into character_friends (source_id, friend_id) values
('1001', '1004');

-- Vader Appears in
insert into character_appears_in (character_id, appears_in) values
('1001', 3),
('1001', 4),
('1001', 5);

-- Solo's friends
insert into character_friends (source_id, friend_id) values
('1002', '1000'),
('1002', '1003'),
('1002', '2001');

-- Solo Appears in
insert into character_appears_in (character_id, appears_in) values
('1002', 3),
('1002', 4),
('1002', 5),
('1002', 6);

-- Leia's friends
insert into character_friends (source_id, friend_id) values
('1003', '1000'),
('1003', '1002'),
('1003', '2000'),
('1003', '2001');

-- Leia Appears in
insert into character_appears_in (character_id, appears_in) values
('1003', 3),
('1003', 4),
('1003', 5),
('1003', 6);

-- Wilhuff's friends
insert into character_friends (source_id, friend_id) values
('1004', '1001');

-- Wilhuff Appears in
insert into character_appears_in (character_id, appears_in) values
('1004', 3);

-- C3PO's friends
insert into character_friends (source_id, friend_id) values
('2000', '1000'),
('2000', '1002'),
('2000', '1003'),
('2000', '2001');

-- C3PO Appears in
insert into character_appears_in (character_id, appears_in) values
('2000', 3),
('2000', 4),
('2000', 5),
('2000', 6);

-- R2's friends
insert into character_friends (source_id, friend_id) values
('2001', '1000'),
('2001', '1002'),
('2001', '1003');

-- R2 Appears in
insert into character_appears_in (character_id, appears_in) values
('2001', 3),
('2001', 4),
('2001', 5),
('2001', 6);

0 comments on commit 34ad719

Please sign in to comment.