shurcooL / githubv4

Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql).
MIT License
1.11k stars 89 forks source link

Question: How to add a querystring as a variable? #59

Closed superbeeny closed 4 years ago

superbeeny commented 4 years ago

Hi,

Hopefully this is simple miss on my part.

    var query struct {
        Search struct {
            RepositoryCount int
            PageInfo        struct {
                EndCursor   githubv4.String
                HasNextPage bool
            }
            Repos []struct {
                Repository respository `graphql:"... on Repository"`
            } `graphql:"nodes"`
        } `graphql:"search(first: 100, after: $repocursor, type: REPOSITORY, query: $querystring)"`
        RateLimit struct {
            Cost      githubv4.Int
            Limit     githubv4.Int
            Remaining githubv4.Int
            ResetAt   githubv4.DateTime
        }
    }

    variables := map[string]interface{}{
        "repocursor":  (*githubv4.String)(nil),
        "querystring": githubv4.String(`\"archived: false pushed:>2020-04-01 created:2020-01-01..2020-02-01\"`),
    }

What is the correct format/method to substitute in a querystring?

Thanks in advance

dmitshur commented 4 years ago

I'm not completely sure what you mean by "substitute"? Do you mean change its value?

Its value is provided in the variables map above. So, you can just modify it there:

variables["querystring"] = githubv4.String(`"archived: true"`)

Also see https://github.com/shurcooL/githubv4#arguments-and-variables.

superbeeny commented 4 years ago

when I put it into the graphql query like } graphql:"search(first: 100, after: $repocursor, type: REPOSITORY, query: \"pushed:>2020-04-01 created:2020-01-01..2020-02-01\")"

I get results, when I choose to use a variable $querystring and variable in graphql:"search(first: 100, after: $repocursor, type: REPOSITORY, query: $querystring)"

I get nothing.

dmitshur commented 4 years ago

Oh. It might be because you're including double quotes in the string in the latter case (but not in the former). Try:

variables["querystring"] = githubv4.String(`pushed:>2020-04-01 created:2020-01-01..2020-02-01`)
superbeeny commented 4 years ago

That was it thanks!