Skip to content

Commit 858d82f

Browse files
author
Alexei Pastuchov
committed
wundergraph_example supports MySql
1 parent 463dafc commit 858d82f

File tree

7 files changed

+642
-1
lines changed

7 files changed

+642
-1
lines changed

wundergraph_example/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ default-features = false
2929

3030
[features]
3131
default = ["postgres", "wundergraph/debug"]
32-
sqlite = ["wundergraph/sqlite", "diesel/sqlite"]
32+
mysql = ["wundergraph/mysql", "diesel/mysql"]
3333
postgres = ["wundergraph/postgres", "diesel/postgres"]
34+
sqlite = ["wundergraph/sqlite", "diesel/sqlite"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- This file should undo anything in `up.sql`
2+
3+
DROP TABLE friends;
4+
DROP TABLE appears_in;
5+
DROP TABLE heros;
6+
DROP TABLE home_worlds;
7+
DROP TABLE species;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
CREATE TABLE `species`(
2+
`id` INTEGER NOT NULL AUTO_INCREMENT,
3+
`name` TEXT NOT NULL,
4+
PRIMARY KEY (`id`)
5+
);
6+
7+
CREATE TABLE `home_worlds`(
8+
`id` INTEGER NOT NULL AUTO_INCREMENT,
9+
`name` TEXT NOT NULL,
10+
PRIMARY KEY (`id`)
11+
);
12+
13+
CREATE TABLE `heros`(
14+
`id` INTEGER NOT NULL AUTO_INCREMENT,
15+
`name` TEXT NOT NULL,
16+
`hair_color` TEXT,
17+
`species` INTEGER NOT NULL,
18+
`home_world` INTEGER DEFAULT NULL,
19+
PRIMARY KEY (`id`),
20+
FOREIGN KEY (`species`) REFERENCES `species` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT,
21+
FOREIGN KEY (`home_world`) REFERENCES `home_worlds` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
22+
);
23+
24+
CREATE TABLE `appears_in`(
25+
`hero_id` INTEGER NOT NULL,
26+
`episode` SMALLINT(1) NOT NULL CHECK(episode IN (1,2,3)),
27+
PRIMARY KEY(`hero_id`, `episode`),
28+
FOREIGN KEY (`hero_id`) REFERENCES `heros` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
29+
);
30+
31+
CREATE TABLE `friends`(
32+
`hero_id` INTEGER NOT NULL,
33+
`friend_id` INTEGER NOT NULL,
34+
PRIMARY KEY(`hero_id`, `friend_id`),
35+
FOREIGN KEY (`hero_id`) REFERENCES `heros`(`id`) ON DELETE CASCADE ON UPDATE RESTRICT,
36+
FOREIGN KEY (`friend_id`) REFERENCES `heros` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
37+
);
38+
39+
INSERT INTO species(id, name) VALUES (1, 'Human'), (2, 'Robot');
40+
41+
INSERT INTO home_worlds(id, name) VALUES(1, 'Tatooine'), (2, 'Alderaan');
42+
43+
INSERT INTO heros(id, name, species, home_world, hair_color)
44+
VALUES (1, 'Luke Skywalker', 1, 1, 'blond'),
45+
(2, 'Darth Vader', 1, 1, DEFAULT),
46+
(3, 'Han Solo', 1, Null, DEFAULT),
47+
(4, 'Leia Organa', 1, 2, DEFAULT),
48+
(5, 'Wilhuff Tarkin', 1, Null, DEFAULT);
49+
50+
INSERT INTO appears_in(hero_id, episode)
51+
VALUES (1, 1), (1, 2), (1, 3),
52+
(2, 1), (2, 2), (2, 3),
53+
(3, 1), (3, 2), (3, 3),
54+
(4, 1), (4, 2), (4, 3),
55+
(5, 3);
56+
57+
58+
INSERT INTO friends(hero_id, friend_id)
59+
VALUES (1, 3), (1, 4), (2, 5), (3, 1),
60+
(3, 4), (4, 1), (4, 3), (5, 2);

wundergraph_example/src/bin/wundergraph_example.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ fn run_migrations(conn: &DBConnection) {
8383
migration_path.push("pg");
8484
} else if cfg!(feature = "sqlite") {
8585
migration_path.push("sqlite");
86+
} else if cfg!(feature = "mysql") {
87+
migration_path.push("mysql");
8688
}
89+
8790
let pending_migrations =
8891
::diesel_migrations::mark_migrations_in_directory(conn, &migration_path)
8992
.unwrap()

wundergraph_example/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ pub type DBConnection = ::diesel::PgConnection;
261261
#[cfg(feature = "sqlite")]
262262
pub type DBConnection = ::diesel::SqliteConnection;
263263

264+
#[cfg(feature = "mysql")]
265+
pub type DBConnection = ::diesel::MysqlConnection;
266+
264267
pub type DbBackend = <DBConnection as Connection>::Backend;
265268

266269
pub type Schema<Ctx> =

wundergraph_example/src/mutations.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use super::HomeWorld;
1111
use super::Species;
1212
use juniper::*;
1313

14+
#[cfg(feature = "mysql")]
15+
pub mod mysql;
16+
1417
#[derive(Insertable, GraphQLInputObject, Clone, Debug)]
1518
#[table_name = "heros"]
1619
pub struct NewHero {

0 commit comments

Comments
 (0)