Skip to content

Commit

Permalink
Merge pull request #1504 from navikt/nais-api-v1
Browse files Browse the repository at this point in the history
Fix outdated GraphQL query, and attempt to introduce pagination
  • Loading branch information
erik-a-e authored Nov 26, 2024
2 parents 2c36eb1 + d972f7e commit 7df59fe
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.web.reactive.function.client.WebClient;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -86,21 +87,36 @@ public boolean naisTeamExists(String teamId) {
}

private List<NaisTeam> fetchAllNaisTeams() {
var paginationLimit = 1000;

var out = client.document(NaisTeam.TEAMS_QUERY)
.variable("limit", paginationLimit)
.variable("offset", 0)
List<NaisTeam> allTeams = new ArrayList<>();
String cursor = "";
boolean hasNextPage = true;

while (hasNextPage) {
var response = client.document(NaisTeam.TEAMS_QUERY)
.variable("first", 100)
.variable("after", cursor)
.execute()
.map(response -> response.field("teams.nodes").toEntity(new ParameterizedTypeReference<List<NaisTeam>>() {
}))
.block();

if(out != null && out.size() > (2*paginationLimit/3)){
log.error("fetchAllNaisTeams: The amount of nais-teams fetched is approaching the pagination limit, {} / {}. Consider implementing proper support for pagination.", out.size(), paginationLimit);
if (response != null && response.isValid()) {
var teams = response
.field("teams.nodes")
.toEntity(new ParameterizedTypeReference<List<NaisTeam>>() {});

if (teams != null) {
allTeams.addAll(teams);
}

cursor = response.field("teams.pageInfo.endCursor").toEntity(String.class);
hasNextPage = Boolean.TRUE.equals(response.field("teams.pageInfo.hasNextPage").toEntity(Boolean.class));
} else {
log.error("fetchAllNaisTeams: Received a null response from the GraphQL query.");
break;
}
}

return out;
log.info("fetchAllNaisTeams: Fetched {} nais-teams in total.", allTeams.size());
return allTeams;
}

private NaisTeam fetchNaisTeam(String slug) {
Expand All @@ -109,6 +125,6 @@ private NaisTeam fetchNaisTeam(String slug) {
.execute()
.block();

return response.isValid() ? response.field("team").toEntity(NaisTeam.class) : null;
return response != null && response.isValid() ? response.field("team").toEntity(NaisTeam.class) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ public record NaisTeam(
@SuppressWarnings("GraphQLUnresolvedReference")
public final static String TEAMS_QUERY = //language=graphql
"""
query ($limit: Int, $offset: Int) {
teams(limit: $limit, offset: $offset) {
query ($first: Int, $after: Cursor!) {
teams(first: $first, after: $after) {
nodes {
slackChannel
purpose
slug
}
pageInfo {
hasNextPage
endCursor
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ client:
console:
auth:
token: ${NAIS_CONSOLE_TOKEN}
base-url: https://console.nav.cloud.nais.io/query
base-url: https://console.nav.cloud.nais.io/graphql
nom:
graphql:
url: https://nom/graphql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void afterEach(ExtensionContext context) {
}

private void stubCommon() {
getWiremock().stubFor(post("/console/query").willReturn(okJson(toJson(consoleMockResponse())))); // Stub Nais Console Team Query
getWiremock().stubFor(post("/console/graphql").willReturn(okJson(toJson(consoleMockResponse())))); // Stub Nais Console Team Query

getWiremock().stubFor(get(urlMatching("/datacatgraph/node/out/.*")).willReturn(notFound()));
getWiremock().stubFor(get(urlMatching("/datacatgraph/node/in/.*")).willReturn(notFound()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ spring.kafka.security.protocol=PLAINTEXT
spring.main.allow-bean-definition-overriding=true
spring.main.lazy-initialization=true

client.nais.console.base-url=http://localhost:${wiremock.server.port:8080}/console/query
client.nais.console.base-url=http://localhost:${wiremock.server.port:8080}/console/graphql
client.process-cat.base-url=http://localhost:${wiremock.server.port:8080}/processcat
client.nom.graphql.url=http://localhost:${wiremock.server.port:8080}/nomgraphql
team-catalog.envlevel=primary
Expand Down

0 comments on commit 7df59fe

Please sign in to comment.