Skip to content

Commit

Permalink
feat: Add graphQL API support
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi347 committed Mar 19, 2024
1 parent 27719e8 commit 039457d
Show file tree
Hide file tree
Showing 7 changed files with 62,393 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ dependency-reduced-pom.xml
# mvn release
pom.xml.releaseBackup
release.properties

## macOS
.DS_Store
46 changes: 46 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@
<opencensus.version>0.31.1</opencensus.version>
<okhttp.version>4.11.0</okhttp.version>

<!-- BEGIN: plugin versions -->
<graphql.codegen.plugin.version>5.8.0</graphql.codegen.plugin.version>
<!-- END: plugin versions -->

<shade.id>${project.groupId}.githubclient.shade</shade.id>
</properties>

Expand Down Expand Up @@ -252,6 +256,16 @@
<version>2.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>io.github.kobylynskyi</groupId>
<artifactId>graphql-java-codegen</artifactId>
<version>${graphql.codegen.plugin.version}</version>
</dependency>
</dependencies>

<profiles>
Expand Down Expand Up @@ -499,6 +513,38 @@
<doclint>none</doclint>
</configuration>
</plugin>
<plugin>
<groupId>io.github.kobylynskyi</groupId>
<artifactId>graphql-codegen-maven-plugin</artifactId>
<version>5.10.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<!-- all config options:
https://github.com/kobylynskyi/graphql-java-codegen/blob/main/docs/codegen-options.md
-->
<graphqlSchemas>
<rootDir>${project.basedir}/src/main/resources/schema</rootDir>
<includePattern>.*\.graphqls?</includePattern>
</graphqlSchemas>
<outputDir>${project.build.directory}/generated-sources</outputDir>
<packageName>com.spotify.github.graphql</packageName>
<modelPackageName>com.spotify.github.graphql.models</modelPackageName>
<generateModelsForRootTypes>true</generateModelsForRootTypes>
<generateClient>true</generateClient>
<generateApis>false</generateApis>
<modelValidationAnnotation>@javax.annotation.Nonnull</modelValidationAnnotation>
<customTypesMapping>
<DateTime>java.util.Date</DateTime>
<Price.amount>java.math.BigDecimal</Price.amount>
</customTypesMapping>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
Expand Down
50 changes: 27 additions & 23 deletions src/main/java/com/spotify/github/v3/clients/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import static okhttp3.MediaType.parse;

import com.fasterxml.jackson.core.type.TypeReference;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequest;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLResponseProjection;
import com.spotify.github.Tracer;
import com.spotify.github.jackson.Json;
import com.spotify.github.v3.Team;
Expand All @@ -41,9 +44,8 @@
import com.spotify.github.v3.repos.CommitItem;
import com.spotify.github.v3.repos.FolderContent;
import com.spotify.github.v3.repos.Repository;
import com.spotify.github.v3.repos.Status;
import com.spotify.github.v3.repos.RepositoryInvitation;

import com.spotify.github.v3.repos.Status;
import java.io.*;
import java.lang.invoke.MethodHandles;
import java.net.URI;
Expand All @@ -58,7 +60,6 @@
import java.util.function.Consumer;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;

import okhttp3.*;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -447,7 +448,7 @@ Json json() {
*/
CompletableFuture<Response> request(final String path) {
final Request request = requestBuilder(path).build();
log.debug("Making request to {}", request.url().toString());
log.debug("Making request to {}", request.url());

Check warning on line 451 in src/main/java/com/spotify/github/v3/clients/GitHubClient.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/spotify/github/v3/clients/GitHubClient.java#L451

Added line #L451 was not covered by tests
return call(request);
}

Expand All @@ -462,7 +463,7 @@ CompletableFuture<Response> request(final String path, final Map<String, String>
final Request.Builder builder = requestBuilder(path);
extraHeaders.forEach(builder::addHeader);
final Request request = builder.build();
log.debug("Making request to {}", request.url().toString());
log.debug("Making request to {}", request.url());
return call(request);
}

Expand All @@ -474,7 +475,7 @@ CompletableFuture<Response> request(final String path, final Map<String, String>
*/
<T> CompletableFuture<T> request(final String path, final Class<T> clazz) {
final Request request = requestBuilder(path).build();
log.debug("Making request to {}", request.url().toString());
log.debug("Making request to {}", request.url());
return call(request)
.thenApply(body -> json().fromJsonUncheckedNotNull(responseBodyUnchecked(body), clazz));
}
Expand All @@ -491,7 +492,7 @@ <T> CompletableFuture<T> request(
final Request.Builder builder = requestBuilder(path);
extraHeaders.forEach(builder::addHeader);
final Request request = builder.build();
log.debug("Making request to {}", request.url().toString());
log.debug("Making request to {}", request.url());
return call(request)
.thenApply(body -> json().fromJsonUncheckedNotNull(responseBodyUnchecked(body), clazz));
}
Expand All @@ -510,7 +511,7 @@ <T> CompletableFuture<T> request(
final Request.Builder builder = requestBuilder(path);
extraHeaders.forEach(builder::addHeader);
final Request request = builder.build();
log.debug("Making request to {}", request.url().toString());
log.debug("Making request to {}", request.url());
return call(request)
.thenApply(
response ->
Expand All @@ -525,7 +526,7 @@ <T> CompletableFuture<T> request(
*/
<T> CompletableFuture<T> request(final String path, final TypeReference<T> typeReference) {
final Request request = requestBuilder(path).build();
log.debug("Making request to {}", request.url().toString());
log.debug("Making request to {}", request.url());
return call(request)
.thenApply(
response ->
Expand All @@ -544,7 +545,7 @@ CompletableFuture<Response> post(final String path, final String data) {
requestBuilder(path)
.method("POST", RequestBody.create(parse(MediaType.APPLICATION_JSON), data))
.build();
log.debug("Making POST request to {}", request.url().toString());
log.debug("Making POST request to {}", request.url());
return call(request);
}

Expand All @@ -563,7 +564,7 @@ CompletableFuture<Response> post(
.method("POST", RequestBody.create(parse(MediaType.APPLICATION_JSON), data));
extraHeaders.forEach(builder::addHeader);
final Request request = builder.build();
log.debug("Making POST request to {}", request.url().toString());
log.debug("Making POST request to {}", request.url());

Check warning on line 567 in src/main/java/com/spotify/github/v3/clients/GitHubClient.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/spotify/github/v3/clients/GitHubClient.java#L567

Added line #L567 was not covered by tests
return call(request);
}

Expand Down Expand Up @@ -603,18 +604,21 @@ <T> CompletableFuture<T> post(final String path, final String data, final Class<
/**
* Make a POST request to the graphql endpoint of Github
*
* @param data request body as stringified JSON
* @param queryRequest GraphQLOperationRequest object with query or mutation request
* @param responseProjection Select what fields are required in the response
* @return response
*
* @see "https://docs.github.com/en/enterprise-server@3.9/graphql/guides/forming-calls-with-graphql#communicating-with-graphql"
*/
public CompletableFuture<Response> postGraphql(final String data) {
public CompletableFuture<Response> queryGraphQL(final GraphQLOperationRequest queryRequest, final GraphQLResponseProjection responseProjection) {
GraphQLRequest graphqlRequest = new GraphQLRequest(queryRequest, responseProjection);
String body = graphqlRequest.toQueryString();
final Request request =
graphqlRequestBuilder()
.method("POST", RequestBody.create(parse(MediaType.APPLICATION_JSON), data))
.build();
log.info("Making POST request to {}", request.url());
return call(request);
graphqlRequestBuilder()
.method("POST", RequestBody.create(parse(MediaType.APPLICATION_JSON), body))
.build();
log.info("Making GraphQL Query POST request to {}, with body {}", request.url(), body);
return this.call(request);
}

/**
Expand All @@ -629,7 +633,7 @@ CompletableFuture<Response> put(final String path, final String data) {
requestBuilder(path)
.method("PUT", RequestBody.create(parse(MediaType.APPLICATION_JSON), data))
.build();
log.debug("Making POST request to {}", request.url().toString());
log.debug("Making POST request to {}", request.url());
return call(request);
}

Expand Down Expand Up @@ -659,7 +663,7 @@ CompletableFuture<Response> patch(final String path, final String data) {
requestBuilder(path)
.method("PATCH", RequestBody.create(parse(MediaType.APPLICATION_JSON), data))
.build();
log.debug("Making PATCH request to {}", request.url().toString());
log.debug("Making PATCH request to {}", request.url());
return call(request);
}

Expand Down Expand Up @@ -695,7 +699,7 @@ <T> CompletableFuture<T> patch(
.method("PATCH", RequestBody.create(parse(MediaType.APPLICATION_JSON), data));
extraHeaders.forEach(builder::addHeader);
final Request request = builder.build();
log.debug("Making PATCH request to {}", request.url().toString());
log.debug("Making PATCH request to {}", request.url());
return call(request)
.thenApply(
response -> json().fromJsonUncheckedNotNull(responseBodyUnchecked(response), clazz));
Expand All @@ -709,7 +713,7 @@ <T> CompletableFuture<T> patch(
*/
CompletableFuture<Response> delete(final String path) {
final Request request = requestBuilder(path).delete().build();
log.debug("Making DELETE request to {}", request.url().toString());
log.debug("Making DELETE request to {}", request.url());

Check warning on line 716 in src/main/java/com/spotify/github/v3/clients/GitHubClient.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/spotify/github/v3/clients/GitHubClient.java#L716

Added line #L716 was not covered by tests
return call(request);
}

Expand All @@ -725,7 +729,7 @@ CompletableFuture<Response> delete(final String path, final String data) {
requestBuilder(path)
.method("DELETE", RequestBody.create(parse(MediaType.APPLICATION_JSON), data))
.build();
log.debug("Making DELETE request to {}", request.url().toString());
log.debug("Making DELETE request to {}", request.url());
return call(request);
}

Expand Down
Loading

0 comments on commit 039457d

Please sign in to comment.