Skip to content

Commit 0e32fb4

Browse files
committed
Add missing files
1 parent e214acc commit 0e32fb4

File tree

7 files changed

+569
-0
lines changed

7 files changed

+569
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
---
2+
source: wundergraph_cli/src/print_schema/mod.rs
3+
expression: "&s"
4+
---
5+
use wundergraph::query_builder::types::{HasMany, HasOne};
6+
use wundergraph::scalar::WundergraphScalarValue;
7+
use wundergraph::WundergraphEntity;
8+
9+
table! {
10+
infer_test.comments (id) {
11+
id -> Int4,
12+
post -> Nullable<Int4>,
13+
commenter -> Nullable<Int4>,
14+
content -> Text,
15+
}
16+
}
17+
18+
table! {
19+
infer_test.posts (id) {
20+
id -> Int4,
21+
author -> Nullable<Int4>,
22+
title -> Text,
23+
content -> Nullable<Text>,
24+
}
25+
}
26+
27+
table! {
28+
infer_test.users (id) {
29+
id -> Int4,
30+
name -> Text,
31+
}
32+
}
33+
34+
allow_tables_to_appear_in_same_query!(
35+
comments,
36+
posts,
37+
users,
38+
);
39+
40+
41+
#[derive(Clone, Debug, Identifiable, WundergraphEntity)]
42+
#[table_name = "comments"]
43+
#[primary_key(id)]
44+
pub struct Comment {
45+
id: i32,
46+
post: Option<HasOne<i32, Post>>,
47+
commenter: Option<HasOne<i32, User>>,
48+
content: String,
49+
}
50+
51+
#[derive(Clone, Debug, Identifiable, WundergraphEntity)]
52+
#[table_name = "posts"]
53+
#[primary_key(id)]
54+
pub struct Post {
55+
id: i32,
56+
author: Option<HasOne<i32, User>>,
57+
title: String,
58+
content: Option<String>,
59+
comments: HasMany<Comment, comments::post>,
60+
}
61+
62+
#[derive(Clone, Debug, Identifiable, WundergraphEntity)]
63+
#[table_name = "users"]
64+
#[primary_key(id)]
65+
pub struct User {
66+
id: i32,
67+
name: String,
68+
comments: HasMany<Comment, comments::commenter>,
69+
posts: HasMany<Post, posts::author>,
70+
}
71+
72+
73+
74+
wundergraph::query_object!{
75+
Query {
76+
Comment,
77+
Post,
78+
User,
79+
}
80+
}
81+
82+
83+
#[derive(Insertable, juniper::GraphQLInputObject, Clone, Debug)]
84+
#[graphql(scalar = "WundergraphScalarValue")]
85+
#[table_name = "comments"]
86+
pub struct NewComment {
87+
post: Option<i32>,
88+
commenter: Option<i32>,
89+
content: String,
90+
}
91+
92+
#[derive(AsChangeset, Identifiable, juniper::GraphQLInputObject, Clone, Debug)]
93+
#[graphql(scalar = "WundergraphScalarValue")]
94+
#[table_name = "comments"]
95+
#[primary_key(id)]
96+
pub struct CommentChangeset {
97+
id: i32,
98+
post: Option<i32>,
99+
commenter: Option<i32>,
100+
content: String,
101+
}
102+
103+
#[derive(Insertable, juniper::GraphQLInputObject, Clone, Debug)]
104+
#[graphql(scalar = "WundergraphScalarValue")]
105+
#[table_name = "posts"]
106+
pub struct NewPost {
107+
author: Option<i32>,
108+
title: String,
109+
content: Option<String>,
110+
}
111+
112+
#[derive(AsChangeset, Identifiable, juniper::GraphQLInputObject, Clone, Debug)]
113+
#[graphql(scalar = "WundergraphScalarValue")]
114+
#[table_name = "posts"]
115+
#[primary_key(id)]
116+
pub struct PostChangeset {
117+
id: i32,
118+
author: Option<i32>,
119+
title: String,
120+
content: Option<String>,
121+
}
122+
123+
#[derive(Insertable, juniper::GraphQLInputObject, Clone, Debug)]
124+
#[graphql(scalar = "WundergraphScalarValue")]
125+
#[table_name = "users"]
126+
pub struct NewUser {
127+
name: String,
128+
}
129+
130+
#[derive(AsChangeset, Identifiable, juniper::GraphQLInputObject, Clone, Debug)]
131+
#[graphql(scalar = "WundergraphScalarValue")]
132+
#[table_name = "users"]
133+
#[primary_key(id)]
134+
pub struct UserChangeset {
135+
id: i32,
136+
name: String,
137+
}
138+
139+
wundergraph::mutation_object!{
140+
Mutation{
141+
Comment(insert = NewComment, update = CommentChangeset, ),
142+
Post(insert = NewPost, update = PostChangeset, ),
143+
User(insert = NewUser, update = UserChangeset, ),
144+
}
145+
}
146+
147+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: wundergraph_cli/src/print_schema/mod.rs
3+
expression: "r.json::<serde_json::Value>().unwrap()"
4+
---
5+
{
6+
"data": {
7+
"CreateUser": {
8+
"id": 1,
9+
"name": "Max"
10+
}
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
source: wundergraph_cli/src/print_schema/mod.rs
3+
expression: "r.json::<serde_json::Value>().unwrap()"
4+
---
5+
{
6+
"data": {
7+
"Users": [
8+
{
9+
"id": 1,
10+
"name": "Max"
11+
}
12+
]
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: wundergraph_cli/src/print_schema/mod.rs
3+
expression: "r.json::<serde_json::Value>().unwrap()"
4+
---
5+
{
6+
"data": {
7+
"Users": []
8+
}
9+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#[macro_use] extern crate diesel;
2+
3+
use actix_web::web::Data;
4+
use actix_web::web::Json;
5+
use actix_web::middleware;
6+
use actix_web::web;
7+
use actix_web::App;
8+
use actix_web::HttpResponse;
9+
use actix_web::HttpServer;
10+
use diesel::{conn};
11+
use diesel::r2d2::ConnectionManager;
12+
use diesel::r2d2::Pool;
13+
use diesel::r2d2::PooledConnection;
14+
use diesel::r2d2::CustomizeConnection;
15+
use diesel::Connection;
16+
use diesel::connection::SimpleConnection;
17+
use juniper::graphiql::graphiql_source;
18+
use juniper::http::GraphQLRequest;
19+
use serde::Deserialize;
20+
use serde::Serialize;
21+
use std::sync::Arc;
22+
use wundergraph::scalar::WundergraphScalarValue;
23+
24+
pub mod api;
25+
26+
pub type Schema<Connection> = juniper::RootNode<
27+
'static,
28+
self::api::Query<PooledConnection<ConnectionManager<Connection>>>,
29+
self::api::Mutation<PooledConnection<ConnectionManager<Connection>>>,
30+
WundergraphScalarValue,
31+
>;
32+
33+
// actix integration stuff
34+
#[derive(Serialize, Deserialize, Debug)]
35+
pub struct GraphQLData(GraphQLRequest<WundergraphScalarValue>);
36+
37+
#[derive(Clone)]
38+
struct AppState {{
39+
schema: Arc<Schema<{conn}>>,
40+
pool: Arc<Pool<ConnectionManager<{conn}>>>,
41+
}}
42+
43+
fn graphiql() -> HttpResponse {{
44+
let html = graphiql_source("/graphql");
45+
HttpResponse::Ok()
46+
.content_type("text/html; charset=utf-8")
47+
.body(html)
48+
}}
49+
50+
fn graphql(
51+
Json(GraphQLData(data)): Json<GraphQLData>,
52+
st: Data<AppState>,
53+
) -> Result<HttpResponse, failure::Error> {{
54+
let ctx = st.get_ref().pool.get()?;
55+
let res = data.execute(&st.get_ref().schema, &ctx);
56+
Ok(HttpResponse::Ok()
57+
.content_type("application/json")
58+
.body(serde_json::to_string(&res)?))
59+
}}
60+
61+
#[derive(Debug)]
62+
struct ConnectionHandler;
63+
64+
impl<E> CustomizeConnection<{conn}, E> for ConnectionHandler {{
65+
fn on_acquire(&self, conn: &mut {conn}) -> Result<(), E> {{
66+
Ok(conn.begin_test_transaction().unwrap())
67+
}}
68+
}}
69+
70+
fn main() {{
71+
let manager = ConnectionManager::<{conn}>::new("{db_url}");
72+
let pool = Pool::builder()
73+
.max_size(1)
74+
.connection_customizer(Box::new(ConnectionHandler))
75+
.build(manager)
76+
.expect("Failed to init pool");
77+
{{
78+
let conn = pool.get().unwrap();
79+
conn.batch_execute("{migrations}").unwrap();
80+
}}
81+
82+
let query = self::api::Query::default();
83+
let mutation = self::api::Mutation::default();
84+
let schema = Schema::new(query, mutation);
85+
86+
let schema = Arc::new(schema);
87+
let pool = Arc::new(pool);
88+
let data = AppState {{ schema, pool }};
89+
90+
let url = "{listen_url}";
91+
92+
println!("Started http server: http://{{}}", url);
93+
94+
HttpServer::new(move || {{
95+
App::new()
96+
.data(data.clone())
97+
.wrap(middleware::Logger::default())
98+
.route("/graphql", web::get().to(graphql))
99+
.route("/graphql", web::post().to(graphql))
100+
.route("/graphiql", web::get().to(graphiql))
101+
.default_service(web::route().to(|| {{
102+
HttpResponse::Found()
103+
.header("location", "/graphiql")
104+
.finish()
105+
}}))
106+
}})
107+
.bind(&url)
108+
.expect("Failed to start server")
109+
.run()
110+
.unwrap();
111+
}}

0 commit comments

Comments
 (0)