1
1
#![ deny( warnings) ]
2
2
3
- use std:: env;
3
+ use std:: { collections :: HashMap , env} ;
4
4
5
5
use actix_cors:: Cors ;
6
6
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
+ }
14
76
15
77
type Schema = RootNode < ' static , Query , EmptyMutation < Database > , EmptySubscription < Database > > ;
16
78
@@ -22,13 +84,13 @@ fn schema() -> Schema {
22
84
)
23
85
}
24
86
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
27
89
}
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
30
92
}
31
- async fn graphql (
93
+ async fn graphql_route (
32
94
req : actix_web:: HttpRequest ,
33
95
payload : actix_web:: web:: Payload ,
34
96
schema : web:: Data < Schema > ,
@@ -57,12 +119,14 @@ async fn main() -> std::io::Result<()> {
57
119
. max_age ( 3600 ) ,
58
120
)
59
121
. 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 ) ) ,
63
125
)
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 ) ) )
66
128
} ) ;
67
129
server. bind ( "127.0.0.1:8080" ) . unwrap ( ) . run ( ) . await
68
130
}
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