cynic icon indicating copy to clipboard operation
cynic copied to clipboard

Dynamic GraphQL query

Open rluvaton opened this issue 4 months ago • 1 comments

In the README it says:

Building dynamic (but still type checked) queries at run time.

But I can't find a way to do it.

I want to fetch 100 issues with the comments for specific repository, and when there are more than 100 comments I want to create dynamic query to only fetch the issues that have more than 100 comments with the next cursor.

Example: I fetched issues from the Rust repository, and 2 of the issues that I fetched have more than 100 comments, so I want to create the following query:

query($issue_1: Int!, $cursor_1: String, $issue_2: Int!, $cursor_2: String) {
  repository(owner: "rust-lang", name: "rust") {
    
    issue_1: issue(number: $issue_1) {
      comments(first: 100, after: $cursor_1) {
        pageInfo {
          endCursor
          hasNextPage
        }
        nodes {
          author {
            login
          }
          body
        }
      }
    }
    issue_2: issue(number: $issue_2) {
      comments(first: 100, after: $cursor_2) {
        pageInfo {
          endCursor
          hasNextPage
        }
        nodes {
          author {
            login
          }
          body
        }
      }
    }
  }
}

and pass my params:

{
  "issue_1": 32838,
  "cursor_1": "Y3Vyc29yOnYyOpHOFCXvSQ",
  "issue_2": 35121,
  "cursor_2": "Y3Vyc29yOnYyOpHOEyFHLQ=="
}

with the issues that I need and comments after a cursor (in this example there is 2 issues, but it can be 10, 20, etc.

How can I do that?

rluvaton avatar Aug 29 '25 12:08 rluvaton

Hi @rluvaton - unfortunately this isn't quite possible right now. It is what I meant by "Dynamic Queries" in the README, and supporting this was always a design goal in cynic.

However until recently I'd never needed the functionality, so hadn't tried it out and hadn't realized that the API doesn't quite work if you need to feed data in to a dynamic query. This obviously makes the functionality kind of useless at the moment.

I do really want to support this though, and have a fairly solid idea of how to do it - hoping I'll get some time today or tomorrow to work on it.

obmarg avatar Aug 30 '25 12:08 obmarg