As you know, I like using PowerShell to perform all administrative tasks. I also like to use the Graph API to assist me in querying the data I may need.

Recently I have been looking into “GraphQL.”

GraphQL is a query language for your API that Facebook developed. It provides a flexible and efficient way for clients to request data from servers. With GraphQL, clients can specify precisely the data they need, and the server will only return that data rather than a fixed set of data as in a REST API. GraphQL works well with modern single-page applications and mobile applications that need to be flexible and responsive to changes on the backend. It makes it easier to evolve APIs over time and enables powerful developer tools.

As I was reading about this, I wondered how easy it would be to combine PowerShell with the GraphQL language. Here is an example that queries the SpaceX GraphQL API to get information about Rockets! 

https://studio.apollographql.com/public/SpaceX-pxxbxen/home?variant=current

Who doesn’t love that 🙂

# Set the GraphQL endpoint and headers
$uri = "https://api.spacex.land/graphql"
$headers = @{
    "Content-Type" = "application/json"
}

# Define the GraphQL query
$query = @"
{
  rockets {
    name
  }
}
"@
$body = @{
    "query" = $query
}

# Convert the body to JSON
$json = $body | ConvertTo-Json

# Send the GraphQL request
$response = Invoke-WebRequest `
    -Method Post `
    -Uri $uri `
    -Headers $headers `
    -Body $json

# Print the response
$json = $response.Content | ConvertFrom-Json
$json.data.rockets | ForEach-Object {
    Write-Output $_.name
}

We could modify this by reviewing the schema (https://studio.apollographql.com/public/SpaceX-pxxbxen/schema/reference?variant=current) and the explorer (https://studio.apollographql.com/public/SpaceX-pxxbxen/explorer?variant=current) to understand the structure needed to retrieve the correct properties and objects. The following example retrieves much more properties.

# Set the GraphQL endpoint and headers
$uri = "https://api.spacex.land/graphql"
$headers = @{
    "Content-Type" = "application/json"
}

# Define the GraphQL query
$query = @"
{
    rockets {
        name
        description
        first_flight
        height {
          feet
        }
        payload_weights {
          id
        }
        country
        company
        cost_per_launch
      }
}
"@
$body = @{
    "query" = $query
}

# Convert the body to JSON
$json = $body | ConvertTo-Json

# Send the GraphQL request
$response = Invoke-WebRequest `
    -Method Post `
    -Uri $uri `
    -Headers $headers `
    -Body $json

# Print the response
$json = $response.Content | ConvertFrom-Json
$json.data.rockets | ForEach-Object {
    Write-Host "$($_.name)" -ForegroundColor Blue
    Write-Host "$($_.description)" -ForegroundColor Blue
    Write-Host "First Flight: $($_.first_flight)" -ForegroundColor Yellow
    Write-Host "Height (ft): $($_.height.feet)" -ForegroundColor Yellow
    Write-Host "Payload Weight: $($_.payload_weights[0].id)" -ForegroundColor Yellow
    Write-Host "Country: $($_.country)" -ForegroundColor Yellow
    Write-Host "Company: $($_.company)" -ForegroundColor Yellow
    Write-Host "Cost Per Launch: $($_.cost_per_launch) USD" -ForegroundColor Red
    Write-Host "*****************************" -ForegroundColor Gray
}

Here is another example of getting all launches and details:

# Set the GraphQL endpoint and headers
$uri = "https://api.spacex.land/graphql"
$headers = @{
    "Content-Type" = "application/json"
}

# Define the GraphQL query
$query = @"
query Launches
{
  launches {
    id
    is_tentative
    upcoming                                    
    mission_name
    links {
      article_link
      video_link
      flickr_images
      mission_patch
    }
    launch_date_utc
    details
  }
}
"@
$body = @{
    "query" = $query
}

# Convert the body to JSON
$json = $body | ConvertTo-Json

# Send the GraphQL request
$response = Invoke-WebRequest `
    -Method Post `
    -Uri $uri `
    -Headers $headers `
    -Body $json

# Print the response
$json = $response.Content | ConvertFrom-Json
$json.data.launches | ForEach-Object {
    Write-Host "Mission Name: $($_.mission_name)" -ForegroundColor White
    Write-Host "Details: $($_.details)" -ForegroundColor White
    Write-Host "Upcoming: $($_.upcoming)" -ForegroundColor Cyan
    Write-Host "Launch Date: $($_.launch_date_utc)" -ForegroundColor Cyan
    Write-Host "Article: $($_.links.article_link)" -ForegroundColor Cyan
    Write-Host "Video: $($_.links.video_link)" -ForegroundColor Cyan

    $_.links.flickr_images | ForEach-Object {
        Write-Host "Images: $($_)" -ForegroundColor Cyan
      }

    Write-Host "*****************************" -ForegroundColor Gray
}

Using PowerShell with GraphQL allows developers to create scripts that can query GraphQL APIs and process the returned data in powerful and efficient ways. It can be handy for data aggregation, migration, and system integration, where a flexible and expressive API query language like GraphQL can provide significant benefits.