Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Mandated
Regular Visitor

How should I integrate a GraphQL query with M?

I've been struggling to get the correct response from a GraphQL-based API.  I'm not too familiar with it, but on the website I can press a Run API button and plug in the following query and get back the desired results (I also input a numeric value for the variable Id):

 

query myQuery($Id: Int!) 
      {
        WorkerAccess(Id: $Id){
        Id
        ProjectName
        NextAction
        Sites {
          TotalCount
          PageInfo {
            HasNextPage
            EndCursor
          }
          Edges {
            Cursor
            Node {
              Id
              Name
              AccessStatus
            }
          }
          Items {
            Id
            Name
            AccessStatus
          }
        }
        }
      }

 

A template for using Power Query places the same GraphQL query in a variable, but something isn't right.  My best guess is that it is the variable I'm using, workerId, is not in the correct format or isn't being recognized properly.  In GraphQL's UI there is a separate area where variable values can be entered, but this is not possible AFAIK in PQ.  The entire query follows:

 

 

let
  Source = (apiToken as text, workerId as number, optional endCursor as text, optional data as list) =>
    let
      endCursor = if endCursor is null then "" else endCursor,
      query =  "
        query myQuery($Id: Int!) 
      {
        WorkerAccess(Id: \""" & workerId & "\""){
        Id
        ProjectName
        NextAction 
        Sites {
          TotalCount
          PageInfo {
            HasNextPage
            EndCursor
          }
          Edges {
            Cursor
            Node {
              Id
              Name
              AccessStatus
            }
          }
          Items {
            Id
            Name
            AccessStatus
          }
        }
      }
    }",
      JSON = Web.Contents("https://cloud.3dsafety.com.au/graphql",
          [
              Headers = [#"X-API-Key"=apiToken, #"Content-Type"="application/json"],
              Content = Text.ToBinary(query)
          ]
      ),
      Source = Json.Document(JSON),
      pageInfo = Source[data][results][PageInfo],
      items = Source[data][results][Items],
      appendedData =
        if pageInfo[HasNextPage] = true and data is null then
          List.Combine({{}, items})
        else List.Combine({data, items}),
          output =
            if pageInfo[HasNextPage] = true then
              @GetWorkerStatus(apiToken, pageInfo[EndCursor], appendedData)
            else
              Table.FromList(appendedData, Record.FieldValues, {"Id", "Name", "AccessStatus"})
    in
      output
in
  Source

  

Generally, the response I get is the 400 error (Bad Request).  In the above example, however, it tells me that the "&" operator cannot be used with text or numbers.  Alternatively, I tried using Text.Format("#{0}, {workerId})) by wrapping it around the query, but no luck there either.  Thanks for any help.

1 REPLY 1
lbendlin
Super User
Super User

try

 

WorkerAccess(Id: " & workerId & "){

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.