How to use Power Query to load data from a GraphQL endpoint into Power BI
GraphQL is a query language for APIs, and it provides a more efficient, powerful, and flexible alternative to REST APIs. It allows clients to request only the data they need and combine data from multiple sources into a single query.
With GraphQL, you define the structure of the data you need, and the server returns exactly that, making it a perfect fit for modern applications.
Loading Data from GraphQL to Power BI:
Below is the M code (Power Query language) example that loads data from a GraphQL endpoint into Power BI.
Loading Data from GraphQL to Power BI:
Below is the M code (Power Query language) example that loads data from a GraphQL endpoint into Power BI.
This example uses the GraphQL endpoint https://countries.trevorblades.com/ to get a list of countries, with their codes, and capitals.
let
/* Define the source from the GraphQL endpoint */
Source = Web.Contents (
/* "https://your-graphql-endpoint.com/graphql", */
"https://countries.trevorblades.com/",
[
Headers = [
#"Method" = "POST",
#"Content-Type" = "application/json"
/* #"Authorization" = "Bearer your_access_token" */
],
Content = Text.ToBinary("{""query"": ""{ countries { code name capital } }""}")
]
),
/* Parse the JSON response */
JSON_Source = Json.Document(Source),
SrcData = JSON_Source[data],
CountriesList = SrcData[countries],
/* Convert the list of countries to a table */
Convert2Table = Table.FromList(CountriesList, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
/* Expand the records to get the country details */
ExpandTable =
Table.ExpandRecordColumn(Convert2Table,
"Column1", {"code", "name", "capital"}, {"CountryCode", "CountryName", "CapitalCity"}
),
/* Filter rows to include only specific countries */
FilterRows = Table.SelectRows(ExpandTable, each ([CountryName] = "Australia" or [CountryName] = "Austria" or
[CountryName] = "Belgium" or [CountryName] = "Brazil" or [CountryName] = "France" or [CountryName] = "India" or
[CountryName] = "Italy" or [CountryName] = "Japan" or [CountryName] = "Mexico" or [CountryName] = "Singapore" or
[CountryName] = "Sweden" or [CountryName] = "Switzerland" or [CountryName] = "United Kingdom" or [CountryName] = "United States"))
in
Notes:
GraphQL Endpoint: In the above example, we have used below public GraphQL API is to fetch a list of countries along with their capital cities.
https://countries.trevorblades.com/
GraphQL Query: The following query fetches a list of countries along with their country codes and capital cities.
{ countries { code name capital } }
Headers: The headers include the HTTP method (POST) and content type (application/json).
Authorization:
This is the header key used to send authentication credentials.
"Authorization" = "Bearer your_access_token"
Bearer:
This indicates the type of token being used; in this case, a bearer token. The Bearer tokens are commonly used in OAuth 2.0, a protocol for authorization, where the token is issued by an authentication server and then presented to the resource server to gain access to protected resources.
your_access_token:
This is the actual token that grants access to the protected resource. If your API requires an authorization token, replace your_access_token with your actual token. For public APIs the authorization header can be omitted.
Content: The content includes the GraphQL query in JSON format.
#--------------------------------------------------------------Thanks--------------------------------------------------------------#
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. If you wish, please share your genuine Feedback or comments only related to this Blog Posts. It is my humble request that, please do not post any Spam comments or Advertising kind of comments, which will be Ignored.