How to access github graphql API from java without running curl commands inside java

Excuse me for the long question, as I am new to graphql . I need to access the github graphql API to get the fault details in a specific file, since there is still no REST API fault available in the github API version 3 . I can get output for below graphql query that works in here

  query { repository(owner: "wso2-extensions", name: "identity-inbound-auth-oauth") { object(expression: "83253ce50f189db30c54f13afa5d99021e2d7ece") { ... on Commit { blame(path: "components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java") { ranges { startingLine endingLine age commit { message url history(first: 2) { edges { node { message url } } } author { name email } } } } } } } } 

from running the following curl command in terminal

 curl -i -H "Authorization: bearer myGitHubToken" -X POST -d '{"query": "query { repository(owner: \"wso2-extensions\", name: \"identity-inbound-auth-oauth\") { object(expression:\"83253ce50f189db30c54f13afa5d99021e2d7ece\") { ... on Commit { blame(path: \"components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java\") { ranges { startingLine endingLine age commit { message url history(first: 2) { edges { node { message url } } } author { name email } } } } } } } }"}' https://api.github.com/graphql 

and run the same curl command inside Java as shown below

 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Demo { public static void main(String[] args) { String url="https://api.github.com/graphql"; String[] command = {"curl", "-H" ,"Authorization: Bearer myGitHubToken","-H","Accept:application/json","-X", "POST", "-d", "{\"query\": \"query { repository(owner: \\\"wso2-extensions\\\", name: \\\"identity-inbound-auth-oauth\\\") { object(expression:\\\"83253ce50f189db30c54f13afa5d99021e2d7ece\\\") { ... on Commit { blame(path: \\\"components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java\\\") { ranges { startingLine endingLine age commit { message url history(first: 2) { edges { node { message url } } } author { name email } } } } } } } }\"}" , url}; ProcessBuilder process = new ProcessBuilder(command); Process p; try { p = process.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); StringBuilder builder = new StringBuilder(); String line = null; while ( (line = reader.readLine()) != null) { builder.append(line); builder.append(System.getProperty("line.separator")); } String result = builder.toString(); System.out.print(result); } catch (IOException e) { System.out.print("error"); e.printStackTrace(); } } } 

is there any other way so that I can get the same result in java without having to execute curl commands since running curl commands inside java not good practice (according to my opinion). thanks in advance

Update using httpClient code

here is the code i tried using apache httpClient

 public void callingGraph(){ CloseableHttpClient client= null; CloseableHttpResponse response= null; client= HttpClients.createDefault(); HttpPost httpPost= new HttpPost("https://api.github.com/graphql"); httpPost.addHeader("Authorization","Bearer myToken"); httpPost.addHeader("Accept","application/json"); String temp="{repository(owner: \"wso2-extensions\", name: \"identity-inbound-auth-oauth\") { object(expression: \"83253ce50f189db30c54f13afa5d99021e2d7ece\") { ... on Commit { blame(path: \"components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java\") { ranges { startingLine, endingLine, age, commit { message url history(first: 2) { edges { node { message, url } } } author { name, email } } } } } } } }"; // String temp="{repository(owner:\"wso2\",name:\"product-is\"){description}}"; try { StringEntity entity= new StringEntity("{\"query\":\"query "+temp+"\"}"); httpPost.setEntity(entity); response= client.execute(httpPost); } catch(UnsupportedEncodingException e){ e.printStackTrace(); } catch(ClientProtocolException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } try{ BufferedReader reader= new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line= null; StringBuilder builder= new StringBuilder(); while((line=reader.readLine())!= null){ builder.append(line); } System.out.println(builder.toString()); } catch(Exception e){ e.printStackTrace(); } } 

but it even gives me a small request {repository(owner:\"wso2\",name:\"product-is\"){description}}

{"message": "Problems with the JSON parser", "documentation_url": " https://developer.github.com/v3 "}

but when such a request is simple String temp="{viewer {email login }}"; , it works. What is wrong with my code. Please, help

+2
java github graphql
source share
3 answers

The problem is that you added the additional word "query", it should be something like this:

 (...) StringEntity entity= new StringEntity("{\"query\":\""+temp+"\"}"); 

Although I should remind you that you should avoid trying hard coding json as much as possible, so the ideal script should use the JSON library, resulting in something like this (full code):

 import org.json.JSONObject; // New import public void callingGraph(){ CloseableHttpClient client= null; CloseableHttpResponse response= null; client= HttpClients.createDefault(); HttpPost httpPost= new HttpPost("https://api.github.com/graphql"); httpPost.addHeader("Authorization","Bearer myToken"); httpPost.addHeader("Accept","application/json"); JSONObject jsonobj = new JSONObject(); jsonobj.put("query", "{repository(owner: \"wso2-extensions\", name: \"identity-inbound-auth-oauth\") { object(expression: \"83253ce50f189db30c54f13afa5d99021e2d7ece\") { ... on Commit { blame(path: \"components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java\") { ranges { startingLine, endingLine, age, commit { message url history(first: 2) { edges { node { message, url } } } author { name, email } } } } } } } }"); try { StringEntity entity= new StringEntity(jsonobj.toString()); httpPost.setEntity(entity); response= client.execute(httpPost); } catch(UnsupportedEncodingException e){ e.printStackTrace(); } catch(ClientProtocolException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } try{ BufferedReader reader= new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line= null; StringBuilder builder= new StringBuilder(); while((line=reader.readLine())!= null){ builder.append(line); } System.out.println(builder.toString()); } catch(Exception e){ e.printStackTrace(); } } 

Note how escaped double quotes are only those that java can understand as a single line.

+2
source share
Answer to

@AdrianoMartins is correct, but I was able to get my program to work just by changing the line

 String temp="{repository(owner: \"wso2-extensions\", name: \"identity-inbound-auth-oauth\") { object(expression: \"83253ce50f189db30c54f13afa5d99021e2d7ece\") { ... on Commit { blame(path: \"components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java\") { ranges { startingLine, endingLine, age, commit { message url history(first: 2) { edges { node { message, url } } } author { name, email } } } } } } } }"; 

to

 String temp="{repository(owner: \\\"wso2-extensions\\\", name: \\\"identity-inbound-auth-oauth\\\") { object(expression: \\\"83253ce50f189db30c54f13afa5d99021e2d7ece\\\") { ... on Commit { blame(path: \\\"components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java\\\") { ranges { startingLine, endingLine, age, commit { message url history(first: 2) { edges { node { message, url } } } author { name, email } } } } } } } }"; 

Thus, the overall program will look like

  public void callingGraph(){ CloseableHttpClient client= null; CloseableHttpResponse response= null; client= HttpClients.createDefault(); HttpPost httpPost= new HttpPost("https://api.github.com/graphql"); httpPost.addHeader("Authorization","Bearer myToken"); httpPost.addHeader("Accept","application/json"); String temp="{repository(owner: \\\"wso2-extensions\\\", name: \\\"identity-inbound-auth-oauth\\\") { object(expression: \\\"83253ce50f189db30c54f13afa5d99021e2d7ece\\\") { ... on Commit { blame(path: \\\"components/org.wso2.carbon.identity.oauth.endpoint/src/main/java/org/wso2/carbon/identity/oauth/endpoint/authz/OAuth2AuthzEndpoint.java\\\") { ranges { startingLine, endingLine, age, commit { message url history(first: 2) { edges { node { message, url } } } author { name, email } } } } } } } }"; // String temp="{repository(owner:\"wso2\",name:\"product-is\"){description}}"; try { StringEntity entity= new StringEntity("{\"query\":\"query "+temp+"\"}"); httpPost.setEntity(entity); response= client.execute(httpPost); } catch(UnsupportedEncodingException e){ e.printStackTrace(); } catch(ClientProtocolException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } try{ BufferedReader reader= new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line= null; StringBuilder builder= new StringBuilder(); while((line=reader.readLine())!= null){ builder.append(line); } System.out.println(builder.toString()); } catch(Exception e){ e.printStackTrace(); } } 

as Adriano Martins also suggested that it is better to use the JSON library than hardcoding a JSON

+1
source share
0
source share

All Articles