graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

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

Open ta32 opened this issue 3 years ago • 7 comments

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

ta32 avatar Dec 09 '21 22:12 ta32

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.

tomhoule avatar Dec 10 '21 07:12 tomhoule

sure I can have a go a fixing this issue

ta32 avatar Dec 10 '21 10:12 ta32

Hello, any update?

zzhengzhuo avatar Apr 02 '22 02:04 zzhengzhuo

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.

ta32 avatar Apr 03 '22 11:04 ta32

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

timvw avatar May 01 '24 08:05 timvw

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

ta32 avatar May 01 '24 09:05 ta32