how to access github graphql API using java
Tag : java , By : Philofax
Date : March 29 2020, 07:55 AM
This might help you Got the program working by changing the above code as below. And it is a good practice to use a JSON library to create complex JSON like above other than manually creating it as most of the time, manual creation of a complex JSON may lead to lot of troubles. import org.json.JSONObject;
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();
}
}
|
how to access the github graphql API from java without running curl commands inside java
Tag : java , By : user137798
Date : March 29 2020, 07:55 AM
To fix the issue you can do The issue is pretty much that you have added an extra "query" word, it should be something like this: (...)
StringEntity entity= new StringEntity("{\"query\":\""+temp+"\"}");
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();
}
}
|
How to write a resolver for a GraphQL subscription using graphql-java and graphql-java-servlet?
Tag : java , By : rhinojosa
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Above stack trace comes when you try to use electron based GraphiQL.app which was not updated since Mar 22, 2018 (v.0.7.2). So, to fix an issue and see subscription working: <!-- Graph(i)QL tool: for development -->
<dependency>
<groupId>com.graphql-java-kickstart</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>5.4.1</version>
</dependency>
graphiql:
endpoint:
subscriptions: /graphql
subscription StockCodeSubscription {
stockQuotes {
dateTime
stockCode
stockPrice
stockPriceChange
}
}
|
Make a POST call to GraphQL API programmatically using Java
Tag : java , By : Kubla Khan
Date : March 29 2020, 07:55 AM
hope this fix your issue Below is a typical graphql endpoint for a java backend There are 2 basic flows here public Map<String, Object> graphqlGET(@RequestParam("query") String query,
@RequestParam(value = "operationName", required = false) String operationName,
@RequestParam("variables") String variablesJson) throws IOException {...
private Map<String, Object> executeGraphqlQuery(String operationName,
String query, Map<String, Object> variables) {
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query(query)
.variables(variables)
.operationName(operationName)
.build();
return graphql.execute(executionInput).toSpecification();
}
val client = HttpClients.createDefault()
val httpPost = HttpPost(url)
val postParameters = ArrayList<NameValuePair>()
postParameters.add(BasicNameValuePair("query", "query as string"))
postParameters.add(BasicNameValuePair("variables", "variables json string"))
httpPost.entity = UrlEncodedFormEntity(postParameters, Charset.defaultCharset())
val response = client.execute(httpPost)
val ret = EntityUtils.toString(response.getEntity())
|
Date : October 01 2020, 06:00 AM
I wish did fix the issue. TLDR; This appears to be a bug. There's no way to bypass the limit applied when fetching a list of resources. Limiting responses like this is a common feature of public APIs -- if the response could include thousands or millions of results, it'll tie up a lot of server resources to fulfill it all at once. Allowing users to make those sort of queries is both costly and a potential security risk. query {
user(login:"armsp") {
repositories {
totalCount
}
repositories {
totalDiskUsage
}
}
}
query {
user(login:"armsp") {
repositories {
totalCount
totalDiskUsage
}
}
}
query {
user(login:"armsp"){
repositories{
totalCount
}
repositories{
nodes{
name
issues(states: OPEN){
totalCount
}
}
}
}
}
|