graphql-rust/graphql-client

How to traverse macro-codegenerated nested query structure?

x10an14 opened this issue · 1 comments

Hi!

I'm struggling to traverse the returned output of my GQL query's structure in Rust, nor can I build docs to see if this crates auto-generates doc that could give me hints as to how.

Ref #420 (comment), I got as far as src/main.rs shows in this commit; https://git.sr.ht/~x10an14/github-project-migrator/tree/80c560d7bb031677ed400dd20243c2a8edc677be/.

And my output to stdout shows this - so it should be possible, somehow:

stdout output
src/main.rs:53] a = Organization(
    ProjectCardsRepositoryOwnerOnOrganization {
        project: Some(
            ProjectCardsRepositoryOwnerOnOrganizationProject {
                name: "<REDACTED>",
                url: "<REDACTED>",
                columns: ProjectCardsRepositoryOwnerOnOrganizationProjectColumns {
                    edges: Some(
                        [
                            Some(
                                ProjectCardsRepositoryOwnerOnOrganizationProjectColumnsEdges {
                                    node: Some(
                                        ProjectCardsRepositoryOwnerOnOrganizationProjectColumnsEdgesNode {
                                            name: "Todo",
                                            cards: ProjectCardsRepositoryOwnerOnOrganizationProjectColumnsEdgesNodeCards {
                                                edges: Some(
                                                    [
                                                        Some(
                                                            ProjectCardsRepositoryOwnerOnOrganizationProjectColumnsEdgesNodeCardsEdges {
                                                                node: Some(
                                                                    ProjectCardsRepositoryOwnerOnOrganizationProjectColumnsEdgesNodeCardsEdgesNode {
                                                                        id: "<REDACTED>",
                                                                        state: Some(
                                                                            CONTENT_ONLY,
                                                                        ),
                                                                        resource_path: "<REDACTED>",
                                                                        url: "<REDACTED>",
                                                                    },
                                                                ),
                                                            },
                                                        )
`cargo doc` error and cause
[2022-09-15 17:36:06] 0 x10an14@home-desktop:~/Documents/sr.ht/github-project-migrator
-> $ cargo doc
    Checking thiserror v1.0.35
 Documenting tracing v0.1.36
 Documenting serde v1.0.144
 Documenting tokio v1.21.1
 Documenting openssl v0.10.41
 Documenting thiserror v1.0.35
error[E0554]: `#![feature]` may not be used on the stable release channel
   --> /home/x10an14/.cargo/registry/src/github.com-1ecc6299db9ec823/thiserror-1.0.35/src/lib.rs:211:34
    |
211 | #![cfg_attr(provide_any, feature(provide_any))]
    |                                  ^^^^^^^^^^^

For more information about this error, try `rustc --explain E0554`.
error: could not compile `thiserror` due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not document `thiserror`

Caused by:
  process didn't exit successfully: `rustdoc --edition=2018 --crate-type lib --crate-name thiserror /home/x10an14/.cargo/registry/src/github.com-1ecc6299db9ec823/thiserror-1.0.35/src/lib.rs --cap-lints allow -o /home/x10an14/Documents/sr.ht/github-project-migrator/target/doc --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat -C metadata=a86faed81653d357 -L dependency=/home/x10an14/Documents/sr.ht/github-project-migrator/target/debug/deps --extern thiserror_impl=/home/x10an14/Documents/sr.ht/github-project-migrator/target/debug/deps/libthiserror_impl-1f83c15b36555f63.so --crate-version 1.0.35 --cfg provide_any` (exit status: 1)
[2022-09-15 22:13:30] 101 x10an14@home-desktop:~/Documents/sr.ht/github-project-migrator
-> $ cargo tree -i thiserror
thiserror v1.0.35
└── graphql-parser v0.4.0
    └── graphql_client_codegen v0.11.0
        └── graphql_query_derive v0.11.0 (proc-macro)
            └── graphql_client v0.11.0
                └── github-project-migrator v0.1.0 (/home/x10an14/Documents/sr.ht/github-project-migrator)
[2022-09-15 22:13:44] 0 x10an14@home-desktop:~/Documents/sr.ht/github-project-migrator
-> $ 

No matter what I import from use project_cards::, or no matter what I write, I can't get the Rust compiler to tell me what it expects after line 52;

`git diff` showing attempt
[2022-09-16 10:59:13] 0 x10an14@home-desktop:~/Documents/sr.ht/github-project-migrator
-> $ git diff

src/main.rs

  6 ⋮  6 │use tracing::debug;
  7 ⋮  7 │use tracing::trace;
  8 ⋮  8 │
    ⋮  9 │use project_cards::ProjectCardsRepositoryOwner;
    ⋮ 10 │
  9 ⋮ 11 │type URI = String;
 10 ⋮ 12 │
 11 ⋮ 13 │#[derive(GraphQLQuery)]

 45 ⋮ 47 │    .await?;
 46 ⋮ 48 │    trace!("Graphql query response: {:?}", &response_body);
 47 ⋮ 49 │
 48 ⋮    │    let a = &response_body
    ⋮ 50 │    let a = response_body
 49 ⋮ 51 │        .data
 50 ⋮ 52 │        .expect("Expected graphql data - double-check query and 'GITHUB_TOKEN' envvar.")
 51 ⋮ 53 │        .repository_owner
 52 ⋮    │        .expect("Expected hit on given organization name.");
    ⋮ 54 │        .expect("Expected hit on given organization name.")
    ⋮ 55 │        .ProjectCardsRepositoryOwner::Organization;
    ⋮ 56 │        //.project;
 53 ⋮ 57 │    dbg!(a);
 54 ⋮ 58 │    //debug!("GQL Response HTTP Code: {}");
 55 ⋮ 59 │    Ok(())
[2022-09-16 11:00:21] 0 x10an14@home-desktop:~/Documents/sr.ht/github-project-migrator
-> $ 

I figured out that there's no way to decipher/wrap one's head around the macro generated structures more complex than the more simple ones.

To that end I managed to dig up this fix from thiserror github issues: dtolnay/thiserror#192

cargo clean && cargo doc worked for me too.