Skip to content

Commit 7e8f720

Browse files
committed
[SeaORM] Edit
1 parent e3c5c4a commit 7e8f720

File tree

5 files changed

+182
-25
lines changed

5 files changed

+182
-25
lines changed

Blog/blog/2024-07-01-graphql-support-with-loco-seaography.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ You can generate Seaography Entities by using `sea-orm-cli` with the extra `--se
4141
sea-orm-cli generate entity -o src/models/_entities -u postgres://loco:loco@localhost:5432/loco_seaography_development --seaography
4242
```
4343

44-
```diff title="loco_seaography/src/models/_entities/notes.rs"
44+
```rust title="loco_seaography/src/models/_entities/notes.rs"
4545
use sea_orm::entity::prelude::*;
4646
use serde::{Serialize, Deserialize};
4747

SeaORM/docs/09-schema-statement/01-create-table.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ assert_eq!(
4747
);
4848
```
4949

50-
To further illustrate it, we will show the SQL statement as string below.
50+
To further illustrate, we will show the SQL statements as string below.
5151

5252
- PostgreSQL
5353
```rust

SeaORM/docs/09-schema-statement/03-create-index.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,48 @@ You can create indices from entities using [`Schema::create_index_from_entity`](
55
Example [`Indexes`](https://github.com/SeaQL/sea-orm/blob/master/src/tests_cfg/indexes.rs) entity:
66

77
```rust title="indexes.rs"
8-
impl ColumnTrait for Column {
9-
type EntityName = Entity;
10-
11-
fn def(&self) -> ColumnDef {
12-
match self {
13-
Self::Index1Attr => ColumnType::Integer.def().indexed(),
14-
Self::Index2Attr => ColumnType::Integer.def().indexed().unique(),
15-
}
16-
}
8+
pub struct Model {
9+
#[sea_orm(primary_key)]
10+
pub id: i32,
11+
#[sea_orm(indexed)]
12+
pub index1_attr: i32,
13+
#[sea_orm(unique, indexed)]
14+
pub index2_attr: i32,
15+
#[sea_orm(unique_key = "my_unique")]
16+
pub unique_key_a: String,
17+
#[sea_orm(unique_key = "my_unique")]
18+
pub unique_key_b: String,
1719
}
1820
```
1921

2022
```rust
2123
use sea_orm::{sea_query, tests_cfg::*, Schema};
2224

2325
let builder = db.get_database_backend();
24-
let schema = Schema::new(builder);
2526

26-
let stmts = schema.create_index_from_entity(indexes::Entity);
27+
let stmts = Schema::new(builder).create_index_from_entity(indexes::Entity);
2728

28-
let idx = sea_query::Index::create()
29+
let idx: IndexCreateStatement = Index::create()
2930
.name("idx-indexes-index1_attr")
3031
.table(indexes::Entity)
3132
.col(indexes::Column::Index1Attr)
3233
.to_owned();
3334
assert_eq!(builder.build(&stmts[0]), builder.build(&idx));
3435

35-
let idx = sea_query::Index::create()
36+
let idx: IndexCreateStatement = Index::create()
3637
.name("idx-indexes-index2_attr")
3738
.table(indexes::Entity)
3839
.col(indexes::Column::Index2Attr)
39-
.to_owned();
40+
.unique()
41+
.take();
4042
assert_eq!(builder.build(&stmts[1]), builder.build(&idx));
43+
44+
let idx: IndexCreateStatement = Index::create()
45+
.name("idx-indexes-my_unique")
46+
.table(indexes::Entity)
47+
.col(indexes::Column::UniqueKeyA)
48+
.col(indexes::Column::UniqueKeyB)
49+
.unique()
50+
.take();
51+
assert_eq!(builder.build(&stmts[2]), builder.build(&idx));
4152
```

SeaORM/docs/10-graph-ql/01-seaography-intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
If you are building a full-stack application with a web GUI these days, it's likely you'd use GraphQL as the communication interface between frontend and backend. However, building GraphQL resolvers is no easy task for backend developers.
44

5-
[Seaography](https://github.com/SeaQL/seaography) is a GraphQL framework built on top of SeaORM and [async-graphql](https://github.com/async-graphql/async-graphql). With just a few commands, you can launch a GraphQL server from SeaORM entities!
5+
[Seaography](https://github.com/SeaQL/seaography) is a GraphQL framework built on top of SeaORM and [async-graphql](https://github.com/async-graphql/async-graphql). Given a set of SeaORM entities, you can instantly launch a fully-featured GraphQL server / resolver with relational query, filters, pagination and mutations.
66

77
SeaORM is dynamic by design with first-class GraphQL support. `async-graphql` `v5.0` introduced [dynamic schema](https://docs.rs/async-graphql/latest/async_graphql/dynamic/index.html) and is a perfect match with SeaORM.
88

SeaORM/docs/10-graph-ql/02-getting-started.md

Lines changed: 154 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,178 @@ This example can be found on [SeaORM + Seaography Example](https://github.com/Se
66

77
To get started, all you need is a live SQL database with schema. You can code everything in Rust by writing SeaORM migrations, or design the schema with a GUI tool (e.g. [DataGrip](https://www.jetbrains.com/datagrip/)).
88

9-
## Install Seaography
9+
## Install Seaography CLI
1010

11-
```bash
11+
```sh
1212
cargo install seaography-cli@^1.1.0
1313
```
1414

1515
## Generate Seaography Entities
1616

17-
```bash
17+
```sh
1818
sea-orm-cli generate entity --output-dir graphql/src/entities --seaography
1919
```
2020

21-
Generate entities with `sea-orm-cli`, but with an additional `--seaography` flag. The entities are basically good-old SeaORM entities, but with additional `RelatedEntity` enum.
21+
Generate entities with `sea-orm-cli` like you normally do, but with an additional `--seaography` flag. The entities are basically good-old SeaORM entities, but with an additional `RelatedEntity` enum.
22+
23+
```rust title="examples/seaography_example/graphql/src/entities/cake.rs"
24+
use sea_orm::entity::prelude::*;
25+
26+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
27+
#[sea_orm(table_name = "cake")]
28+
pub struct Model {
29+
#[sea_orm(primary_key)]
30+
pub id: i32,
31+
pub name: String,
32+
#[sea_orm(column_type = "Decimal(Some((16, 4)))")]
33+
pub price: Decimal,
34+
pub bakery_id: i32,
35+
pub gluten_free: i8,
36+
}
37+
38+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
39+
pub enum Relation {
40+
#[sea_orm(
41+
belongs_to = "super::bakery::Entity",
42+
from = "Column::BakeryId",
43+
to = "super::bakery::Column::Id",
44+
on_update = "Cascade",
45+
on_delete = "Cascade"
46+
)]
47+
Bakery,
48+
#[sea_orm(has_many = "super::cake_baker::Entity")]
49+
CakeBaker,
50+
}
51+
52+
impl Related<super::bakery::Entity> for Entity {
53+
fn to() -> RelationDef {
54+
Relation::Bakery.def()
55+
}
56+
}
57+
58+
impl Related<super::cake_baker::Entity> for Entity {
59+
fn to() -> RelationDef {
60+
Relation::CakeBaker.def()
61+
}
62+
}
63+
64+
impl Related<super::baker::Entity> for Entity {
65+
fn to() -> RelationDef {
66+
super::cake_baker::Relation::Baker.def()
67+
}
68+
fn via() -> Option<RelationDef> {
69+
Some(super::cake_baker::Relation::Cake.def().rev())
70+
}
71+
}
72+
73+
impl ActiveModelBehavior for ActiveModel {}
74+
75+
// Additional schema meta exposed to Seaography
76+
+ #[derive(Copy, Clone, Debug, EnumIter, DeriveRelatedEntity)]
77+
+ pub enum RelatedEntity {
78+
+ #[sea_orm(entity = "super::bakery::Entity")]
79+
+ Bakery,
80+
+ #[sea_orm(entity = "super::cake_baker::Entity")]
81+
+ CakeBaker,
82+
+ #[sea_orm(entity = "super::baker::Entity")]
83+
+ Baker,
84+
+ }
85+
```
2286

2387
## Generate GraphQL Project
2488

25-
```bash
26-
# seaography-cli <DESTINATION> <ENTITIES> <DATABASE_URL> <CRATE_NAME>
89+
Generating a fresh project is the easiest way to launch a GraphQL server.
90+
However, Seaography can easily be integrated to an existing web server built with any web framework.
91+
92+
Seaography supports Actix, Poem and Axum out of the box.
93+
94+
Run the following command:
95+
96+
```sh
2797
seaography-cli graphql graphql/src/entities $DATABASE_URL sea-orm-seaography-example
2898
```
2999

100+
Full help:
101+
102+
```sh
103+
🧭 A dynamic GraphQL framework for SeaORM
104+
105+
Usage: seaography-cli [OPTIONS] <DESTINATION> <ENTITIES> <DATABASE_URL> <CRATE_NAME>
106+
107+
Arguments:
108+
<DESTINATION> Project destination folder
109+
<ENTITIES> SeaORM entities folder
110+
<DATABASE_URL> Database URL to write in .env
111+
<CRATE_NAME> Crate name for generated project
112+
113+
Options:
114+
-f, --framework <FRAMEWORK>
115+
Which web framework to use [default: poem] [possible values: actix, poem, axum]
116+
--depth-limit <DEPTH_LIMIT>
117+
GraphQL depth limit
118+
--complexity-limit <COMPLEXITY_LIMIT>
119+
GraphQL complexity limit
120+
-h, --help
121+
Print help
122+
-V, --version
123+
Print version
124+
```
125+
30126
## Start the server
31127

32-
```bash
128+
```sh
33129
cd graphql
34130
cargo run
35131
```
36132

37-
You are of course free to modify the project to suit your needs. But the interesting bit starts at the `seaography::register_entity!` macro and the [seaography::Builder](https://docs.rs/seaography/1.1.0/seaography/builder/struct.Builder.html).
133+
You are of course free to modify the project to suit your needs.
134+
The interesting bit starts at the `seaography::register_entities!` macro in `query_root.rs`.
135+
You can add custom entities, queries and mutations to the GraphQL schema.
136+
137+
## Run some queries
138+
139+
```sh
140+
Visit GraphQL Playground at http://localhost:8000
141+
```
142+
143+
Navigate to the GraphQL Playground, and then start running some queries!
144+
145+
### Bakery -> Cake -> Baker
146+
147+
```graphql
148+
{
149+
bakery(pagination: { page: { limit: 10, page: 0 } }, orderBy: { name: ASC }) {
150+
nodes {
151+
name
152+
cake {
153+
nodes {
154+
name
155+
price
156+
baker {
157+
nodes {
158+
name
159+
}
160+
}
161+
}
162+
}
163+
}
164+
}
165+
}
166+
```
167+
168+
### List gluten-free cakes and know where to buy them
169+
170+
```graphql
171+
{
172+
cake(filters: { glutenFree: { eq: 1 } }) {
173+
nodes {
174+
name
175+
price
176+
glutenFree
177+
bakery {
178+
name
179+
}
180+
}
181+
}
182+
}
183+
```

0 commit comments

Comments
 (0)