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

Inline fragments with strings #19

Closed ideate closed 7 years ago

ideate commented 7 years ago

ran into issues running variables with inline fragments within a github query string graphql:"search(query: \"language:$language\", type: REPOSITORY, first:$pageSize)"

however, this version works correctly with variables: graphql:"search(query: \"language:PowerShell\", type: REPOSITORY, first:$pageSize)"

variables := map[string]interface{}{
    "language": githubql.String("PowerShell"),
    "pageSize": githubql.Int(100),
}
err := client.Query(context.Background(), &query, variables)
dmitshur commented 7 years ago

ran into issues

What were the issues?

I think this is just how GraphQL works:

https://facebook.github.io/graphql/#sec-Language.Variables

A variable needs to provide an entire value. There is no support for doing string concatenation.

image

You'll need to do this instead:

graphql:"search(query: $query, type: REPOSITORY, first: $pageSize)"

query := fmt.Sprintf("language: %s", "PowerShell")

variables := map[string]interface{}{
    "query": githubql.String(query),
    "pageSize": githubql.Int(100),
}
err := client.Query(context.Background(), &query, variables)

Let me know if you find something in the GraphQL spec that says variables are expected to work in the way you suggested.

ideate commented 7 years ago

awesome! that makes sense! it looks like i assumed that the behavior was similar to JS template literals