Created
October 11, 2023 09:10
-
-
Save iKunalChhabra/08ea55128cadd9bb792cee4e18315ad9 to your computer and use it in GitHub Desktop.
GraphQL Client in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
// <dependency> | |
// <groupId>org.apache.httpcomponents</groupId> | |
// <artifactId>httpclient</artifactId> | |
// <version>4.5.14</version> | |
// </dependency> | |
// <dependency> | |
// <groupId>org.json</groupId> | |
// <artifactId>json</artifactId> | |
// <version>20230618</version> | |
// </dependency> | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpHeaders; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.auth.AuthScope; | |
import org.apache.http.auth.UsernamePasswordCredentials; | |
import org.apache.http.client.CredentialsProvider; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.impl.client.BasicCredentialsProvider; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.util.EntityUtils; | |
import org.json.JSONObject; | |
public class GraphQLClient { | |
private final String endpoint; | |
private final String username; | |
private final String password; | |
public GraphQLClient(String endpoint, String username, String password) { | |
this.endpoint = endpoint; | |
this.username = username; | |
this.password = password; | |
} | |
private CloseableHttpClient getHTTPClient() { | |
return HttpClients.custom() | |
.setDefaultCredentialsProvider(getBasicAuth(username, password)) | |
.build(); | |
} | |
private HttpPost createHTTPRequest() { | |
HttpPost request = new HttpPost(endpoint); | |
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); | |
return request; | |
} | |
private String prepareQuery(String query) { | |
return new JSONObject() | |
.put("query", query) | |
.toString(); | |
} | |
private void validateHTTPResponse(HttpResponse response) { | |
if (response.getStatusLine().getStatusCode() != 200) { | |
throw new RuntimeException("Failed : HTTP error code : " + | |
response.getStatusLine().getStatusCode() + | |
" " + response.getStatusLine().getReasonPhrase()); | |
} | |
} | |
private void validateJSONResponse(JSONObject response) { | |
if (response.has("errors")) { | |
throw new RuntimeException("Failed : GraphQL error : " + | |
response.getJSONArray("errors").toString()); | |
} | |
} | |
private static CredentialsProvider getBasicAuth(String username, String password) { | |
CredentialsProvider provider = new BasicCredentialsProvider(); | |
provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); | |
return provider; | |
} | |
public JSONObject query(String query) throws Exception { | |
// prepare | |
CloseableHttpClient client = getHTTPClient(); | |
HttpPost request = createHTTPRequest(); | |
request.setEntity(new StringEntity(prepareQuery(query), "UTF-8")); | |
// execute | |
HttpResponse response = client.execute(request); | |
// validate | |
validateHTTPResponse(response); | |
// get response | |
HttpEntity entity = response.getEntity(); | |
// prepare JSON response | |
JSONObject jsonResponse = new JSONObject(EntityUtils.toString(entity)); | |
validateJSONResponse(jsonResponse); | |
// close client | |
client.close(); | |
return jsonResponse; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment