Skip to content

Add null safety checks in GraphQL response handling #558

@MariusStorhaug

Description

Request

Problem: Several private functions that use GraphQL to fetch a single resource do not check for $null before constructing class instances. When the queried resource does not exist (e.g., a deleted repository, a non-existent enterprise, or a release with no matching tag), the GraphQL response returns null for that field, causing constructor errors.

Context

With the introduction of partial error handling in Invoke-GitHubGraphQLQuery (see #557), GraphQL queries can now return partial data where some fields are null alongside errors. Functions must guard against null values to avoid runtime exceptions when constructing typed objects.

Acceptance criteria

  • All affected functions check for null before calling class constructors
  • Functions silently return nothing (no output) when the queried resource is null
  • No constructor errors when querying non-existent repositories, enterprises, or releases

Technical decisions

Pattern: Assign the GraphQL result to a variable, then wrap the constructor call in an if ($variable) check. This is consistent with PowerShell idioms and keeps the code readable.

Affected functions:

Function File Null field
Get-GitHubRepositoryByName src/functions/private/Repositories/Get-GitHubRepositoryByName.ps1 $_.repositoryOwner.repository
Get-GitHubMyRepositoryByName src/functions/private/Repositories/Get-GitHubMyRepositoryByName.ps1 $_.viewer.repository
Get-GitHubEnterpriseByName src/functions/private/Enterprise/Get-GitHubEnterpriseByName.ps1 $enterpriseResult.enterprise
Get-GitHubReleaseAssetByTag src/functions/private/Releases/Assets/Get-GitHubReleaseAssetByTag.ps1 $_.repository.release
Get-GitHubReleaseAssetFromLatest src/functions/private/Releases/Assets/Get-GitHubReleaseAssetFromLatest.ps1 $_.repository.latestRelease

No new tests: These are defensive null checks on private functions. They will be exercised by existing integration tests when querying non-existent resources.


Implementation plan

Core changes

  • Get-GitHubRepositoryByName — guard $_.repositoryOwner.repository before constructing [GitHubRepository]
  • Get-GitHubMyRepositoryByName — guard $_.viewer.repository before constructing [GitHubRepository]
  • Get-GitHubEnterpriseByName — guard $enterpriseResult.enterprise before constructing [GitHubEnterprise]
  • Get-GitHubReleaseAssetByTag — guard $_.repository.release before processing assets and pagination
  • Get-GitHubReleaseAssetFromLatest — guard $_.repository.latestRelease before processing assets and pagination

Metadata

Metadata

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions