Skip to content

Commit a6c43d7

Browse files
committed
Switch to sakila database as example
1 parent cc1bf48 commit a6c43d7

File tree

7 files changed

+534
-743
lines changed

7 files changed

+534
-743
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jooq {
6060

6161
jdbc.apply {
6262
driver = "org.postgresql.Driver"
63-
url = "jdbc:postgresql://localhost:5432/byos_schema_names"
63+
url = "jdbc:postgresql://localhost:5432/sakila"
6464
user = "postgres"
6565
password = ""
6666
}

src/main/kotlin/byos/JooqHelpers.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import java.sql.DriverManager
1414

1515
private const val userName = "postgres"
1616
private const val password = ""
17-
private const val url = "jdbc:postgresql://localhost:5432/byos_schema_names"
17+
private const val url = "jdbc:postgresql://localhost:5432/sakila"
1818

1919
fun <T> executeJooqQuery(withDsl: (dsl: DSLContext) -> T): T {
2020
val connection = DriverManager.getConnection(url, userName, password)

src/main/kotlin/byos/Logic.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ fun resolveInternalQueryTree(relation: InternalQueryNode.Relation, joinCondition
132132

133133
val (relations, attributes) = relation.children.partition { it is InternalQueryNode.Relation }
134134
val attributeNames = attributes.distinctBy { it.graphQLAlias }
135-
.map { attribute -> outerTable.field(attribute.graphQLFieldName.lowercase())!!.`as`(attribute.graphQLAlias) }
135+
.map { attribute ->
136+
outerTable.field(attribute.graphQLFieldName.lowercase())?.`as`(attribute.graphQLAlias)
137+
?: error("Field ${attribute.graphQLFieldName} does not exist on table $outerTable")
138+
}
136139

137140
val subSelects = relations.map { subRelation ->
138141
val innerTable = getTableWithAlias(subRelation as InternalQueryNode.Relation)
@@ -265,7 +268,7 @@ fun resolveInternalQueryTree(relation: InternalQueryNode.Relation, joinCondition
265268
}
266269

267270
private fun getTableWithAlias(relation: InternalQueryNode.Relation) =
268-
PUBLIC.getTable(relation.fieldTypeInfo.relationName)?.`as`(relation.sqlAlias) ?: error("Table not found")
271+
PUBLIC.getTable(relation.fieldTypeInfo.relationName)?.`as`(relation.sqlAlias) ?: error("Table ${relation.fieldTypeInfo.relationName} not found")
269272

270273
fun getFieldTypeInfo(schema: GraphQLSchema, fieldName: String, typeName: String): FieldTypeInfo {
271274
val type = schema.getType(typeName) as? GraphQLObjectType ?: error("Type '$typeName' not found in schema")

src/main/kotlin/byos/WhereCondition.kt

+35-22
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package byos
22

33
import com.fasterxml.jackson.databind.ObjectMapper
4-
import db.jooq.generated.Tables.BOOK
5-
import db.jooq.generated.Tables.BOOK_TO_BOOKSTORE
6-
import db.jooq.generated.tables.Author
7-
import db.jooq.generated.tables.Book
8-
import db.jooq.generated.tables.BookToBookstore
9-
import db.jooq.generated.tables.Bookstore
4+
import db.jooq.generated.Tables.FILM_ACTOR
5+
import db.jooq.generated.Tables.FILM_CATEGORY
6+
import db.jooq.generated.Tables.INVENTORY
7+
import db.jooq.generated.tables.Actor
8+
import db.jooq.generated.tables.Category
9+
import db.jooq.generated.tables.Film
10+
import db.jooq.generated.tables.Inventory
1011
import db.jooq.generated.tables.Language
11-
import db.jooq.generated.tables.Shoporder
12-
import db.jooq.generated.tables.Shopuser
13-
import db.jooq.generated.tables.Tree
12+
import db.jooq.generated.tables.Store
1413
import graphql.language.Argument
1514
import graphql.language.ArrayValue
1615
import graphql.language.BooleanValue
@@ -31,21 +30,35 @@ import org.jooq.impl.DSL
3130
object WhereCondition {
3231
fun getForRelationship(relationshipName: String, left: Table<*>, right: Table<*>): Condition =
3332
when {
34-
relationshipName == "author" && left is Book && right is Author -> left.AUTHORID.eq(right.ID)
35-
relationshipName == "books" && left is Author && right is Book -> right.AUTHORID.eq(left.ID)
36-
relationshipName == "user" && left is Shoporder && right is Shopuser -> right.USER_ID.eq(left.USER_ID)
37-
relationshipName == "orders" && left is Shopuser && right is Shoporder -> right.USER_ID.eq(left.USER_ID)
38-
relationshipName == "children" && left is Tree && right is Tree -> left.ID.eq(right.PARENT_ID)
39-
relationshipName == "parent" && left is Tree && right is Tree -> right.ID.eq(left.PARENT_ID)
40-
relationshipName == "books" && left is Bookstore && right is Book -> DSL.exists(
41-
DSL.selectOne().from(BOOK_TO_BOOKSTORE).where(left.NAME.eq(BOOK_TO_BOOKSTORE.NAME).and(BOOK_TO_BOOKSTORE.BOOKID.eq(right.ID)))
33+
relationshipName == "actors" && left is Film && right is Actor -> DSL.exists(
34+
DSL.selectOne().from(FILM_ACTOR).where(left.FILM_ID.eq(FILM_ACTOR.FILM_ID).and(FILM_ACTOR.ACTOR_ID.eq(right.ACTOR_ID)))
4235
)
4336

44-
relationshipName == "b2b" && left is Bookstore && right is BookToBookstore -> left.NAME.eq(right.NAME)
45-
relationshipName == "book" && left is BookToBookstore && right is Book -> left.BOOKID.eq(right.ID)
46-
relationshipName == "language" && left is Book && right is Language -> left.LANGUAGEID.eq(right.ID)
47-
relationshipName == "publicationLanguages" && left is Author && right is Language -> DSL.exists(
48-
DSL.selectOne().from(BOOK).where(left.ID.eq(BOOK.AUTHORID).and(BOOK.LANGUAGEID.eq(right.ID)))
37+
relationshipName == "films" && left is Actor && right is Film -> DSL.exists(
38+
DSL.selectOne().from(FILM_ACTOR).where(left.ACTOR_ID.eq(FILM_ACTOR.ACTOR_ID).and(FILM_ACTOR.FILM_ID.eq(right.FILM_ID)))
39+
)
40+
41+
relationshipName == "stores" && left is Film && right is Store -> DSL.exists(
42+
DSL.selectOne().from(INVENTORY).where(left.FILM_ID.eq(INVENTORY.FILM_ID).and(INVENTORY.STORE_ID.eq(right.STORE_ID)))
43+
)
44+
45+
relationshipName == "films" && left is Store && right is Film -> DSL.exists(
46+
DSL.selectOne().from(INVENTORY).where(left.STORE_ID.eq(INVENTORY.STORE_ID).and(INVENTORY.FILM_ID.eq(right.FILM_ID)))
47+
)
48+
49+
relationshipName == "language" && left is Film && right is Language -> left.LANGUAGE_ID.eq(right.LANGUAGE_ID)
50+
relationshipName == "original_language" && left is Film && right is Language -> left.ORIGINAL_LANGUAGE_ID.eq(right.LANGUAGE_ID)
51+
52+
relationshipName == "inventories" && left is Store && right is Inventory -> left.STORE_ID.eq(right.STORE_ID)
53+
54+
relationshipName == "film" && left is Inventory && right is Film -> left.FILM_ID.eq(right.FILM_ID)
55+
56+
relationshipName == "categories" && left is Film && right is Category -> DSL.exists(
57+
DSL.selectOne().from(FILM_CATEGORY).where(left.FILM_ID.eq(FILM_CATEGORY.FILM_ID).and(FILM_CATEGORY.CATEGORY_ID.eq(right.CATEGORY_ID)))
58+
)
59+
60+
relationshipName == "films" && left is Category && right is Film -> DSL.exists(
61+
DSL.selectOne().from(FILM_CATEGORY).where(left.CATEGORY_ID.eq(FILM_CATEGORY.CATEGORY_ID).and(FILM_CATEGORY.FILM_ID.eq(right.FILM_ID)))
4962
)
5063

5164
else -> error("No relationship called $relationshipName found for tables $left and $right")
+56-125
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
type Query {
2-
allAuthors: [Author!]!
3-
authorById(id: ID!): Author
4-
authorsByIds(id: [ID!]!): [[Author]]!
5-
allBooks(first: Int, after: String, orderBy: OrderBooksBy): BookConnection!
6-
test: Test!
7-
allOrders(first: Int): ShopOrderConnection!
8-
allUsers(first: Int): ShopUserConnection!
9-
allTrees(first: Int): TreeConnection!
10-
allBookStores(first: Int): BookStoreConnection!
11-
booksByYear(publishedin: Int!, first: Int): BookConnection!
12-
distinguishedAuthors(distinguished: Boolean): [Author!]!
13-
distinguishedAuthorsList(distinguished: [Boolean]!): [Author!]!
14-
allProducts(first: Int, after: String, orderBy: OrderProductsBy): ProductConnection!
2+
allFilms(first: Int, after: String, orderBy: OrderFilmsBy): FilmConnection!
3+
filmById(film_id: ID!): Film
4+
filmByIds(first: Int, after: String, film_id: [ID!]!): FilmConnection!
5+
allActors: [Actor!]!
6+
actorById(actor_id: ID!): Actor
7+
allCategories(first: Int, after: String): CategoryConnection!
8+
allStores(first: Int, after: String): StoreConnection!
9+
filmsByYear(first: Int, after: String, release_year: Int!): FilmConnection!
1510
}
1611

1712
enum SortDirection {
@@ -24,159 +19,95 @@ type PageInfo {
2419
endCursor: String
2520
}
2621

27-
input OrderBooksBy {
28-
id: SortDirection
29-
title: SortDirection
30-
publishedin: SortDirection
31-
}
32-
33-
type Test {
34-
value: String
35-
}
36-
37-
type Book {
38-
id: ID!
22+
type Film {
23+
film_id: ID!
3924
title: String!
40-
publishedin: Int!
41-
author: Author!
25+
release_year: Int
26+
actors: [Actor!]!
27+
categories(first: Int, after: String): CategoryConnection!
28+
stores: StoreConnection!
4229
language: Language!
30+
original_language: Language
4331
}
4432

45-
type BookConnection {
46-
edges: [BookEdge!]!
47-
totalCount: Int!
48-
pageInfo: PageInfo!
49-
}
50-
51-
type BookEdge {
52-
node: Book!
53-
cursor: String!
54-
}
55-
56-
# example for connection without pagination
57-
type Author {
58-
id: ID!
59-
firstName: String
60-
lastName: String!
61-
books(publishedin: Int, first: Int): BookConnection!
62-
distinguished: Boolean
63-
publicationLanguages: [Language!]!
64-
}
65-
66-
type BookStore {
67-
name: String!
68-
books(first: Int): BookConnection!
69-
b2b: B2BConnection!
70-
}
71-
72-
type BookStoreConnection {
73-
edges: [BookStoreEdge!]!
74-
totalCount: Int!
75-
pageInfo: PageInfo!
76-
}
77-
78-
type BookStoreEdge {
79-
node: BookStore!
80-
cursor: String!
81-
}
82-
83-
type Book_To_Bookstore {
84-
book: Book!
85-
stock: Int
33+
input OrderFilmsBy {
34+
film_id: SortDirection
35+
title: SortDirection
36+
release_year: SortDirection
8637
}
8738

88-
type B2BConnection {
89-
edges: [B2BEdge!]!
39+
type FilmConnection {
40+
edges: [FilmEdge!]!
9041
totalCount: Int!
9142
pageInfo: PageInfo!
9243
}
9344

94-
type B2BEdge {
95-
node: Book_To_Bookstore!
45+
type FilmEdge {
46+
node: Film!
9647
cursor: String!
9748
}
9849

99-
type Language {
100-
id: ID!
101-
cd: String!
102-
description: String
50+
type Actor {
51+
actor_id: ID!
52+
first_name: String!
53+
last_name: String!
54+
films(first: Int, after: String, orderBy: OrderFilmsBy, release_year: Int): FilmConnection!
10355
}
10456

105-
type ShopUser {
106-
user_id: ID!
107-
username: String!
108-
email: String!
109-
orders(first: Int): ShopOrderConnection!
57+
type Store {
58+
store_id: ID!
59+
films(first: Int, after: String, orderBy: OrderFilmsBy): FilmConnection!
60+
inventories(first: Int, after: String): InventoryConnection!
11061
}
11162

112-
type ShopUserConnection {
113-
edges: [ShopUserEdge!]!
63+
type StoreConnection {
64+
edges: [StoreEdge!]!
11465
totalCount: Int!
11566
pageInfo: PageInfo!
11667
}
11768

118-
type ShopUserEdge {
119-
node: ShopUser!
69+
type StoreEdge {
70+
node: Store!
12071
cursor: String!
12172
}
12273

123-
type ShopOrder {
124-
order_id: ID!
125-
order_number: String!
126-
user: ShopUser
127-
order_date: String!
128-
}
129-
130-
type ShopOrderConnection {
131-
edges: [ShopOrderEdge!]!
132-
totalCount: Int!
133-
pageInfo: PageInfo!
134-
}
135-
136-
type ShopOrderEdge {
137-
node: ShopOrder!
138-
cursor: String!
74+
type Language {
75+
language_id: ID!
76+
name: String!
13977
}
14078

141-
type Tree {
142-
id: ID!
143-
label: String!
144-
parent: Tree
145-
children(first: Int): TreeConnection!
79+
type Inventory {
80+
inventory_id: ID!
81+
film: Film!
82+
# stock: Int!
14683
}
14784

148-
type TreeConnection {
149-
edges: [TreeEdge!]!
85+
type InventoryConnection {
86+
edges: [InventoryEdge!]!
15087
totalCount: Int!
15188
pageInfo: PageInfo!
15289
}
15390

154-
type TreeEdge {
155-
node: Tree!
91+
type InventoryEdge {
92+
node: Inventory!
15693
cursor: String!
15794
}
15895

159-
type Product {
160-
id: ID!
96+
type Category {
97+
category_id: ID!
16198
name: String!
162-
price: Float!
163-
category: String!
164-
}
165-
166-
input OrderProductsBy {
167-
id: SortDirection
168-
name: SortDirection
169-
price: SortDirection
170-
category: SortDirection
99+
films(first: Int, after: String, orderBy: OrderFilmsBy): FilmConnection!
100+
# parent: Category
101+
# children: [Category!]!
171102
}
172103

173-
type ProductConnection {
174-
edges: [ProductEdge!]!
104+
type CategoryConnection {
105+
edges: [CategoryEdge!]!
175106
totalCount: Int!
176107
pageInfo: PageInfo!
177108
}
178109

179-
type ProductEdge {
180-
node: Product!
110+
type CategoryEdge {
111+
node: Category!
181112
cursor: String!
182113
}

0 commit comments

Comments
 (0)