Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying GraphQL with PowerShell

So I've been trying to query GraphQL from PowerShell. Everything looks perfect in a cmd, but I cannot make it work in PS, see, I have this code so far:

$oAuth2TokenUrl = "https://api.cloudflare.com/client/v4/zones/0000/analytics/dashboard"
$accessKey = '1111' 

$Cloudflare_Oauth_Header = @{
    "Authorization" = "Bearer $accessKey";
}

$query = @'
{ "query": "query { viewer {
zones(filter: {zoneTag: 0000}) {
  httpRequests1mGroups(orderBy: [datetimeMinute_ASC], limit: 1000, filter: {datetime_geq: "2019-09-08T20:00:00Z", datetime_lt: "2019-09-08T20:02:00Z"}) {
    dimensions {datetimeMinute}
    sum {
      browserMap {
        pageViews
        uaBrowserFamily
      }
      bytes
      cachedBytes
      cachedRequests
      contentTypeMap {
        bytes
        requests
        edgeResponseContentTypeName
      }
      clientSSLMap {
        requests
        clientSSLProtocol
      }
      countryMap {
        bytes
        requests
        threats
        clientCountryName
      }
      encryptedBytes
      encryptedRequests
      ipClassMap {
        requests
        ipType
      }
      pageViews
      requests
      responseStatusMap {
        requests
        edgeResponseStatus
      }
      threats
      threatPathingMap {
        requests
        threatPathingName
      }
    }
    uniq {
      uniques
    }
  }
}
  } }" }
'@


$Cloudflare_zone = Invoke-RestMethod -Method Post -Headers $Cloudflare_Oauth_Header -ContentType "application/json; charset=utf-8" -Uri $oAuth2TokenUrl -Body $query 

And I keep getting the error Invoke-RestMethod : The remote server returned an error: (501) Not Implemented. But if I curl in cmd it returns what is expected and no error is shown.

Please let me know if you need more details.

like image 714
Bryan Arreola Avatar asked Nov 18 '25 05:11

Bryan Arreola


1 Answers

I have a GraphQL module for PowerShell with a single function (Invoke-GraphQLQuery that may help you here). Link: https://github.com/anthonyg-1/PSGraphQL

But I definitely agree that you should start with a much smaller query. Check out some of the examples in my readme or run Get-Help Invoke-GraphQLQuery for more.

like image 171
AnthonyG Avatar answered Nov 21 '25 06:11

AnthonyG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!