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:
216
210
217
211
```console
@@ -228,13 +222,13 @@ For more information try --help
228
222
229
223
##### Setting up a command to work with `rover-client`
230
224
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.
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.
232
226
233
227
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`.
234
228
235
229
You can do this by changing the `Command::Hello(command) => command.run(),` line to `Command::Hello(command) => command.run(client_config),`.
236
230
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.
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.
238
232
239
233
##### Auto-generated help command
240
234
@@ -271,19 +265,19 @@ Whenever you create a new command, make sure to add `#[serde(skip_serializing)]`
271
265
272
266
##### Adding a query to Apollo Studio
273
267
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.
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.
275
269
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.
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.
277
271
278
272
##### Writing a GraphQL operation
279
273
280
274
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.
281
275
282
-
Create a `hello.graphql` file in `crates/rover-client/src/query/graph` and paste the following into it:
276
+
Create a `hello_query.graphql` file in `crates/rover-client/src/operations/graph` and paste the following into it:
283
277
284
278
```graphql
285
-
queryGraphHello($graphId: ID!) {
286
-
service(id: $graphId) {
279
+
queryGraphHello($graph_id: ID!) {
280
+
service(id: $graph_id) {
287
281
deletedAt
288
282
}
289
283
}
@@ -295,32 +289,34 @@ This basic GraphQL operation uses a graph's unique ID (which we get from the `Gr
295
289
296
290
This project uses [graphql-client](https://docs.rs/graphql_client/latest/graphql_client/) to generate types for each raw `.graphql` query that we write.
297
291
298
-
First, create an empty file at `crates/rover-client/src/query/graph/hello.rs`.
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.
299
293
300
-
To start compiling this file, we need to export the module in `crates/rover-client/src/query/graph/mod.rs`:
294
+
To start compiling this file, we need to export the module in `crates/rover-client/src/operations/graph/mod.rs`:
301
295
302
296
```rust
303
297
...
304
-
/// "Graph hello" command execution
298
+
/// "graph hello" command execution
305
299
pubmodhello;
306
300
```
307
301
308
-
Back in `hello.rs`, we'll import the following types:
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:
309
305
310
306
```rust
311
307
usecrate::blocking::StudioClient;
312
308
usecrate::RoverClientError;
313
309
usegraphql_client::*;
314
310
```
315
311
316
-
Then, we'll create a new struct that will have auto-generated types for the `hello.graphql` file that we created earlier:
312
+
Then, we'll create a new struct that will have auto-generated types for the `hello_query.graphql` file that we created earlier:
317
313
318
314
```rust
319
315
#[derive(GraphQLQuery)]
320
316
// The paths are relative to the directory where your `Cargo.toml` is located.
321
317
// 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. This is left as an exercise for the reader.
397
+
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
399
400
400
-
##### `RoverStdout`
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`.
401
402
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`.
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.
403
404
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)`:
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)`:
0 commit comments