Skip to content

Commit 250e261

Browse files
Make the actix example self contained (#839)
A smaller example is esier to understand. Fixes #804 Co-authored-by: Christian Legnitto <[email protected]>
1 parent 4682fe2 commit 250e261

File tree

1 file changed

+82
-18
lines changed

1 file changed

+82
-18
lines changed

juniper_actix/examples/actix_server.rs

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,78 @@
11
#![deny(warnings)]
22

3-
use std::env;
3+
use std::{collections::HashMap, env};
44

55
use actix_cors::Cors;
66
use actix_web::{http::header, middleware, web, App, Error, HttpResponse, HttpServer};
7-
use juniper::{
8-
tests::fixtures::starwars::schema::{Database, Query},
9-
EmptyMutation, EmptySubscription, RootNode,
10-
};
11-
use juniper_actix::{
12-
graphiql_handler as gqli_handler, graphql_handler, playground_handler as play_handler,
13-
};
7+
use juniper::{graphql_object, EmptyMutation, EmptySubscription, GraphQLObject, RootNode};
8+
use juniper_actix::{graphiql_handler, graphql_handler, playground_handler};
9+
#[derive(Clone, GraphQLObject)]
10+
///a user
11+
pub struct User {
12+
///the id
13+
id: i32,
14+
///the name
15+
name: String,
16+
}
17+
18+
#[derive(Default, Clone)]
19+
pub struct Database {
20+
///this could be a database connection
21+
users: HashMap<i32, User>,
22+
}
23+
impl Database {
24+
pub fn new() -> Database {
25+
let mut users = HashMap::new();
26+
users.insert(
27+
1,
28+
User {
29+
id: 1,
30+
name: "Aron".to_string(),
31+
},
32+
);
33+
users.insert(
34+
2,
35+
User {
36+
id: 2,
37+
name: "Bea".to_string(),
38+
},
39+
);
40+
users.insert(
41+
3,
42+
User {
43+
id: 3,
44+
name: "Carl".to_string(),
45+
},
46+
);
47+
users.insert(
48+
4,
49+
User {
50+
id: 4,
51+
name: "Dora".to_string(),
52+
},
53+
);
54+
Database { users }
55+
}
56+
pub fn get_user(&self, id: &i32) -> Option<&User> {
57+
self.users.get(id)
58+
}
59+
}
60+
61+
// To make our Database usable by Juniper, we have to implement a marker trait.
62+
impl juniper::Context for Database {}
63+
64+
// Queries represent the callable funcitons
65+
struct Query;
66+
#[graphql_object(context = Database)]
67+
impl Query {
68+
fn apiVersion() -> String {
69+
"1.0".to_string()
70+
}
71+
#[graphql(arguments(id(description = "id of the user")))]
72+
fn user(database: &Database, id: i32) -> Option<&User> {
73+
database.get_user(&id)
74+
}
75+
}
1476

1577
type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;
1678

@@ -22,13 +84,13 @@ fn schema() -> Schema {
2284
)
2385
}
2486

25-
async fn graphiql_handler() -> Result<HttpResponse, Error> {
26-
gqli_handler("/", None).await
87+
async fn graphiql_route() -> Result<HttpResponse, Error> {
88+
graphiql_handler("/graphgl", None).await
2789
}
28-
async fn playground_handler() -> Result<HttpResponse, Error> {
29-
play_handler("/", None).await
90+
async fn playground_route() -> Result<HttpResponse, Error> {
91+
playground_handler("/graphgl", None).await
3092
}
31-
async fn graphql(
93+
async fn graphql_route(
3294
req: actix_web::HttpRequest,
3395
payload: actix_web::web::Payload,
3496
schema: web::Data<Schema>,
@@ -57,12 +119,14 @@ async fn main() -> std::io::Result<()> {
57119
.max_age(3600),
58120
)
59121
.service(
60-
web::resource("/")
61-
.route(web::post().to(graphql))
62-
.route(web::get().to(graphql)),
122+
web::resource("/graphgl")
123+
.route(web::post().to(graphql_route))
124+
.route(web::get().to(graphql_route)),
63125
)
64-
.service(web::resource("/playground").route(web::get().to(playground_handler)))
65-
.service(web::resource("/graphiql").route(web::get().to(graphiql_handler)))
126+
.service(web::resource("/playground").route(web::get().to(playground_route)))
127+
.service(web::resource("/graphiql").route(web::get().to(graphiql_route)))
66128
});
67129
server.bind("127.0.0.1:8080").unwrap().run().await
68130
}
131+
// now go to http://127.0.0.1:8080/playground or graphiql and execute
132+
//{ apiVersion, user(id: 2){id, name}}

0 commit comments

Comments
 (0)