Skip to content

Commit

Permalink
Fix missing alternates in graphql endpoint (#1228)
Browse files Browse the repository at this point in the history
The GET endpoint for graphql was broken

Change: graphql-alternate
  • Loading branch information
LMG committed Jul 3, 2023
1 parent c0a170a commit 6413f1e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
42 changes: 35 additions & 7 deletions josh-core/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,31 +159,59 @@ pub fn render(
}

let template = if let Ok(blob) = obj.peel_to_blob() {
let template = std::str::from_utf8(blob.content())?;
let file = std::str::from_utf8(blob.content())?;
if cmd == "get" {
return Ok(Some(template.to_string()));
return Ok(Some(file.to_string()));
}
if cmd == "graphql" {
let mut variables = juniper::Variables::new();

for (k, v) in params {
variables.insert(k.to_string(), juniper::InputValue::scalar(v));
}
let transaction = cache::Transaction::open(transaction.repo().path(), None)?;
let transaction_overlay = cache::Transaction::open(transaction.repo().path(), None)?;
let (transaction, transaction_mirror) = if let Ok(to) = cache::Transaction::open(
&transaction
.repo()
.path()
.parent()
.ok_or(josh_error("parent"))?
.join("overlay"),
None,
) {
to.repo().odb()?.add_disk_alternate(
&transaction
.repo()
.path()
.parent()
.ok_or(josh_error("parent"))?
.join("mirror")
.join("objects")
.to_str()
.unwrap(),
)?;
(
to,
cache::Transaction::open(&transaction.repo().path(), None)?,
)
} else {
(
cache::Transaction::open(transaction.repo().path(), None)?,
cache::Transaction::open(transaction.repo().path(), None)?,
)
};
let (res, _errors) = juniper::execute_sync(
template,
file,
None,
&graphql::commit_schema(commit_id),
&variables,
&graphql::context(transaction, transaction_overlay),
&graphql::context(transaction, transaction_mirror),
)?;

let j = serde_json::to_string_pretty(&res)?;
return Ok(Some(j));
}
if cmd == "render" {
template.to_string()
file.to_string()
} else {
return Err(josh_error("no such cmd"));
}
Expand Down
9 changes: 9 additions & 0 deletions tests/proxy/query.t
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ Now render still works (used to fail if filtered previously)
sha: 890148bbaa6a797bac8aef672a437f2b08635f15
filter_sha: ffe8d082c1034053534ea8068f4205ac72a1098e
Graphql works
$ curl -s http://localhost:8002/real_repo.git?graphql=x.graphql
{
"hash": "890148bbaa6a797bac8aef672a437f2b08635f15",
"rev": {
"hash": "ffe8d082c1034053534ea8068f4205ac72a1098e"
}
} (no-eol)
Failing render for lack of variable
$ curl -i -s http://localhost:8002/real_repo.git?render=tmpl_file
Expand Down

0 comments on commit 6413f1e

Please sign in to comment.