Skip to content

Commit 760bff1

Browse files
authored
Merge pull request #2 from apollosolutions/shane-updates
Update with working Java example
2 parents 3bc0857 + 3d89219 commit 760bff1

16 files changed

+190
-165
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,9 @@ dist
129129
.yarn/install-state.gz
130130
.pnp.*
131131

132+
# Router binary
132133
router/router
133-
.env
134+
router/configuration_schema.json
135+
136+
# IDE files
137+
.idea

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lts/*

java-coprocessor/pom.xml

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>3.0.5</version>
9+
<version>3.1.1</version>
1010
<relativePath /> <!-- lookup parent from repository -->
1111
</parent>
1212
<groupId>com.example</groupId>
@@ -32,22 +32,22 @@
3232
<artifactId>commons-lang3</artifactId>
3333
<version>3.12.0</version>
3434
</dependency>
35-
36-
35+
<dependency>
36+
<groupId>com.fasterxml.jackson.core</groupId>
37+
<artifactId>jackson-annotations</artifactId>
38+
<version>2.15.2</version>
39+
</dependency>
3740
<dependency>
3841
<groupId>com.fasterxml.jackson.core</groupId>
3942
<artifactId>jackson-databind</artifactId>
40-
<version>2.15.0</version>
43+
<version>2.15.2</version>
4144
</dependency>
42-
43-
4445
<dependency>
4546
<groupId>com.fasterxml.jackson</groupId>
4647
<artifactId>jackson-base</artifactId>
47-
<version>2.15.0</version>
48+
<version>2.15.2</version>
4849
<type>pom</type>
4950
</dependency>
50-
5151
<dependency>
5252
<groupId>org.springframework.boot</groupId>
5353
<artifactId>spring-boot-starter-web</artifactId>
@@ -68,4 +68,4 @@
6868
</plugins>
6969
</build>
7070

71-
</project>
71+
</project>

java-coprocessor/src/main/java/com/example/demo/CoprocessorController.java

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
package com.example.demo;
22

3-
import com.example.demo.Models.RouterRequestBody;
4-
import org.springframework.web.bind.annotation.*;
3+
import com.example.demo.models.CoprocessorStage;
4+
import com.example.demo.models.RouterPayload;
5+
import com.example.demo.handlers.RouterRequestHandler;
56

6-
import com.example.demo.RequestHandlers.RouterRequest;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RestController;
710

811
@RestController
912
public class CoprocessorController {
10-
private final RouterRequest routerRequestHandler;
13+
private final RouterRequestHandler routerRequestHandler;
1114

1215
public CoprocessorController() {
13-
this.routerRequestHandler = new RouterRequest();
16+
this.routerRequestHandler = new RouterRequestHandler();
1417
}
1518

1619
@PostMapping("/")
17-
public RouterRequestBody StageSelect(@RequestBody RouterRequestBody request) {
18-
String stage = request.getStage();
20+
public RouterPayload StageSelect(@RequestBody RouterPayload request) {
21+
CoprocessorStage stage = request.getStage();
1922

20-
switch (stage) {
21-
case "RouterRequest":
22-
request = routerRequestHandler.handle(request);
23-
break;
23+
if (stage == CoprocessorStage.ROUTER_REQUEST) {
24+
request = routerRequestHandler.handle(request);
2425
}
2526

2627
return request;

java-coprocessor/src/main/java/com/example/demo/Models/Context.java

-15
This file was deleted.

java-coprocessor/src/main/java/com/example/demo/Models/RouterRequestBody.java

-98
This file was deleted.

java-coprocessor/src/main/java/com/example/demo/RequestHandlers/RouterRequest.java renamed to java-coprocessor/src/main/java/com/example/demo/handlers/RouterRequestHandler.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1-
package com.example.demo.RequestHandlers;
1+
package com.example.demo.handlers;
22

3-
import com.example.demo.Models.RouterRequestBody;
3+
import com.example.demo.models.RouterPayload;
44

5-
import java.util.ArrayList;
6-
import java.util.LinkedHashMap;
7-
import java.util.UUID;
8-
9-
public class RouterRequest {
10-
public RouterRequestBody handle(RouterRequestBody request) {
5+
public class RouterRequestHandler {
6+
public RouterPayload handle(RouterPayload request) {
117
// This is the object sent by the Router that you can act upon to update headers, context, auth claims, etc
128
// If you update the "control" property from "Continue" to something like { "break": 400 }, it will terminate the request and return the specified HTTP error
139
// See: https://www.apollographql.com/docs/router/customizations/coprocessor/
1410
// The object sent by the Router has been mapped to a `RouterRequestBody` object by the `@RequestBody` annotation in the `CoprocessorController` class
1511
System.out.println(request.getHeaders());
1612
System.out.println(request.getMethod());
17-
System.out.println(request.getContext().getEntries());
1813
System.out.println(request.getSDL());
1914
System.out.println(request.getVersion());
20-
System.out.println(request.getBody());
2115
System.out.println(request.getStage());
22-
System.out.println(request.getPath());
2316
System.out.println(request.getControl());
2417
System.out.println(request.getId());
2518

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.demo.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
5+
@JsonInclude(JsonInclude.Include.NON_ABSENT)
6+
class CoprocessorControl {
7+
private Integer breakVal;
8+
9+
CoprocessorControl() {
10+
}
11+
12+
CoprocessorControl(Integer breakVal) {
13+
this.breakVal = breakVal;
14+
}
15+
16+
public Integer getBreak() {
17+
return this.breakVal;
18+
}
19+
20+
public void setBreak(Integer breakVal) {
21+
this.breakVal = breakVal;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.demo.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
4+
import com.fasterxml.jackson.annotation.JsonValue;
5+
6+
public enum CoprocessorStage {
7+
ROUTER_REQUEST("RouterRequest"),
8+
ROUTER_RESPONSE("RouterResponse"),
9+
SUPERGRAPH_REQUEST("SupergraphRequest"),
10+
SUPERGRAPH_RESPONSE("SupergraphResponse"),
11+
SUBGRAPH_REQUEST("SubgraphRequest"),
12+
SUBGRAPH_RESPONSE("SubgraphResponse"),
13+
@JsonEnumDefaultValue
14+
UNKNOWN("Unknown");
15+
16+
private String name;
17+
18+
private CoprocessorStage(String name) {
19+
this.name = name;
20+
}
21+
22+
@JsonValue
23+
public String getName() {
24+
return this.name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.example.demo.models;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
5+
import java.util.ArrayList;
6+
import java.util.LinkedHashMap;
7+
8+
@JsonInclude(JsonInclude.Include.NON_ABSENT)
9+
public class RouterPayload {
10+
private Object control;
11+
private LinkedHashMap<String, ArrayList<String>> headers;
12+
private String id;
13+
private String method;
14+
private String sdl;
15+
private CoprocessorStage stage;
16+
private Integer version;
17+
18+
RouterPayload() {
19+
this.control = "continue";
20+
this.id = "";
21+
}
22+
23+
public Object getControl() {
24+
return this.control;
25+
}
26+
27+
public void setControl(Object control) {
28+
this.control = control;
29+
}
30+
31+
public LinkedHashMap<String, ArrayList<String>> getHeaders() {
32+
return this.headers;
33+
}
34+
35+
public void setHeaders(LinkedHashMap<String, ArrayList<String>> headers) {
36+
this.headers = headers;
37+
}
38+
39+
public String getId() {
40+
return this.id;
41+
}
42+
43+
public void setId(String id) {
44+
this.id = id;
45+
}
46+
47+
public String getMethod() {
48+
return this.method;
49+
}
50+
51+
public void setMethod(String method) {
52+
this.method = method;
53+
}
54+
55+
public String getSDL() {
56+
return this.sdl;
57+
}
58+
59+
public void setSDL(String sdl) {
60+
this.sdl = sdl;
61+
}
62+
63+
public CoprocessorStage getStage() {
64+
return this.stage;
65+
}
66+
67+
public void setStage(CoprocessorStage stage) {
68+
this.stage = stage;
69+
}
70+
71+
public Integer getVersion() {
72+
return this.version;
73+
}
74+
75+
public void setVersion(Integer version) {
76+
this.version = version;
77+
}
78+
}

router/download_router.sh

+9-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
curl -sSL https://router.apollo.dev/download/nix/latest | sh
1+
set -e
2+
3+
echo "Downloading latest Router version..."
4+
curl -sSL https://router.apollo.dev/download/nix/latest | sh
5+
6+
echo "Updating config schema file..."
7+
./router config schema > configuration_schema.json
8+
9+
echo "Success!"

router/router-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
# $schema: configuration_schema.json
2+
13
coprocessor:
24
url: http://localhost:3007
35
router:
46
request:
57
headers: true
6-
context: true
7-
body: true

0 commit comments

Comments
 (0)