Sending data (GraphQL) to REST asset request body


(Mitch Kerry) #1

Hi

Never worked this out…how can I send data to a REST asset that is then sent in the request body?
Specifically, I want to send a GraphQL request to a service, but I want the request to be dynamic based on a choice on page.

The GraphQL is in a format:

query DynamicAssetPreviewUrl {
asset: node(id: “ID_that_is_dynamic”) {
… on Image {
previewUrl
title
}
}
}

Appreciate help on this, or more generally how to pass to a request body.

Thanks


(Iain Simmons) #2

Hi @mitchk,

I might be misunderstanding, but I think GraphQL is always using POST requests, and then your GraphQL query goes in the Request Body part of the REST asset.

If you want something dynamic in that Request Body, you would use %globals_ keywords. They could be something like %globals_asset_metadata_someField%, or potentially %globals_get_someParam% that you use when nesting the REST asset or sending a request to it. e.g. https://path.to/rest-asset?someParam=foo would put foo where you put %globals_get_someParam%.

There should be a checkbox in the REST asset to ‘pre-process global keywords’ or similar, that will need to be checked.

Hope that helps!

Cheers,
iain


(Mitch Kerry) #3

Thanks @isimmons, I got it to work.

The main problem was the formatting of the GraphQL query in the response body.

This is how I ended up doing it…if of interest this is for returning icons from Frontify:

{“query”:"\n query LibraryAssets {\n library(id: “LIBRARY_ID”) {\n id \n name \n type: __typename\n assets(page: %globals_get_page%, limit: 100) {\n total\n items {\n id\n title \n tags {\n value \n }\n … on Image {\n previewUrl\n }\n }\n }\n }\n }\n",“variables”:{}}

I’m then passing the ‘page’ value from the main page using with_get.
I’m then using an Asset listing using a JSON Data Source with the REST asset as the source.

Cheers