Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): variables #9260

Merged
merged 11 commits into from
Oct 17, 2024
9 changes: 7 additions & 2 deletions crates/turborepo-lib/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,9 @@ pub enum Command {
/// GraphQL server with GraphiQL.
#[clap(hide = true)]
Query {
/// Pass variables to the query via a JSON file
#[clap(short = 'V', long, requires = "query")]
variables: Option<Utf8PathBuf>,
/// The query to run, either a file path or a query string
query: Option<String>,
},
Expand Down Expand Up @@ -1343,14 +1346,16 @@ pub async fn run(
})?;
Ok(exit_code)
}
Command::Query { query } => {
Command::Query { query, variables } => {
warn!("query command is experimental and may change in the future");
let query = query.clone();
let variables = variables.clone();
let event = CommandEventBuilder::new("query").with_parent(&root_telemetry);
event.track_call();

let base = CommandBase::new(cli_args, repo_root, version, color_config);

let query = query::run(base, event, query).await?;
let query = query::run(base, event, query, variables.as_deref()).await?;

Ok(query)
}
Expand Down
17 changes: 15 additions & 2 deletions crates/turborepo-lib/src/commands/query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fs, sync::Arc};

use async_graphql::{EmptyMutation, EmptySubscription, Schema, ServerError};
use async_graphql::{EmptyMutation, EmptySubscription, Request, Schema, ServerError, Variables};
use camino::Utf8Path;
use miette::{Diagnostic, Report, SourceSpan};
use thiserror::Error;
use turbopath::AbsoluteSystemPathBuf;
Expand Down Expand Up @@ -58,10 +59,20 @@ pub async fn run(
mut base: CommandBase,
telemetry: CommandEventBuilder,
query: Option<String>,
variables_path: Option<&Utf8Path>,
) -> Result<i32, Error> {
let signal = get_signal()?;
let handler = SignalHandler::new(signal);

let variables: Variables = variables_path
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: (sorry if I missed a good reason not to do this) might be nice to move this down with where it's used in the hunk below this one

.map(AbsoluteSystemPathBuf::from_cwd)
.transpose()?
.map(|path| path.read_to_string())
.transpose()?
.map(|content| serde_json::from_str(&content))
.transpose()?
.unwrap_or_default();

// We fake a run command, so we can construct a `Run` type
base.args_mut().command = Some(Command::Run {
run_args: Box::default(),
Expand Down Expand Up @@ -90,7 +101,9 @@ pub async fn run(
EmptySubscription,
);

let result = schema.execute(&query).await;
let request = Request::new(&query).variables(variables);

let result = schema.execute(request).await;
if result.errors.is_empty() {
println!("{}", serde_json::to_string_pretty(&result)?);
} else {
Expand Down
44 changes: 44 additions & 0 deletions turborepo-tests/integration/tests/query/variables.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Setup
$ . ${TESTDIR}/../../../helpers/setup_integration_test.sh

Create a variables file
$ echo '{ "name": "my-app" }' > vars.json

Query packages
$ ${TURBO} query 'query($name: String) { package(name: $name) { name } }' --variables vars.json | jq
WARNING query command is experimental and may change in the future
{
"data": {
"package": {
"name": "my-app"
}
}
}

Write query to file
$ echo 'query($name: String) { package(name: $name) { name } }' > query.gql

Run the query
$ ${TURBO} query query.gql --variables vars.json | jq
WARNING query command is experimental and may change in the future
{
"data": {
"package": {
"name": "my-app"
}
}
}

Make sure we can't pass variables without a query
$ ${TURBO} query --variables vars.json
ERROR the following required arguments were not provided:
<QUERY>

Usage: turbo(.exe)? query --variables <VARIABLES> <QUERY> (re)

For more information, try '--help'.

[1]



Loading