Skip to content

Commit

Permalink
feat(query): add array type (#9161)
Browse files Browse the repository at this point in the history
### Description

Added an `Array` type that automatically adds a `length` and `items`
field to GraphQL responses.

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
NicholasLYang committed Sep 17, 2024
1 parent ed8ff72 commit c482ce3
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 148 deletions.
32 changes: 24 additions & 8 deletions crates/turborepo-lib/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ impl Query {
}
}

#[derive(Debug, SimpleObject)]
struct Array<T: OutputType> {
items: Vec<T>,
length: usize,
}

impl<T: OutputType> FromIterator<T> for Array<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let items: Vec<_> = iter.into_iter().collect();
let length = items.len();
Self { items, length }
}
}

struct Package {
run: Arc<Run>,
name: PackageName,
Expand Down Expand Up @@ -293,7 +307,7 @@ impl Query {
&self,
base: Option<String>,
head: Option<String>,
) -> Result<Vec<Package>, Error> {
) -> Result<Array<Package>, Error> {
let mut opts = self.run.opts().clone();
opts.scope_opts.affected_range = Some((base, head));

Expand Down Expand Up @@ -322,7 +336,7 @@ impl Query {
}

/// Gets a list of packages that match the given filter
async fn packages(&self, filter: Option<PackagePredicate>) -> Result<Vec<Package>, Error> {
async fn packages(&self, filter: Option<PackagePredicate>) -> Result<Array<Package>, Error> {
let Some(filter) = filter else {
return Ok(self
.run
Expand Down Expand Up @@ -369,7 +383,7 @@ impl Package {
}

/// The upstream packages that have this package as a direct dependency
async fn direct_dependents(&self) -> Result<Vec<Package>, Error> {
async fn direct_dependents(&self) -> Result<Array<Package>, Error> {
let node: PackageNode = PackageNode::Workspace(self.name.clone());
Ok(self
.run
Expand All @@ -386,7 +400,7 @@ impl Package {
}

/// The downstream packages that directly depend on this package
async fn direct_dependencies(&self) -> Result<Vec<Package>, Error> {
async fn direct_dependencies(&self) -> Result<Array<Package>, Error> {
let node: PackageNode = PackageNode::Workspace(self.name.clone());
Ok(self
.run
Expand All @@ -402,7 +416,8 @@ impl Package {
.collect())
}

async fn all_dependents(&self) -> Result<Vec<Package>, Error> {
/// The downstream packages that depend on this package, transitively
async fn all_dependents(&self) -> Result<Array<Package>, Error> {
let node: PackageNode = PackageNode::Workspace(self.name.clone());
Ok(self
.run
Expand All @@ -417,7 +432,8 @@ impl Package {
.collect())
}

async fn all_dependencies(&self) -> Result<Vec<Package>, Error> {
/// The upstream packages that this package depends on, transitively
async fn all_dependencies(&self) -> Result<Array<Package>, Error> {
let node: PackageNode = PackageNode::Workspace(self.name.clone());
Ok(self
.run
Expand All @@ -433,7 +449,7 @@ impl Package {
}

/// The downstream packages that depend on this package, indirectly
async fn indirect_dependents(&self) -> Result<Vec<Package>, Error> {
async fn indirect_dependents(&self) -> Result<Array<Package>, Error> {
let node: PackageNode = PackageNode::Workspace(self.name.clone());
let immediate_dependents = self
.run
Expand All @@ -456,7 +472,7 @@ impl Package {
}

/// The upstream packages that this package depends on, indirectly
async fn indirect_dependencies(&self) -> Result<Vec<Package>, Error> {
async fn indirect_dependencies(&self) -> Result<Array<Package>, Error> {
let node: PackageNode = PackageNode::Workspace(self.name.clone());
let immediate_dependencies = self
.run
Expand Down
2 changes: 1 addition & 1 deletion turborepo-tests/integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "turborepo-tests-integration",
"scripts": {
"test": "prysk tests",
"test:interactive": "prysk --interactive tests",
"test:interactive": "PRYSK_INTERACTIVE=true prysk tests",
"test:parallel": ".cram_env/bin/pytest -n auto tests --prysk-shell=`which bash`",
"pretest:parallel": ".cram_env/bin/pip3 install --quiet pytest \"prysk[pytest-plugin]\" pytest-xdist"
},
Expand Down
132 changes: 74 additions & 58 deletions turborepo-tests/integration/tests/affected.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ Do the same thing with the `ls` command


Do the same thing with the `query` command
$ ${TURBO} query "query { affectedPackages { name } }"
$ ${TURBO} query "query { affectedPackages { items { name } } }"
WARNING query command is experimental and may change in the future
{
"data": {
"affectedPackages": [
{
"name": "my-app"
}
]
"affectedPackages": {
"items": [
{
"name": "my-app"
}
]
}
}
}

Expand Down Expand Up @@ -88,15 +90,17 @@ Do the same thing with the `ls` command


Do the same thing with the `query` command
$ ${TURBO} query "query { affectedPackages { name } }"
$ ${TURBO} query "query { affectedPackages { items { name } } }"
WARNING query command is experimental and may change in the future
{
"data": {
"affectedPackages": [
{
"name": "my-app"
}
]
"affectedPackages": {
"items": [
{
"name": "my-app"
}
]
}
}
}

Expand Down Expand Up @@ -130,15 +134,17 @@ Do the same thing with the `ls` command


Do the same thing with the `query` command
$ ${TURBO} query "query { affectedPackages { name } }"
$ ${TURBO} query "query { affectedPackages { items { name } } }"
WARNING query command is experimental and may change in the future
{
"data": {
"affectedPackages": [
{
"name": "my-app"
}
]
"affectedPackages": {
"items": [
{
"name": "my-app"
}
]
}
}
}

Expand All @@ -163,11 +169,13 @@ Do the same thing with the `ls` command


Do the same thing with the `query` command
$ ${TURBO} query "query { affectedPackages(base: \"HEAD\") { name } }"
$ ${TURBO} query "query { affectedPackages(base: \"HEAD\") { items { name } } }"
WARNING query command is experimental and may change in the future
{
"data": {
"affectedPackages": []
"affectedPackages": {
"items": []
}
}
}
Expand All @@ -192,11 +200,13 @@ Do the same thing with the `ls` command
Do the same thing with the `query` command
$ ${TURBO} query "query { affectedPackages(head: \"main\") { name } }"
$ ${TURBO} query "query { affectedPackages(head: \"main\") { items { name } } }"
WARNING query command is experimental and may change in the future
{
"data": {
"affectedPackages": []
"affectedPackages": {
"items": []
}
}
}
Expand Down Expand Up @@ -235,15 +245,17 @@ Do the same thing with the `ls` command
Do the same thing with the `query` command
$ ${TURBO} query "query { affectedPackages { name } }"
$ ${TURBO} query "query { affectedPackages { items { name } } }"
WARNING query command is experimental and may change in the future
{
"data": {
"affectedPackages": [
{
"name": "my-app"
}
]
"affectedPackages": {
"items": [
{
"name": "my-app"
}
]
}
}
}
Expand Down Expand Up @@ -277,26 +289,28 @@ Do the same thing with the `ls` command
Do the same thing with the `query` command
$ ${TURBO} query "query { affectedPackages { name } }"
$ ${TURBO} query "query { affectedPackages { items { name } } }"
WARNING query command is experimental and may change in the future
WARNING unable to detect git range, assuming all files have changed: git error: fatal: no merge base found
{
"data": {
"affectedPackages": [
{
"name": "//"
},
{
"name": "another"
},
{
"name": "my-app"
},
{
"name": "util"
}
]
"affectedPackages": {
"items": [
{
"name": "//"
},
{
"name": "another"
},
{
"name": "my-app"
},
{
"name": "util"
}
]
}
}
}
Expand Down Expand Up @@ -330,25 +344,27 @@ Do the same thing with the `ls` command
Do the same thing with the `query` command
$ ${TURBO} query "query { affectedPackages { name } }"
$ ${TURBO} query "query { affectedPackages { items { name } } }"
WARNING query command is experimental and may change in the future
WARNING unable to detect git range, assuming all files have changed: git error: fatal: no merge base found
{
"data": {
"affectedPackages": [
{
"name": "//"
},
{
"name": "another"
},
{
"name": "my-app"
},
{
"name": "util"
}
]
"affectedPackages": {
"items": [
{
"name": "//"
},
{
"name": "another"
},
{
"name": "my-app"
},
{
"name": "util"
}
]
}
}
}
Loading

0 comments on commit c482ce3

Please sign in to comment.