shurcooL / githubv4

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

Help in structuring a query #94

Closed carlqt closed 2 years ago

carlqt commented 2 years ago

I need help/example on how to structure this query

query rootQuery {
  search(query: "repo:cli/cli is:open type:pr", type: ISSUE, first: 10) {
    nodes {
      ... on PullRequest {
        id
        url
        title
        author {
          login
        }
      }
    }
  }
}

My structure looks like:

type SearchQuery struct {
    Search Search `graphql:"search(query: $q, type: $type, first: $first)"`
}

type Search struct {
    Nodes []PullRequestFragment `graphql:"... on PullRequest"`
}

type PullRequestFragment struct {
    ID        string
    Title     string
    Url       string
    CreatedAt time.Time
    UpdatedAt time.Time
    Author    struct {
        Login string `json:"login"`
    }
}

And the errors that I got is Fragment on PullRequest can't be spread inside SearchResultItemConnection

dmitshur commented 2 years ago

You may be missing one struct layer that corresponds to nodes:

type Search struct {
    Nodes []struct {
        PullRequestFragment `graphql:"... on PullRequest"`
    }
}
carlqt commented 2 years ago

Thanks. Tried it and it works.

carlqt commented 2 years ago

Closing this issue since it's been resolved.