Examples GraphQL Requests
February 14, 2023 at 10:38 AMLooks the following examples to know how to send a GraphQL requests using different programming languages with a query using variables.
For example using the following query and variables:
query ($limit: Int!, $page: Int!) {
products {
all(first: $limit, page: $page) {
totalCount
pages
pageInfo {
page
hasNextPage
hasPreviousPage
}
edges {
node {
id
name
price
}
}
}
}
}
{
"limit": 10,
"page": 1
}
{
"data": {
"products": {
"all": {
"totalCount": 3723,
"pages": 373,
"pageInfo": {
"page": 1,
"hasNextPage": true,
"hasPreviousPage": false
},
"edges": [
{
"node": {
"id": "amkxMW31230QwPQ==",
"name": "T-Mobile $30",
"price": 30
}
},
{
"node": {
"id": "amkx2309a221wPQ==",
"name": "T-Mobile $100",
"price": 100
}
},
{
"node": {
"id": "amkx2414d87ask3=",
"name": "H2O Bolt 4G LTE $25.00",
"price": 25
}
},
{
"node": {
"id": "TRkODdhc2szPQ==",
"name": "H2O Bolt 4G LTE $50.00",
"price": 50
}
},
{
"node": {
"id": "415adhc2szPQ==",
"name": "H2O Bolt 4G LTE $70.00",
"price": 70
}
},
{
"node": {
"id": "JrT0RkaGMyc3pQUT09",
"name": "EasyGo Unlimited 100MB @ 4G $20.00",
"price": 20
}
},
{
"node": {
"id": "hR015YzNwUVVUMDk=",
"name": "H2O $20.00 RTR - Unlimited plans",
"price": 20
}
},
{
"node": {
"id": "wMTVZek53VVZWVU1Eaz0=",
"name": "H2O $30.00 3GB @ 4G LTE RTR",
"price": 30
}
},
{
"node": {
"id": "azUzVlZaV1ZVMUVhejA9=",
"name": "H2O $35.00 RTR",
"price": 35
}
},
{
"node": {
"id": "ZrWktjbFF3VWzVlZaV1=",
"name": "H2O $40.00 8GB @ 4G LTE RTR",
"price": 40
}
}
]
}
}
}
}
The following examples assume that you already know how to generate the authentication token. Ensure change the Bearer token for a valid one before execute any of the following examples as is.
CURL
curl --location --request POST 'https://api.paynup.com' \
--header 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJDOTRCQkRBRTA1Qzc1MjM0QjgwRiIsImp0aSI6IjZlMmEzNGQzLTFmYTItNGU3Ni04ODVkLTZmY2Y5M2Q1ZDU1NCIsImlhdCI6MTYwODEzNjUzOCwiZXhwIjoxNjA4Mzk1NzM4fQ.n9f1HUXrYWTQIB6nnHQb3egZSzRQ9id73IcOe9ejNeU' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query ($limit: Int!, $page: Int!) {\n products {\n all(first: $limit, page: $page) {\n totalCount\n pages\n pageInfo {\n page\n hasNextPage\n hasPreviousPage\n }\n edges {\n node {\n id\n name\n price\n }\n }\n }\n }\n}","variables":{"limit":10,"page":1}}'
PHP
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paynup.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\"query\":\"query (\$limit: Int!, \$page: Int!) {\\n products {\\n all(first: \$limit, page: \$page) {\\n totalCount\\n pages\\n pageInfo {\\n page\\n hasNextPage\\n hasPreviousPage\\n }\\n edges {\\n node {\\n id\\n name\\n price\\n }\\n }\\n }\\n }\\n}\",\"variables\":{\"limit\":10,\"page\":1}}",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJDOTRCQkRBRTA1Qzc1MjM0QjgwRiIsImp0aSI6IjZlMmEzNGQzLTFmYTItNGU3Ni04ODVkLTZmY2Y5M2Q1ZDU1NCIsImlhdCI6MTYwODEzNjUzOCwiZXhwIjoxNjA4Mzk1NzM4fQ.n9f1HUXrYWTQIB6nnHQb3egZSzRQ9id73IcOe9ejNeU",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Java
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"query\":\"query ($limit: Int!, $page: Int!) {\\n products {\\n all(first: $limit, page: $page) {\\n totalCount\\n pages\\n pageInfo {\\n page\\n hasNextPage\\n hasPreviousPage\\n }\\n edges {\\n node {\\n id\\n name\\n price\\n }\\n }\\n }\\n }\\n}\",\"variables\":{\"limit\":10,\"page\":1}}");
Request request = new Request.Builder()
.url("https://api.paynup.com")
.method("POST", body)
.addHeader("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJDOTRCQkRBRTA1Qzc1MjM0QjgwRiIsImp0aSI6IjZlMmEzNGQzLTFmYTItNGU3Ni04ODVkLTZmY2Y5M2Q1ZDU1NCIsImlhdCI6MTYwODEzNjUzOCwiZXhwIjoxNjA4Mzk1NzM4fQ.n9f1HUXrYWTQIB6nnHQb3egZSzRQ9id73IcOe9ejNeU")
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
Javascript
var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJDOTRCQkRBRTA1Qzc1MjM0QjgwRiIsImp0aSI6IjZlMmEzNGQzLTFmYTItNGU3Ni04ODVkLTZmY2Y5M2Q1ZDU1NCIsImlhdCI6MTYwODEzNjUzOCwiZXhwIjoxNjA4Mzk1NzM4fQ.n9f1HUXrYWTQIB6nnHQb3egZSzRQ9id73IcOe9ejNeU");
myHeaders.append("Content-Type", "application/json");
var graphql = JSON.stringify({
query: "query ($limit: Int!, $page: Int!) {\n products {\n all(first: $limit, page: $page) {\n totalCount\n pages\n pageInfo {\n page\n hasNextPage\n hasPreviousPage\n }\n edges {\n node {\n id\n name\n price\n }\n }\n }\n }\n}",
variables: {"limit":10,"page":1}
})
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: graphql,
redirect: 'follow'
};
fetch("https://api.paynup.com", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Node
var request = require('request');
var options = {
'method': 'POST',
'url': 'https://api.paynup.com',
'headers': {
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJDOTRCQkRBRTA1Qzc1MjM0QjgwRiIsImp0aSI6IjZlMmEzNGQzLTFmYTItNGU3Ni04ODVkLTZmY2Y5M2Q1ZDU1NCIsImlhdCI6MTYwODEzNjUzOCwiZXhwIjoxNjA4Mzk1NzM4fQ.n9f1HUXrYWTQIB6nnHQb3egZSzRQ9id73IcOe9ejNeU',
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: 'query ($limit: Int!, $page: Int!) {\n products {\n all(first: $limit, page: $page) {\n totalCount\n pages\n pageInfo {\n page\n hasNextPage\n hasPreviousPage\n }\n edges {\n node {\n id\n name\n price\n }\n }\n }\n }\n}',
variables: {"limit":10,"page":1}
})
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
Python
import http.client
import mimetypes
conn = http.client.HTTPSConnection("api.paynup.com")
payload = "{\"query\":\"query ($limit: Int!, $page: Int!) {\\n products {\\n all(first: $limit, page: $page) {\\n totalCount\\n pages\\n pageInfo {\\n page\\n hasNextPage\\n hasPreviousPage\\n }\\n edges {\\n node {\\n id\\n name\\n price\\n }\\n }\\n }\\n }\\n}\",\"variables\":{\"limit\":10,\"page\":1}}"
headers = {
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJDOTRCQkRBRTA1Qzc1MjM0QjgwRiIsImp0aSI6IjZlMmEzNGQzLTFmYTItNGU3Ni04ODVkLTZmY2Y5M2Q1ZDU1NCIsImlhdCI6MTYwODEzNjUzOCwiZXhwIjoxNjA4Mzk1NzM4fQ.n9f1HUXrYWTQIB6nnHQb3egZSzRQ9id73IcOe9ejNeU',
'Content-Type': 'application/json'
}
conn.request("POST", "", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Ruby
require "uri"
require "net/http"
url = URI("https://api.paynup.com")
http = Net::HTTP.new(url.host, url.port);
request = Net::HTTP::Post.new(url)
request["Authorization"] = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJDOTRCQkRBRTA1Qzc1MjM0QjgwRiIsImp0aSI6IjZlMmEzNGQzLTFmYTItNGU3Ni04ODVkLTZmY2Y5M2Q1ZDU1NCIsImlhdCI6MTYwODEzNjUzOCwiZXhwIjoxNjA4Mzk1NzM4fQ.n9f1HUXrYWTQIB6nnHQb3egZSzRQ9id73IcOe9ejNeU"
request["Content-Type"] = "application/json"
request.body = "{\"query\":\"query ($limit: Int!, $page: Int!) {\\n products {\\n all(first: $limit, page: $page) {\\n totalCount\\n pages\\n pageInfo {\\n page\\n hasNextPage\\n hasPreviousPage\\n }\\n edges {\\n node {\\n id\\n name\\n price\\n }\\n }\\n }\\n }\\n}\",\"variables\":{\"limit\":10,\"page\":1}}"
response = http.request(request)
puts response.read_body
Swift
import Foundation
var semaphore = DispatchSemaphore (value: 0)
let parameters = "{\"query\":\"query ($limit: Int!, $page: Int!) {\\n products {\\n all(first: $limit, page: $page) {\\n totalCount\\n pages\\n pageInfo {\\n page\\n hasNextPage\\n hasPreviousPage\\n }\\n edges {\\n node {\\n id\\n name\\n price\\n }\\n }\\n }\\n }\\n}\",\"variables\":{\"limit\":10,\"page\":1}}"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.paynup.com")!,timeoutInterval: Double.infinity)
request.addValue("Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJDOTRCQkRBRTA1Qzc1MjM0QjgwRiIsImp0aSI6IjZlMmEzNGQzLTFmYTItNGU3Ni04ODVkLTZmY2Y5M2Q1ZDU1NCIsImlhdCI6MTYwODEzNjUzOCwiZXhwIjoxNjA4Mzk1NzM4fQ.n9f1HUXrYWTQIB6nnHQb3egZSzRQ9id73IcOe9ejNeU", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()
C#
var client = new RestClient("https://api.paynup.com");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJDOTRCQkRBRTA1Qzc1MjM0QjgwRiIsImp0aSI6IjZlMmEzNGQzLTFmYTItNGU3Ni04ODVkLTZmY2Y5M2Q1ZDU1NCIsImlhdCI6MTYwODEzNjUzOCwiZXhwIjoxNjA4Mzk1NzM4fQ.n9f1HUXrYWTQIB6nnHQb3egZSzRQ9id73IcOe9ejNeU");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\"query\":\"query ($limit: Int!, $page: Int!) {\\n products {\\n all(first: $limit, page: $page) {\\n totalCount\\n pages\\n pageInfo {\\n page\\n hasNextPage\\n hasPreviousPage\\n }\\n edges {\\n node {\\n id\\n name\\n price\\n }\\n }\\n }\\n }\\n}\",\"variables\":{\"limit\":10,\"page\":1}}",
ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);