Introduction
Running examples
Run example code in a language you choose.
Jump to your preferred language
CurlNode.jsPHPRubyJava
Overview
The documentation provides example code in popular programming languages. The examples give you a quick way to see how working with the GraphQL API will be in actual code.
Instructions below will help you setup and run a basic GraphQL query in the programming language you choose. Note that the instructions assume that you already have the language support installed to run each language.
Variables
The examples have two variables that you will need to change before running the code: subdomain, apikey. Set the subdomain variable to the subdomain of the Kuali instance you are working with. Set the apikey variable to an API key you generated in your account. See the Authentication article for information on generating and using an API key.
GraphQL
This is the GraphQL query used in the instructions below. It queries for a list of apps.
Query
query {
apps {
id
name
}
}
Results
{
"data": {
"apps": [
{
"id": "5d5f337395a039001e48a649",
"name": "Incomplete Grade Request"
},
{
"id": "5ac835643d7f3600196b2749",
"name": "Parking Tag Application"
},
{
"id": "5e6715e208f295001f8219b6",
"name": "Travel Disclosure"
},
]
}
}
Curl
Query
Run the following command in your terminal. Make sure to replace the subdomain and apikey.
curl 'https://subdomain.kualibuild.com/app/api/v0/graphql' -H 'Content-Type: application/json' -H 'Authorization: Bearer apikey' --data-binary '{"query":"query { apps { id name }}","variables":{}}' --compressed
Results
The results will look something like this.
{"data":{"apps":[{"id":"5d5f337395a039001e48a649","name":"Incomplete Grade Request"},{"id":"5ac835643d7f3600196b2749","name":"Parking Tag Application"},{"id":"5e6715e208f295001f8219b6","name":"Travel Disclosure"}]}}
Node.js
Step 1
Create a new folder and run this command.
npm init es6 --yes
Step 2
Install the node-fetch package.
npm install node-fetch
Step 3
Create an index.js file with the following code. Make sure you replace the subdomain and apikey variables.
import fetch from 'node-fetch'
const subdomain = 'subdomain'
const apikey = 'apikey'
const query = `query {
apps {
id
name
}
}`
const variables = {}
fetch(`https://${subdomain}.kualibuild.com/app/api/v0/graphql`, {
method:'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apikey}`
},
body: JSON.stringify({query, variables})
})
.then(res => res.text())
.then(data => console.log(data))
.catch(err => console.log(err))
Step 4
Run the code with this command.
node index.js
Results
The response will look something like this.
{"data":{"apps":[{"id":"5d5f337395a039001e48a649","name":"Incomplete Grade Request"},{"id":"5ac835643d7f3600196b2749","name":"Parking Tag Application"},{"id":"5e6715e208f295001f8219b6","name":"Travel Disclosure"}]}}
PHP
Step 1
Create a file named index.php with the following code. Make sure to replace the subdomain and apikey variables.
<?php
$subdomain = "subdomain";
$apikey = "apikey";
$url = "https://{$subdomain}.kualibuild.com/app/api/v0/graphql";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer {$apikey}",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data= '{"query":"query { apps { id name }}","variables":{}}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
Step 2
Run the code with this command.
php index.php
Results
The response will look something like this.
string(216) "{"data":{"apps":[{"id":"5d5f337395a039001e48a649","name":"Incomplete Grade Request"},{"id":"5ac835643d7f3600196b2749","name":"Parking Tag Application"},{"id":"5e6715e208f295001f8219b6","name":"Travel Disclosure"}]}}"
Ruby
Step 1
Create a file named index.rb with the following code. Make sure to replace the subdomain and apikey variables.
require 'net/http'
require 'uri'
require 'json'
subdomain = "subdomain"
apikey = "apikey"
uri = URI.parse("https://#{subdomain}.kualibuild.com/app/api/v0/graphql")
request = Net::HTTP::Post.new(uri)
request.content_type = "application/json"
request["Authorization"] = "Bearer #{apikey}"
request.body = JSON.dump({
"query" => "query {
apps {
id
name
}
}",
"variables" => {}
})
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
puts response.code
puts response.body
Step 2
Run the code with this command.
ruby index.rb
Results
The response will look something like this.
200 {"data":{"apps":[{"id":"5d5f337395a039001e48a649","name":"Incomplete Grade Request"},{"id":"5ac835643d7f3600196b2749","name":"Parking Tag Application"},{"id":"5e6715e208f295001f8219b6","name":"Travel Disclosure"}]}}
Java
Step 1
Create a file named Main.java with the following code. Make sure to replace the subdomain and apikey variables.
import java.io.*;
import java.util.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
public class exampleCreate {
public static void main(String[] args) throws IOException {
String subdomain = "subdomain";
String apikey = "apikey";
URL url = new URL("https://" + subdomain + ".kualibuild.com/app/api/v0/graphql");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setRequestProperty("Content-Type", "application/json");
http.setRequestProperty("Origin", "https://" + subdomain + ".kualibuild.com");
http.setRequestProperty("Authorization", "Bearer " + apikey);
String data = "{\"query\":\"query { apps { id name }}\",\"variables\":{}}";
byte[] out = data.getBytes(Charset.forName("UTF-8"));
OutputStream stream = http.getOutputStream();
stream.write(out);
System.out.println(http.getResponseCode() + " " + http.getResponseMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print in String
System.out.println(response.toString());
http.disconnect();
}
}
Step 2
Run the code with this command.
java Main.java
Results
The response will look something like this.
200 OK {"data":{"apps":[{"id":"5d5f337395a039001e48a649","name":"Incomplete Grade Request"},{"id":"5ac835643d7f3600196b2749","name":"Parking Tag Application"},{"id":"5e6715e208f295001f8219b6","name":"Travel Disclosure"}]}}