Skip to content

Commit d6ea60b

Browse files
committed
feat: Add graphQL API support
1 parent 27719e8 commit d6ea60b

File tree

7 files changed

+62394
-56
lines changed

7 files changed

+62394
-56
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ dependency-reduced-pom.xml
2727
# mvn release
2828
pom.xml.releaseBackup
2929
release.properties
30+
31+
## macOS
32+
.DS_Store

pom.xml

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
<jsr305.version>3.0.2</jsr305.version>
9595
<jsr311-api.version>1.1.1</jsr311-api.version>
9696
<junit.version>5.10.0</junit.version>
97-
<slf4j-api.version>1.7.36</slf4j-api.version>
97+
<slf4j-api.version>2.0.7</slf4j-api.version>
9898
<mockito-core.version>5.6.0</mockito-core.version>
9999
<commons-io.version>2.6</commons-io.version>
100100
<jackson.version>2.15.3</jackson.version>
@@ -103,6 +103,10 @@
103103
<opencensus.version>0.31.1</opencensus.version>
104104
<okhttp.version>4.11.0</okhttp.version>
105105

106+
<!-- BEGIN: plugin versions -->
107+
<graphql.codegen.plugin.version>5.8.0</graphql.codegen.plugin.version>
108+
<!-- END: plugin versions -->
109+
106110
<shade.id>${project.groupId}.githubclient.shade</shade.id>
107111
</properties>
108112

@@ -252,6 +256,16 @@
252256
<version>2.7</version>
253257
<scope>compile</scope>
254258
</dependency>
259+
<dependency>
260+
<groupId>javax.validation</groupId>
261+
<artifactId>validation-api</artifactId>
262+
<version>1.1.0.Final</version>
263+
</dependency>
264+
<dependency>
265+
<groupId>io.github.kobylynskyi</groupId>
266+
<artifactId>graphql-java-codegen</artifactId>
267+
<version>${graphql.codegen.plugin.version}</version>
268+
</dependency>
255269
</dependencies>
256270

257271
<profiles>
@@ -499,6 +513,38 @@
499513
<doclint>none</doclint>
500514
</configuration>
501515
</plugin>
516+
<plugin>
517+
<groupId>io.github.kobylynskyi</groupId>
518+
<artifactId>graphql-codegen-maven-plugin</artifactId>
519+
<version>5.10.0</version>
520+
<executions>
521+
<execution>
522+
<goals>
523+
<goal>generate</goal>
524+
</goals>
525+
<configuration>
526+
<!-- all config options:
527+
https://github.com/kobylynskyi/graphql-java-codegen/blob/main/docs/codegen-options.md
528+
-->
529+
<graphqlSchemas>
530+
<rootDir>${project.basedir}/src/main/resources/schema</rootDir>
531+
<includePattern>.*\.graphqls?</includePattern>
532+
</graphqlSchemas>
533+
<outputDir>${project.build.directory}/generated-sources</outputDir>
534+
<packageName>com.spotify.github.graphql</packageName>
535+
<modelPackageName>com.spotify.github.graphql.models</modelPackageName>
536+
<generateModelsForRootTypes>true</generateModelsForRootTypes>
537+
<generateClient>true</generateClient>
538+
<generateApis>false</generateApis>
539+
<modelValidationAnnotation>@javax.annotation.Nonnull</modelValidationAnnotation>
540+
<customTypesMapping>
541+
<DateTime>java.util.Date</DateTime>
542+
<Price.amount>java.math.BigDecimal</Price.amount>
543+
</customTypesMapping>
544+
</configuration>
545+
</execution>
546+
</executions>
547+
</plugin>
502548
</plugins>
503549
<pluginManagement>
504550
<plugins>

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import static okhttp3.MediaType.parse;
2525

2626
import com.fasterxml.jackson.core.type.TypeReference;
27+
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest;
28+
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequest;
29+
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLResponseProjection;
2730
import com.spotify.github.Tracer;
2831
import com.spotify.github.jackson.Json;
2932
import com.spotify.github.v3.Team;
@@ -41,9 +44,8 @@
4144
import com.spotify.github.v3.repos.CommitItem;
4245
import com.spotify.github.v3.repos.FolderContent;
4346
import com.spotify.github.v3.repos.Repository;
44-
import com.spotify.github.v3.repos.Status;
4547
import com.spotify.github.v3.repos.RepositoryInvitation;
46-
48+
import com.spotify.github.v3.repos.Status;
4749
import java.io.*;
4850
import java.lang.invoke.MethodHandles;
4951
import java.net.URI;
@@ -58,7 +60,6 @@
5860
import java.util.function.Consumer;
5961
import javax.ws.rs.core.HttpHeaders;
6062
import javax.ws.rs.core.MediaType;
61-
6263
import okhttp3.*;
6364
import org.apache.commons.io.FileUtils;
6465
import org.slf4j.Logger;
@@ -447,7 +448,7 @@ Json json() {
447448
*/
448449
CompletableFuture<Response> request(final String path) {
449450
final Request request = requestBuilder(path).build();
450-
log.debug("Making request to {}", request.url().toString());
451+
log.debug("Making request to {}", request.url());
451452
return call(request);
452453
}
453454

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

@@ -474,7 +475,7 @@ CompletableFuture<Response> request(final String path, final Map<String, String>
474475
*/
475476
<T> CompletableFuture<T> request(final String path, final Class<T> clazz) {
476477
final Request request = requestBuilder(path).build();
477-
log.debug("Making request to {}", request.url().toString());
478+
log.debug("Making request to {}", request.url());
478479
return call(request)
479480
.thenApply(body -> json().fromJsonUncheckedNotNull(responseBodyUnchecked(body), clazz));
480481
}
@@ -491,7 +492,7 @@ <T> CompletableFuture<T> request(
491492
final Request.Builder builder = requestBuilder(path);
492493
extraHeaders.forEach(builder::addHeader);
493494
final Request request = builder.build();
494-
log.debug("Making request to {}", request.url().toString());
495+
log.debug("Making request to {}", request.url());
495496
return call(request)
496497
.thenApply(body -> json().fromJsonUncheckedNotNull(responseBodyUnchecked(body), clazz));
497498
}
@@ -510,7 +511,7 @@ <T> CompletableFuture<T> request(
510511
final Request.Builder builder = requestBuilder(path);
511512
extraHeaders.forEach(builder::addHeader);
512513
final Request request = builder.build();
513-
log.debug("Making request to {}", request.url().toString());
514+
log.debug("Making request to {}", request.url());
514515
return call(request)
515516
.thenApply(
516517
response ->
@@ -525,7 +526,7 @@ <T> CompletableFuture<T> request(
525526
*/
526527
<T> CompletableFuture<T> request(final String path, final TypeReference<T> typeReference) {
527528
final Request request = requestBuilder(path).build();
528-
log.debug("Making request to {}", request.url().toString());
529+
log.debug("Making request to {}", request.url());
529530
return call(request)
530531
.thenApply(
531532
response ->
@@ -544,7 +545,7 @@ CompletableFuture<Response> post(final String path, final String data) {
544545
requestBuilder(path)
545546
.method("POST", RequestBody.create(parse(MediaType.APPLICATION_JSON), data))
546547
.build();
547-
log.debug("Making POST request to {}", request.url().toString());
548+
log.debug("Making POST request to {}", request.url());
548549
return call(request);
549550
}
550551

@@ -563,7 +564,7 @@ CompletableFuture<Response> post(
563564
.method("POST", RequestBody.create(parse(MediaType.APPLICATION_JSON), data));
564565
extraHeaders.forEach(builder::addHeader);
565566
final Request request = builder.build();
566-
log.debug("Making POST request to {}", request.url().toString());
567+
log.debug("Making POST request to {}", request.url());
567568
return call(request);
568569
}
569570

@@ -603,18 +604,21 @@ <T> CompletableFuture<T> post(final String path, final String data, final Class<
603604
/**
604605
* Make a POST request to the graphql endpoint of Github
605606
*
606-
* @param data request body as stringified JSON
607+
* @param queryRequest GraphQLOperationRequest object with query or mutation request
608+
* @param responseProjection Select what fields are required in the response
607609
* @return response
608610
*
609611
* @see "https://docs.github.com/en/[email protected]/graphql/guides/forming-calls-with-graphql#communicating-with-graphql"
610612
*/
611-
public CompletableFuture<Response> postGraphql(final String data) {
613+
public CompletableFuture<Response> queryGraphQL(final GraphQLOperationRequest queryRequest, final GraphQLResponseProjection responseProjection) {
614+
GraphQLRequest graphqlRequest = new GraphQLRequest(queryRequest, responseProjection);
615+
String body = graphqlRequest.toQueryString();
612616
final Request request =
613-
graphqlRequestBuilder()
614-
.method("POST", RequestBody.create(parse(MediaType.APPLICATION_JSON), data))
615-
.build();
616-
log.info("Making POST request to {}", request.url());
617-
return call(request);
617+
graphqlRequestBuilder()
618+
.method("POST", RequestBody.create(parse(MediaType.APPLICATION_JSON), body))
619+
.build();
620+
log.info("Making GraphQL Query POST request to {}, with body {}", request.url(), body);
621+
return this.call(request);
618622
}
619623

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

@@ -659,7 +663,7 @@ CompletableFuture<Response> patch(final String path, final String data) {
659663
requestBuilder(path)
660664
.method("PATCH", RequestBody.create(parse(MediaType.APPLICATION_JSON), data))
661665
.build();
662-
log.debug("Making PATCH request to {}", request.url().toString());
666+
log.debug("Making PATCH request to {}", request.url());
663667
return call(request);
664668
}
665669

@@ -695,7 +699,7 @@ <T> CompletableFuture<T> patch(
695699
.method("PATCH", RequestBody.create(parse(MediaType.APPLICATION_JSON), data));
696700
extraHeaders.forEach(builder::addHeader);
697701
final Request request = builder.build();
698-
log.debug("Making PATCH request to {}", request.url().toString());
702+
log.debug("Making PATCH request to {}", request.url());
699703
return call(request)
700704
.thenApply(
701705
response -> json().fromJsonUncheckedNotNull(responseBodyUnchecked(response), clazz));
@@ -709,7 +713,7 @@ <T> CompletableFuture<T> patch(
709713
*/
710714
CompletableFuture<Response> delete(final String path) {
711715
final Request request = requestBuilder(path).delete().build();
712-
log.debug("Making DELETE request to {}", request.url().toString());
716+
log.debug("Making DELETE request to {}", request.url());
713717
return call(request);
714718
}
715719

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

0 commit comments

Comments
 (0)