How to convert Graphql query into Rest API query/curl command query

Summary:

Harness facilitates users to execute GraphQl queries via provided API Explorer user interface, and this GraphQl query gets converted into a REST API call.
Therefore, this article shows how a GraphQl query from UI can build into a REST API curl command. Please follow the steps below example.

Example:

Here is an example of a Graphql using which the deployment status can be fetched.

GraphQl query example via Harness API explorer:

Step 1: Extract the network payload via browser’s inspection (here shown using Google chrome’s inspect window )
1.1 Before Running the query from the API explorer UI, open the inspect window and navigate to its
Network tab.

1.2 Now run the Graphql query from the UI; in the inspect window, an entry for the graphql query will
be listed.

1.3. In the inspect window’s Network tab, navigate to payload → Request payload → view parsed and copy the query as shown below.

Step 2: Once the payload query is extracted, the same can be used for curl commands.
cURL command syntax:

curl --location --request POST 'https://app.harness.io/gateway/api/graphql?accountId=<YOUR_ACCOUNT_ID>' \
    --header 'x-api-key: <API_KEY>' \
    --header 'Content-Type: application/json' \
    --data-raw '< EXTRACTED PAYLOAD QUERY >'

<YOUR_ACCOUNT_ID> - Your Harness account ID
<API_KEY> - API key created in your account with appropriate privileges.
< EXTRACTED PAYLOAD QUERY > - payload query extracted in the previous step.

Example curl command:

curl --location --request POST 'https://app.harness.io/gateway/api/graphql?accountId=abfud234njf' \
    --header 'x-api-key: dfklfygsfbviyeqwuifgu3u5r32o9fbd' \
    --header 'Content-Type: application/json' \
    --data-raw '{"query":"query{\n    execution(executionId: \"fsdgwegwecBa8U8-oVQ\") {\n      id\n      status\n      createdAt\n    }\n}","variables":null}'

For more details about Graphql, please refer to doc: ​Introduction to Harness GraphQL API - Harness.io Docs

4 Likes