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

How to check if the result is null? #65

Open hug6 opened 4 years ago

hug6 commented 4 years ago

query:

{
  repositoryOwner(login: "_dnxv__") {
    ... on ProfileOwner {
      pinnedItemsRemaining
      itemShowcase {
        hasPinnedItems
      }
    }
  }
}

result:

{
  "data": {
    "repositoryOwner": null
  }
}

but the variable query in golang is a struct, so how to check if it's null? any workaround or tricks?

dmitshur commented 4 years ago

You can use a pointer to a struct, and check if the pointer value is nil. For example:

var q struct {
    RepositoryOwner *struct {
        ProfileOwner struct {
            PinnedItemsRemaining int
            // ...
        } `graphql:"... on ProfileOwner"`
    } `graphql:"repositoryOwner(login: \"_dnxv__\")"`
}

// call client.Query(...)

if q.RepositoryOwner == nil {
    // handle this case
}
// ...