Forum Discussion

Mandated's avatar
Mandated
Regular Visitor
2 years ago

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