Query data Adobe Commerce Optimizer
Learn how to query data using GraphQL on an Adobe Commerce Optimizer instance.
Who is this video for?
- Commerce Solution Architect and developers
Video content
- Query data using GraphQL
- Using jq to make json easier to read
Hi, this is Russell with Adobe Commerce, and in this session, I’m going to go over how to query your Adobe Commerce Optimizer data using GraphQL.
For this first demonstration, I’m going to show you how to use the curl command, and it will be as simple a query as possible and still return relevant data.
When you use curl on this GraphQL endpoint, the response returned is formatted in JSON, and being JSON in a terminal window, this can be a bit hard to read. So a quick tip, to make it easier to read, you can use jq, which is similar to sed for JSON. Now, when I run that same curl command with jq piped at the end, the results are going to be much easier to read.
Now let’s dig into each part of the curl command to understand a few details, and a few that are important to Adobe Commerce Optimizer. The first part is pretty standard, the curl command, followed by the URL of the GraphQL endpoint. And a quick refresher, to get this endpoint, you head over to experience.adobe.com, locate your Commerce Optimizer instance, click on the information icon, and this exposes your GraphQL endpoint. Up next are headers, and for content type, we’re going to use application slash JSON. The second header is a special one for Adobe Commerce Optimizer, and it’s ac-source-locale. This value you can get from the catalog views found in your Adobe Commerce Optimizer dashboard. And in case you were wondering, you can get the URL for your Commerce Optimizer instance from the same section you got your GraphQL endpoint. So we can use this Commerce Optimizer URL. Doing so takes us to the Adobe Commerce Optimizer instance, and then under catalog views, you select the information icon, and it displays a few values. We’re looking for this one, call catalog sources, and here it’s en-us, and this is what we’ll use for the ac-source-locale header.
The last option is dash d, and this is used to send data as part of your HTTP POST request. In my initial request, it was rather long and wrapped the screen, so here’s a better example of what it looks like. It has two sections, the first being the query, the other is variables, and under variables, I defined one for search. In this demo, I was searching for tire. Another thing to note on this example is that I’m asking it to only return 10 items. And another interesting part about this is that I’m asking it to just return the second page of those results. And the rest is going to be all about the details that I want to have come back as part of the response. So for this, I’m asking for each item returned. I want the name, the SKU, the description, the short description, and images. And if it happens to be a simple product, I want to get all of the attributes and those labels, names, and values. And of course, we always want the pricing details and currency details. So that’s it for this session on querying your Adobe Commerce Optimizer data. Please continue to come back to Experience League to learn more about Adobe Commerce, as well as all of the other Adobe products.
Code Samples
Be sure to exchange values like{{insert-your-graphql-endpoint-url}}, {{insert-your-ac-view-id}} and {{your-search-query-string}} with the values needed on your query.
Basic sample query
curl '{{insert-your-graphql-endpoint-url}}' \
-H 'Content-Type: application/json' \
-H 'AC-View-ID: {{insert-your-ac-view-id}}' \
-d '{"query": "query ProductSearch($search: String!) { productSearch( phrase: $search, page_size: 10, current_page: 2) { items { productView { sku name description shortDescription images { url } ... on SimpleProductView { attributes { label name value } price { regular { amount { value currency } } roles } } } } } }", "variables": { "search": "{{your-search-query-string}}"}}'
Basic sample query using jq to pretty-print the output
curl '{{insert-your-graphql-endpoint-url}}' \
-H 'Content-Type: application/json' \
-H 'AC-View-ID: {{insert-your-ac-view-id}}' \
-d '{"query": "query ProductSearch($search: String!) { productSearch( phrase: $search, page_size: 10, current_page: 2) { items { productView { sku name description shortDescription images { url } ... on SimpleProductView { attributes { label name value } price { regular { amount { value currency } } roles } } } } } }", "variables": { "search": "{{your-search-query-string}}"}}' | jq .