Skip to content

Commit 0da2814

Browse files
author
Alexei Pastuchov
committed
wundergraph_example supports mysql
1 parent 61641d2 commit 0da2814

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

wundergraph_example/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0"
66
publish = false
77
repository = "https://github.com/weiznich/wundergraph"
88
readme = "../README.md"
9-
keywords = ["GraphQL", "ORM", "PostgreSQL", "SQLite"]
9+
keywords = ["GraphQL", "ORM", "PostgreSQL", "SQLite", "Mysql"]
1010
categories = ["database", "web-programming"]
1111
description = "A GraphQL ORM build on top of diesel"
1212
edition = "2018"
@@ -31,3 +31,4 @@ default-features = false
3131
default = ["postgres", "wundergraph/debug"]
3232
sqlite = ["wundergraph/sqlite", "diesel/sqlite"]
3333
postgres = ["wundergraph/postgres", "diesel/postgres"]
34+
mysql = ["wundergraph/mysql", "diesel/mysql"]
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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
CREATE TABLE species(
2+
id INT AUTO_INCREMENT PRIMARY KEY,
3+
name TEXT NOT NULL
4+
);
5+
6+
CREATE TABLE home_worlds(
7+
id INT AUTO_INCREMENT PRIMARY KEY,
8+
name TEXT NOT NULL
9+
);
10+
11+
CREATE TABLE heros(
12+
id INT AUTO_INCREMENT PRIMARY KEY,
13+
name TEXT NOT NULL,
14+
hair_color TEXT,
15+
species INTEGER NOT NULL REFERENCES species(id) ON DELETE CASCADE ON UPDATE RESTRICT,
16+
home_world INTEGER REFERENCES home_worlds(id) ON DELETE CASCADE ON UPDATE RESTRICT
17+
);
18+
19+
CREATE TABLE appears_in(
20+
hero_id INTEGER NOT NULL REFERENCES heros(id) ON DELETE CASCADE ON UPDATE RESTRICT,
21+
episode SMALLINT NOT NULL CHECK(episode IN (1,2,3)),
22+
PRIMARY KEY(hero_id, episode)
23+
);
24+
25+
CREATE TABLE friends(
26+
hero_id INTEGER NOT NULL REFERENCES heros(id) ON DELETE CASCADE ON UPDATE RESTRICT,
27+
friend_id INTEGER NOT NULL REFERENCES heros(id) ON DELETE CASCADE ON UPDATE RESTRICT,
28+
PRIMARY KEY(hero_id, friend_id)
29+
);
30+
31+
INSERT INTO species(id, name) VALUES (1, 'Human'), (2, 'Robot');
32+
33+
INSERT INTO home_worlds(id, name) VALUES(1, 'Tatooine'), (2, 'Alderaan');
34+
35+
INSERT INTO heros(id, name, species, home_world, hair_color)
36+
VALUES (1, 'Luke Skywalker', 1, 1, 'blond'),
37+
(2, 'Darth Vader', 1, 1, DEFAULT),
38+
(3, 'Han Solo', 1, Null, DEFAULT),
39+
(4, 'Leia Organa', 1, 2, DEFAULT),
40+
(5, 'Wilhuff Tarkin', 1, Null, DEFAULT);
41+
42+
INSERT INTO appears_in(hero_id, episode)
43+
VALUES (1, 1), (1, 2), (1, 3),
44+
(2, 1), (2, 2), (2, 3),
45+
(3, 1), (3, 2), (3, 3),
46+
(4, 1), (4, 2), (4, 3),
47+
(5, 3);
48+
49+
50+
INSERT INTO friends(hero_id, friend_id)
51+
VALUES (1, 3), (1, 4), (2, 5), (3, 1),
52+
(3, 4), (4, 1), (4, 3), (5, 2);

wundergraph_example/src/bin/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ 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
}
8789
let pending_migrations =
8890
::diesel_migrations::mark_migrations_in_directory(conn, &migration_path)

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::mysql::MysqlConnection;
266+
264267
pub type DbBackend = <DBConnection as Connection>::Backend;
265268

266269
pub type Schema<Ctx> =

0 commit comments

Comments
 (0)