Skip to content

Commit 139922d

Browse files
committed
[grid] Splitting GraphQL queries
This will make life easier for clients consuming data because they can cache data without any custom configuration.
1 parent 21e5734 commit 139922d

File tree

11 files changed

+60
-56
lines changed

11 files changed

+60
-56
lines changed

java/server/src/org/openqa/selenium/grid/graphql/GraphqlHandler.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,14 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
173173
}
174174

175175
private RuntimeWiring buildRuntimeWiring() {
176+
GridData gridData = new GridData(distributor, newSessionQueuer, publicUri, version);
176177
return RuntimeWiring.newRuntimeWiring()
177178
.scalar(Types.Uri)
178179
.scalar(Types.Url)
179180
.type("GridQuery", typeWiring -> typeWiring
180-
.dataFetcher("grid", new GridData(
181-
distributor,
182-
newSessionQueuer,
183-
publicUri,
184-
version))
181+
.dataFetcher("grid", gridData)
182+
.dataFetcher("sessionsInfo", gridData)
183+
.dataFetcher("nodesInfo", gridData)
185184
.dataFetcher("session", new SessionData(distributor)))
186185
.build();
187186
}

java/server/src/org/openqa/selenium/grid/graphql/selenium-grid-schema.graphqls

+11-3
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,27 @@ type Node {
4646
osInfo: OsInfo!
4747
}
4848

49+
type Sessions {
50+
sessionQueueRequests: [String]!
51+
sessions: [Session]!
52+
}
53+
54+
type Nodes {
55+
nodes: [Node!]!
56+
}
57+
4958
type GridQuery {
5059
grid: Grid!
60+
sessionsInfo: Sessions!
61+
nodesInfo: Nodes!
5162
session(id: String!) : Session!
5263
}
5364

5465
type Grid {
5566
uri: Uri!
56-
nodes: [Node!]!
5767
totalSlots: Int!
5868
usedSlots: Int!
5969
sessionCount: Int!
6070
version: String!
6171
sessionQueueSize: Int!
62-
sessionQueueRequests: [String]!
63-
sessions: [Session]!
6472
}

java/server/test/org/openqa/selenium/grid/graphql/GraphqlHandlerTest.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717

1818
package org.openqa.selenium.grid.graphql;
1919

20+
import com.google.common.collect.ImmutableMap;
2021
import org.junit.Before;
2122
import org.junit.Test;
2223
import org.openqa.selenium.Capabilities;
2324
import org.openqa.selenium.ImmutableCapabilities;
2425
import org.openqa.selenium.SessionNotCreatedException;
2526
import org.openqa.selenium.events.EventBus;
2627
import org.openqa.selenium.events.local.GuavaEventBus;
27-
import org.openqa.selenium.grid.data.*;
28+
import org.openqa.selenium.grid.data.CreateSessionRequest;
29+
import org.openqa.selenium.grid.data.CreateSessionResponse;
30+
import org.openqa.selenium.grid.data.RequestId;
2831
import org.openqa.selenium.grid.data.Session;
2932
import org.openqa.selenium.grid.data.Slot;
3033
import org.openqa.selenium.grid.distributor.Distributor;
@@ -57,8 +60,8 @@
5760
import java.io.UncheckedIOException;
5861
import java.net.URI;
5962
import java.net.URISyntaxException;
60-
import java.time.Instant;
6163
import java.time.Duration;
64+
import java.time.Instant;
6265
import java.util.Collections;
6366
import java.util.Map;
6467
import java.util.Optional;
@@ -77,15 +80,13 @@
7780
import static org.openqa.selenium.remote.http.HttpMethod.GET;
7881
import static org.openqa.selenium.remote.http.HttpMethod.POST;
7982

80-
import com.google.common.collect.ImmutableMap;
81-
8283
public class GraphqlHandlerTest {
8384

85+
private static final Json JSON = new Json();
8486
private final Secret registrationSecret = new Secret("stilton");
8587
private final URI publicUri = new URI("http://example.com/grid-o-matic");
8688
private final String version = "4.0.0";
8789
private final Wait<Object> wait = new FluentWait<>(new Object()).withTimeout(Duration.ofSeconds(5));
88-
private static final Json JSON = new Json();
8990
private Distributor distributor;
9091
private NewSessionQueuer queuer;
9192
private Tracer tracer;
@@ -188,12 +189,12 @@ public void shouldBeAbleToGetSessionQueueRequests() {
188189
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
189190

190191
Map<String, Object> topLevel = executeQuery(handler,
191-
"{ grid { sessionQueueRequests } }");
192+
"{ sessionsInfo { sessionQueueRequests } }");
192193

193194
assertThat(topLevel).isEqualTo(
194195
singletonMap(
195196
"data", singletonMap(
196-
"grid", singletonMap(
197+
"sessionsInfo", singletonMap(
197198
"sessionQueueRequests", singletonList(JSON.toJson(caps))))));
198199
}
199200

@@ -202,25 +203,25 @@ public void shouldBeReturnAnEmptyListIfQueueIsEmpty() {
202203
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
203204

204205
Map<String, Object> topLevel = executeQuery(handler,
205-
"{ grid { sessionQueueRequests } }");
206+
"{ sessionsInfo { sessionQueueRequests } }");
206207

207208
assertThat(topLevel).isEqualTo(
208209
singletonMap(
209210
"data", singletonMap(
210-
"grid", singletonMap(
211+
"sessionsInfo", singletonMap(
211212
"sessionQueueRequests", Collections.emptyList()))));
212213
}
213214

214215
@Test
215216
public void shouldReturnAnEmptyListForNodesIfNoneAreRegistered() {
216217
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
217218

218-
Map<String, Object> topLevel = executeQuery(handler, "{ grid { nodes { uri } } }");
219+
Map<String, Object> topLevel = executeQuery(handler, "{ nodesInfo { nodes { uri } } }");
219220

220221
assertThat(topLevel).describedAs(topLevel.toString()).isEqualTo(
221222
singletonMap(
222223
"data", singletonMap(
223-
"grid", singletonMap(
224+
"nodesInfo", singletonMap(
224225
"nodes", Collections.emptyList()))));
225226
}
226227

@@ -245,12 +246,12 @@ public boolean test(Capabilities capabilities) {
245246
wait.until(obj -> distributor.getStatus().hasCapacity());
246247

247248
GraphqlHandler handler = new GraphqlHandler(tracer, distributor, queuer, publicUri, version);
248-
Map<String, Object> topLevel = executeQuery(handler, "{ grid { nodes { uri } } }");
249+
Map<String, Object> topLevel = executeQuery(handler, "{ nodesInfo { nodes { uri } } }");
249250

250251
assertThat(topLevel).describedAs(topLevel.toString()).isEqualTo(
251252
singletonMap(
252253
"data", singletonMap(
253-
"grid", singletonMap(
254+
"nodesInfo", singletonMap(
254255
"nodes", singletonList(singletonMap("uri", nodeUri))))));
255256
}
256257

javascript/grid-ui/src/components/RunningSessions/RunningSessions.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export default function RunningSessions(props) {
336336
<Typography gutterBottom>
337337
Capabilities:
338338
</Typography>
339-
<Typography gutterBottom>
339+
<Typography gutterBottom component={'span'}>
340340
<pre>
341341
{JSON.stringify(JSON.parse(row.rawCapabilities as string), null, 2)}
342342
</pre>

javascript/grid-ui/src/components/TopBar/TopBar.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const useStyles = makeStyles((theme) => ({
5252
},
5353
}));
5454

55-
const GRID_SUMMARY_QUERY = loader("../../graphql/grid-summary.gql");
55+
const GRID_QUERY = loader("../../graphql/grid.gql");
5656

5757

5858
export default function TopBar() {
@@ -65,7 +65,7 @@ export default function TopBar() {
6565
setOpen(false);
6666
};
6767

68-
const {loading, error, data} = useQuery(GRID_SUMMARY_QUERY,
68+
const {loading, error, data} = useQuery(GRID_QUERY,
6969
{pollInterval: GridConfig.status.xhrPollingIntervalMillis, fetchPolicy: "network-only"});
7070
if (loading) return <p>Loading...</p>;
7171
if (error) return <p>`Error! ${error.message}`</p>;

javascript/grid-ui/src/graphql/grid-summary.gql

-8
This file was deleted.

javascript/grid-ui/src/graphql/grid.gql

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
1-
query {
1+
query Summary {
22
grid {
33
uri
4-
nodes {
5-
id
6-
uri
7-
status
8-
maxSession
9-
slotCount
10-
stereotypes
11-
version
12-
sessionCount
13-
osInfo {
14-
version
15-
name
16-
arch
17-
}
18-
}
194
totalSlots
205
usedSlots
216
version
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
query GetNodes {
2+
nodesInfo {
3+
nodes {
4+
id
5+
uri
6+
status
7+
maxSession
8+
slotCount
9+
stereotypes
10+
version
11+
sessionCount
12+
osInfo {
13+
version
14+
name
15+
arch
16+
}
17+
}
18+
}
19+
}

javascript/grid-ui/src/graphql/sessions.gql

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
query {
2-
grid {
1+
query GetSessions {
2+
sessionsInfo {
33
sessions {
44
id
55
capabilities

javascript/grid-ui/src/screens/Overview/Overview.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ const useStyles = makeStyles((theme) => ({
2828
},
2929
}));
3030

31-
const GRID_QUERY = loader("../../graphql/grid.gql");
31+
const NODES_QUERY = loader("../../graphql/nodes.gql");
3232

3333

3434
export default function Overview() {
3535
const classes = useStyles();
3636
const fixedHeightPaper = clsx(classes.paper, classes.fixedHeight);
3737

38-
const {loading, error, data} = useQuery(GRID_QUERY,
39-
{pollInterval: GridConfig.status.xhrPollingIntervalMillis, fetchPolicy: "network-only"});
38+
const {loading, error, data} = useQuery(NODES_QUERY,
39+
{pollInterval: GridConfig.status.xhrPollingIntervalMillis, fetchPolicy: "network-only"});
4040
if (loading) return <p>Loading...</p>;
4141
if (error) return <p>`Error! ${error.message}`</p>;
4242

43-
const nodes = data.grid.nodes.map((node) => {
43+
const nodes = data.nodesInfo.nodes.map((node) => {
4444
const osInfo: OsInfoType = {
4545
name: node.osInfo.name,
4646
version: node.osInfo.version,

javascript/grid-ui/src/screens/Sessions/Sessions.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ export default function Sessions() {
1818

1919
return (
2020
<Grid container spacing={3}>
21-
<QueuedSessions sessionQueueRequests={data.grid.sessionQueueRequests}/>
22-
<RunningSessions sessions={data.grid.sessions}/>
21+
<QueuedSessions sessionQueueRequests={data.sessionsInfo.sessionQueueRequests}/>
22+
<RunningSessions sessions={data.sessionsInfo.sessions}/>
2323
</Grid>
2424
);
2525
}

0 commit comments

Comments
 (0)