How to make a graphql query with parameter

I have a graphql backend, so I want to use rest to query with graphql, I searched the history, but didn’t find any simple instruction for query with parameter.

For example, I just want to query the country:

url: https://countries.trevorblades.com/

query{
  country(code: "BR") {
    name
  }
}

How can I do this?

Hi @connygy, if you paste the example query in the field and click “Copy CURL”, you’ll get the following:

curl 'https://countries.trevorblades.com/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://countries.trevorblades.com' --data-binary '{"query":"query{country(code: \"BR\"){name}}"}' --compressed

Based on this the query object being sent is:
{“query”:“query{country(code: “BR”){name}}”}

To make this work in Composer, go to the Data Configurator, create a new REST API direct integration resource with https://countries.trevorblades.com/ as the base url, and enable the POST method. If you bind the query object with a formula to “Record properties” (HTTP request payload), you’ll get the queried object in return. When in your app, you’ll be able to make these queries using the Create record node.

1 Like

Hi Mari, thanks very much, got it and success.

But I also saw “create record(post) request schema”/“create record(Post) response schema” at data → create record(post) > schema(tab) , Is it possible do something that I can set the input paramenter and response value? For example, I want to set “code” as input parameter, and the return value only the country name?

Hi, you can set the schema to require a “query” field, but beyond that I don’t think you can use the schema builder because the syntax of the “query” is in graphql, not JSON.

The response is in JSON though, so you can define the response path by putting for example data.country.name in the “Response key path” field to just get the name as a response.

Hi Mari,

I only want to show resulted country, so I set the “data.coutry.name” in the “Response key path” as you said, then

  1. set a page variable: “countryName”, set it as text, preview Value as “USA”
  2. connect the button “graphql country” button logic: create record → set page variable
  3. bind the paragraph page variable to “countryName”

How ever, I got a problem at the the output of the create record, because it has only “id” as possible selection. So it is impossible to set the page variable:

How can I do it???

On the Schema tab of the data configurator, choose “Custom schema” for both options to get rid of the ID:

Change the response path to data.country

Run the test again, and choose “Set schema from response”

Now you can access the “name” property in the node outputs. :slight_smile:

1 Like

Hi Mari, thanks so much! it works now! :+1: :smile:

1 Like

Hi @Mari,

If the Graphql endpoint requires authentication in the form of a Header Key and Value, how can we set these for our Graphql requests?

Best Regards,
Edwin

Hi @Edwin_Lee, you can include headers on the Config tab.

1 Like

Great and thanks again Mari.