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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use crate::extensions::{Extension, ResolveInfo};
use crate::{Error, Variables};
use async_graphql_parser::query::{Definition, Document, OperationDefinition, Selection};
use uuid::Uuid;
pub struct Logger {
id: Uuid,
enabled: bool,
query: String,
variables: Variables,
}
impl Default for Logger {
fn default() -> Self {
Self {
id: Uuid::new_v4(),
enabled: true,
query: String::new(),
variables: Default::default(),
}
}
}
impl Extension for Logger {
fn parse_start(&mut self, query_source: &str, variables: &Variables) {
self.query = query_source.replace(char::is_whitespace, "");
self.variables = variables.clone();
}
fn parse_end(&mut self, document: &Document) {
let mut is_schema = false;
for definition in document.definitions() {
if let Definition::Operation(operation) = &definition.node {
let selection_set = match &operation.node {
OperationDefinition::Query(query) => &query.selection_set,
OperationDefinition::SelectionSet(selection_set) => &selection_set.node,
_ => continue,
};
is_schema = selection_set.items.iter().any(|selection| {
if let Selection::Field(field) = &selection.node {
if field.name.as_str() == "__schema" {
return true;
}
}
false
});
if is_schema {
break;
}
}
}
if is_schema {
self.enabled = false;
return;
}
info!(
target = "async-graphql",
"Query",
id = self.id,
source = self.query,
variables = self.variables
);
}
fn resolve_start(&mut self, info: &ResolveInfo<'_>) {
if !self.enabled {
return;
}
trace!(
target = "async-graphql",
"Resolve start",
id = self.id,
path = info.path_node
);
}
fn resolve_end(&mut self, info: &ResolveInfo<'_>) {
if !self.enabled {
return;
}
trace!(
target = "async-graphql",
"Resolve end",
id = self.id,
path = info.path_node
);
}
fn error(&mut self, err: &Error) {
match err {
Error::Parse(err) => {
error!(
target = "async-graphql",
"Parse error",
id = self.id,
pos = err.pos,
query = self.query,
variables = self.variables,
message = err.message
);
}
Error::Query { pos, path, err } => {
error!(
target = "async-graphql",
"Query error",
id = self.id,
pos = pos,
path = path,
query = self.query,
variables = self.variables,
error = err,
);
}
Error::Rule { errors } => {
for err in errors {
error!(
target = "async-graphql",
"Validation error",
id = self.id,
pos = err.locations,
query = self.query,
variables = self.variables,
error = err.message,
);
}
}
}
}
}