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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::context::with_puff_context;
use crate::errors::{to_py_error, PuffResult};
use crate::graphql::scalar::AggroScalarValue;
use crate::graphql::{convert_pyany_to_input, juniper_value_to_python, PuffGraphqlConfig};
use crate::prelude::ToText;
use crate::python::async_python::run_python_async;
use crate::python::postgres::Connection;
use crate::types::Text;
use anyhow::bail;
use juniper::executor::{get_operation, resolve_validated_subscription};
use juniper::parser::parse_document_source;
use juniper::validation::{validate_input_values, visit_all_rules, ValidatorContext};
use juniper::{execute, ExecutionError, GraphQLError, Value};
use pyo3::prelude::*;
use pyo3::types::{PyDict, PyList};
use pyo3::{PyObject, PyResult, Python};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::mpsc::{channel, Receiver};
use tokio::sync::Mutex;
use tokio_stream::{StreamExt, StreamMap};
#[pyclass]
#[derive(Clone)]
pub struct GlobalGraphQL;
impl ToPyObject for GlobalGraphQL {
fn to_object(&self, py: Python<'_>) -> PyObject {
self.clone().into_py(py)
}
}
#[pymethods]
impl GlobalGraphQL {
fn __call__(&self, py: Python) -> PyObject {
with_puff_context(|ctx| PythonGraphql(ctx.gql())).to_object(py)
}
fn by_name(&self, py: Python, name: &str) -> PyObject {
with_puff_context(|ctx| PythonGraphql(ctx.gql_named(name))).to_object(py)
}
}
#[pyclass]
#[derive(Clone)]
pub struct StreamReceiver(Arc<Mutex<Receiver<PuffResult<(Option<Text>, PyObject)>>>>);
impl ToPyObject for StreamReceiver {
fn to_object(&self, py: Python<'_>) -> PyObject {
self.clone().into_py(py)
}
}
#[pymethods]
impl StreamReceiver {
fn __call__(&self, ret_func: PyObject) {
let rec = self.0.clone();
run_python_async(ret_func, async move {
if let Some(r) = rec.lock().await.recv().await {
r.map(|f| Python::with_gil(|py| f.into_py(py)))
} else {
Python::with_gil(|py| Ok(py.None()))
}
})
}
}
#[pyclass]
#[derive(Clone)]
pub struct PythonGraphql(PuffGraphqlConfig);
impl ToPyObject for PythonGraphql {
fn to_object(&self, py: Python<'_>) -> PyObject {
self.clone().into_py(py)
}
}
#[pymethods]
impl PythonGraphql {
pub fn query(
&self,
return_fun: PyObject,
query: String,
variables: &PyDict,
conn: Option<&Connection>,
auth: Option<PyObject>,
) -> PyResult<()> {
let mut hm = HashMap::with_capacity(variables.len());
for (k, v) in variables {
let variables = to_py_error("GQL Inputs", convert_pyany_to_input(v))?;
hm.insert(k.to_string(), variables);
}
let this_root = self.0.root();
let context = if let Some(c) = conn {
self.0.new_context_with_connection(auth, Some(c.clone()))
} else {
self.0.new_context(auth)
};
run_python_async(return_fun, async move {
let (value, errors) = execute(query.as_str(), None, &this_root, &hm, &context).await?;
convert_execution_response(&value, errors)
});
Ok(())
}
pub fn subscribe(
&self,
return_fun: PyObject,
query: String,
variables: &PyDict,
conn: Option<&Connection>,
auth: Option<PyObject>,
) -> PyResult<()> {
let mut hm = HashMap::with_capacity(variables.len());
for (k, v) in variables {
let variables = to_py_error("GQL Inputs", convert_pyany_to_input(v))?;
hm.insert(k.to_string(), variables);
}
let this_root = self.0.root();
let context = if let Some(c) = conn {
self.0.new_context_with_connection(auth, Some(c.clone()))
} else {
self.0.new_context(auth)
};
run_python_async(return_fun, async move {
let (sender, rec) = channel(1);
let this_sender = sender.clone();
let fut = async move {
let document = parse_document_source(query.as_str(), &this_root.schema)?;
{
let mut ctx = ValidatorContext::new(&this_root.schema, &document);
visit_all_rules(&mut ctx, &document);
let errors = ctx.into_errors();
if !errors.is_empty() {
Err(GraphQLError::ValidationError(errors))?;
}
}
let operation = get_operation(&document, None)?;
{
let errors = validate_input_values(&hm, operation, &this_root.schema);
if !errors.is_empty() {
Err(GraphQLError::ValidationError(errors))?;
}
}
let (value, errors) =
resolve_validated_subscription(&document, operation, &this_root, &hm, &context)
.await?;
if !errors.is_empty() {
let py_val = convert_execution_response(&Value::null(), errors)?;
if let Err(_) = sender.send(Ok((None, py_val))).await {
return Ok(());
}
}
let response_returned_object = match value {
Value::Object(o) => o,
_ => bail!("Expected object from subscription."),
};
let fields = response_returned_object.into_iter();
let mut stream_map = StreamMap::new();
for (name, stream_val) in fields {
match stream_val {
Value::Scalar(stream) => {
stream_map.insert(name.to_text(), stream);
}
_ => unreachable!(),
}
}
while let Some((name, nv)) = stream_map.next().await {
let py_val = match nv {
Ok(v) => convert_execution_response(&v, vec![])?,
Err(e) => convert_execution_response(&Value::null(), vec![e])?,
};
if let Err(_) = sender.send(Ok((Some(name), py_val))).await {
break;
}
}
Ok(())
};
let handle = with_puff_context(|ctx| ctx.handle());
handle.spawn(async move {
if let Err(e) = fut.await {
this_sender.send(Err(e)).await.unwrap_or_default();
}
});
PuffResult::Ok(StreamReceiver(Arc::new(Mutex::new(rec))))
});
Ok(())
}
}
fn convert_execution_response(
value: &Value<AggroScalarValue>,
errors: Vec<ExecutionError<AggroScalarValue>>,
) -> PuffResult<PyObject> {
Python::with_gil(|py| {
let pydict = PyDict::new(py);
let data = juniper_value_to_python(py, &value)?;
if !errors.is_empty() {
let py_errors = PyList::empty(py);
for error in errors {
let pydict = PyDict::new(py);
pydict.set_item("path", error.path())?;
pydict.set_item("error", format!("{:?}", error.error()))?;
pydict.set_item("location", format!("{:?}", error.location()))?;
py_errors.append(pydict)?;
}
pydict.set_item("errors", py_errors)?;
}
pydict.set_item("data", data)?;
let r: PyObject = pydict.into_py(py);
Ok(r)
})
}