You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now if we run the command again, it will complain if we don't provide a graph ref:
210
216
211
217
```console
@@ -222,13 +228,13 @@ For more information try --help
222
228
223
229
##### Setting up a command to work with `rover-client`
224
230
225
-
Most of Rover's commands make requests to Apollo Studio's API, or to another GraphQL API. Rather than handling the request logic in the repository's main package, Rover is structured so that this logic lives in `crates/rover-client`. This is helpful for separation of concerns and testing.
231
+
Most of Rover's commands make requests to Apollo Studio's API. Rather than handling the request logic in the repository's main package, Rover is structured so that this logic lives in `crates/rover-client`. This is helpful for separation of concerns and testing.
226
232
227
233
To access functionality from `rover-client` in our `rover graph hello` command, we'll need to pass down a client from the entry to our command in `src/command/graph/mod.rs`.
228
234
229
235
You can do this by changing the `Command::Hello(command) => command.run(),` line to `Command::Hello(command) => command.run(client_config),`.
230
236
231
-
Then you'll need to change `Hello::run` to accept a `client_config: StudioClientConfig` parameter in `src/command/graph/hello.rs`, and add a `use crate::utils::client::StudioClientConfig` import statement. Then, at the top of the run function, you can create a `StudioClient` by adding `let client = client_config.get_authenticated_client(&self.profile_name)?;`. You can see examples of this in the other commands.
237
+
Then you'll need to change `Hello::run` to accept a `client_config: StudioClientConfig` parameter in `src/command/graph/hello.rs`, and add a `use crate::utils::client::StudioClientConfig` import statement. Then, at the top of the run function, you can create a `StudioClient` by adding `let client = client_config.get_client(&self.profile_name)?;`. You can see examples of this in the other commands.
232
238
233
239
##### Auto-generated help command
234
240
@@ -265,19 +271,19 @@ Whenever you create a new command, make sure to add `#[serde(skip_serializing)]`
265
271
266
272
##### Adding a query to Apollo Studio
267
273
268
-
The only piece of the `rover-client` crate that we need to be concerned with for now is the `src/operations` directory. This is where all the queries to Apollo Studio live. This directory is roughly organized by the command names as well, but there might be some queries in these directories that are used by multiple commands.
274
+
The only piece of the `rover-client` crate that we need to be concerned with for now is the `src/query` directory. This is where all the queries to Apollo Studio live. This directory is roughly organized by the command names as well, but there might be some queries in these directories that are used by multiple commands.
269
275
270
-
You can see in the `src/operations/graph` directory a number of `.rs` files paired with `.graphql` files. The `.graphql` files are the files where the GraphQL operations live, and the matching `.rs` files contain the logic needed to execute those operations.
276
+
You can see in the `src/query/graph` directory a number of `.rs` files paired with `.graphql` files. The `.graphql` files are the files where the GraphQL operations live, and the matching `.rs` files contain the logic needed to execute those operations.
271
277
272
278
##### Writing a GraphQL operation
273
279
274
280
For our basic `graph hello` command, we're going to make a request to Apollo Studio that inquires about the existence of a particular graph, and nothing else. For this, we can use the `Query.service` field.
275
281
276
-
Create a `hello_query.graphql` file in `crates/rover-client/src/operations/graph` and paste the following into it:
282
+
Create a `hello.graphql` file in `crates/rover-client/src/query/graph` and paste the following into it:
277
283
278
284
```graphql
279
-
queryGraphHello($graph_id: ID!) {
280
-
service(id: $graph_id) {
285
+
queryGraphHello($graphId: ID!) {
286
+
service(id: $graphId) {
281
287
deletedAt
282
288
}
283
289
}
@@ -289,34 +295,32 @@ This basic GraphQL operation uses a graph's unique ID (which we get from the `Gr
289
295
290
296
This project uses [graphql-client](https://docs.rs/graphql_client/latest/graphql_client/) to generate types for each raw `.graphql` query that we write.
291
297
292
-
First, create an empty directory at `crates/rover-client/src/operations/graph/hello`, and then in that directory, create a `mod.rs` file to initialize the module.
298
+
First, create an empty file at `crates/rover-client/src/query/graph/hello.rs`.
293
299
294
-
To start compiling this file, we need to export the module in `crates/rover-client/src/operations/graph/mod.rs`:
300
+
To start compiling this file, we need to export the module in `crates/rover-client/src/query/graph/mod.rs`:
295
301
296
302
```rust
297
303
...
298
-
/// "graph hello" command execution
304
+
/// "Graph hello" command execution
299
305
pubmodhello;
300
306
```
301
307
302
-
Back in our `hello` module, we'll create a `runner.rs`, and add `mod runner` to our `mod.rs` file.
303
-
304
-
Then, in `runner.rs`, import the following types:
308
+
Back in `hello.rs`, we'll import the following types:
305
309
306
310
```rust
307
311
usecrate::blocking::StudioClient;
308
312
usecrate::RoverClientError;
309
313
usegraphql_client::*;
310
314
```
311
315
312
-
Then, we'll create a new struct that will have auto-generated types for the `hello_query.graphql` file that we created earlier:
316
+
Then, we'll create a new struct that will have auto-generated types for the `hello.graphql` file that we created earlier:
313
317
314
318
```rust
315
319
#[derive(GraphQLQuery)]
316
320
// The paths are relative to the directory where your `Cargo.toml` is located.
317
321
// Both json and the GraphQL schema language are supported as sources for the schema
This should get you to the point where you can run `rover graph hello <GRAPH_REF>` and see if and when the last graph was deleted. From here, you should be able to follow the examples of other commands to write out tests for the `build_response` function.
398
-
399
-
##### Clean up the API
398
+
This should get you to the point where you can run `rover graph hello <GRAPH_REF>` and see if and when the last graph was deleted. From here, you should be able to follow the examples of other commands to write out tests for the `build_response` function. This is left as an exercise for the reader.
400
399
401
-
Unfortunately this is not the cleanest API and doesn't match the pattern set by the rest of the commands. Each `rover-client` operation has an input type and an output type, along with a `run` function that takes in a `reqwest::blocking::Client`.
400
+
##### `RoverStdout`
402
401
403
-
You'll want to define all of the types scoped to this command in `types.rs`, and re-export them from the top level `hello` module, and nothing else.
402
+
Now that you can actually execute the `hello::run` query and return its result, you should create a new variant of `RoverStdout`in `src/command/output.rs` that is not `PlainText`. Your new variant should print the descriptor using the `print_descriptor` function, and print the raw content using `print_content`.
404
403
405
-
##### `RoverOutput`
406
-
407
-
Now that you can actually execute the `hello::run` query and return its result, you should create a new variant of `RoverOutput` in `src/command/output.rs` that is not `None`. Your new variant should print the descriptor using the `print_descriptor` function, and print the raw content using `print_content`.
408
-
409
-
To do so, change the line `Ok(RoverOutput::None)` to `Ok(RoverOutput::DeletedAt(deleted_at))`, add a new `DeletedAt(String)` variant to `RoverOutput`, and then match on it in `pub fn print(&self)` and `pub fn get_json(&self)`:
404
+
To do so, change the line `Ok(RoverStdout::PlainText(deleted_at))` to `Ok(RoverStdout::DeletedAt(deleted_at))`, add a new `DeletedAt(String)` variant to `RoverStdout`, and then match on it in `pub fn print(&self)`:
0 commit comments