graphql-rust / graphql-client

Typed, correct GraphQL requests and responses in Rust
Apache License 2.0
1.15k stars 158 forks source link

Query with default variable that is an enums type doesn't compile #403

Open ta32 opened 2 years ago

ta32 commented 2 years ago

When this issue was happening I was using the derive macro, however the compile error was very strange. Warning about taking a str. I switch to the code gen method to see what was happening.

The query has variables where one is typed as an enum

query MergedPrQuery($user: String = "user", $state: [PullRequestState!] = [MERGED] ) {
  user(login: $user) {
    pullRequests(states: $state, first:100, orderBy: { field: CREATED_AT, direction: DESC} ) {
      nodes {
        number,
        baseRefName,
        headRefName,
        createdAt
      }
    }
  }
}

The generated variable section is:

    #[derive(Serialize)]
    pub struct Variables {
        pub user: Option<String>,
        pub state: Option<Vec<PullRequestState>>,
    }
    impl Variables {
        pub fn default_user() -> Option<String> {
            Some("user".to_string())
        }
        pub fn default_state() -> Option<Vec<PullRequestState>> {
            Some(vec!["MERGED"])
        }
    }

I manually corrected it too

    #[derive(Serialize)]
    pub struct Variables {
        pub user: Option<String>,
        pub state: Option<Vec<PullRequestState>>,
    }
    impl Variables {
        pub fn default_user() -> Option<String> {
            Some("user".to_string())
        }
        pub fn default_state() -> Option<Vec<PullRequestState>> {
            Some(vec![PullRequestState::MERGED])
        }
    }

Then the query works, I suspect the same issue was occurring with the macro derive method. Good thing we have this code gen tool because it would be much harder to troubleshoot the proc macro :)

would this be a simple fix for a beginner, I don't have much knowledge about proc macros though

tomhoule commented 2 years ago

Hi @ta32, thanks for reporting this! I think this can be fixed without touching any macro code. My first instinct would be to look here: https://github.com/graphql-rust/graphql-client/blob/fd5dc3dc7223616e1f5a3b35df4b39e8f80e6412/graphql_client_codegen/src/codegen.rs#L96 — it should also be pretty easy to write a test (with a minimal schema, to check that it doesn't compile before the fix, then that it does after the fix and that the default is what you would expect). If you want to pick this up, I'm happy to review a PR — I can't guarantee when it's going to be fixed otherwise, since there is very little free time to work on this project.

ta32 commented 2 years ago

sure I can have a go a fixing this issue

zzhengzhuo commented 2 years ago

Hello, any update?

ta32 commented 2 years ago

I created a failing test, but I wanted to simplify it a bit to make it easier to debug the code and understand this. There is a manual work around for this issue - use the code gen mode and fix the generated code to get it to compile.

timvw commented 6 months ago

Any further updates on this? (Still need to apply the manual workaround when using the cli)

ta32 commented 6 months ago

Hi I am working on another project at the moment - my knowledge of rust macro's isn't that great so probably not going to pick up this issue any time soon

tomhoule commented 2 weeks ago

No updates as long as nobody picks this up. Nobody is paid to work on this crate, but several people have merge and release permissions.