-
Notifications
You must be signed in to change notification settings - Fork 432
/
Copy pathmain.rs
59 lines (50 loc) · 1.37 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use juniper::http::{GraphQLRequest, GraphQLResponse};
use juniper::Document;
use juniper::{
graphql_object, DefaultScalarValue, EmptyMutation, EmptySubscription, ParseError, RootNode,
Spanning,
};
#[derive(Clone, Copy, Debug)]
struct Context;
impl juniper::Context for Context {}
#[derive(Clone, Debug)]
struct User {
name: String,
}
#[graphql_object(Context = Context)]
impl User {
fn name(&self) -> &str {
&self.name
}
}
#[derive(Clone, Copy, Debug)]
struct Query;
#[graphql_object(Context = Context)]
impl Query {
fn users() -> Vec<User> {
vec![User {
name: "user1".into(),
}]
}
}
type Schema = RootNode<'static, Query, EmptyMutation<Context>, EmptySubscription<Context>>;
fn main() {
let schema = Schema::new(
Query,
EmptyMutation::<Context>::new(),
EmptySubscription::<Context>::new(),
);
let ctx = Context {};
let query = r#" query { users { name } } "#;
let req = GraphQLRequest::<DefaultScalarValue>::new(query.to_owned(), None, None);
{
// Just parse the request query
let doc: Result<Document<DefaultScalarValue>, Spanning<ParseError>> = req.parse(&schema);
println!("{:#?}", &doc);
}
{
// Execute the query synchronously
let res: GraphQLResponse = req.execute_sync(&schema, &ctx);
println!("{:#?}", &res);
}
}