githubv4 icon indicating copy to clipboard operation
githubv4 copied to clipboard

How to check if the result is null?

Open hug6 opened this issue 5 years ago • 1 comments

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?

hug6 avatar May 18 '20 17:05 hug6

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
}
// ...

dmitshur avatar May 18 '20 17:05 dmitshur