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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

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
Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.