query ($representations: [_Any!]!) {
_entities(representations: $representations) {
... on Movie {
movieId
script { title }
}}}
@Test
void federatedMovieQuery() throws IOException {
String query = "query ($representations: [_Any!]!) {" +
"_entities(representations: $representations) {" +
"... on Movie {" +
"movieId " +
"script { title }" +
"}}}";
Map<String, Object> variables = new HashMap<>();
Map<String,Object> representation = new HashMap<>();
representation.put("__typename", "Movie");
representation.put("movieId", 1);
variables.put("representations", List.of(representation));
DocumentContext context = queryExecutor.executeAndGetDocumentContext(query, variables);
GraphQLResponse response = new GraphQLResponse(context.jsonString());
Movie movie = response.extractValueAsObject("data._entities[0]", Movie.class);
assertThat(movie.getScript().getTitle()).isEqualTo("Top Secret");
}
@Test
void federatedMovieQueryAPI() throws IOException {
// constructs the _entities query with variable $representations containing a
// movie representation that represents { __typename: "Movie" movieId: 12345 }
EntitiesGraphQLQuery entitiesQuery = new EntitiesGraphQLQuery.Builder()
.addRepresentationAsVariable(
MovieRepresentation.newBuilder().movieId(1122).build()
)
.build();
// sets up the query and the field selection set using the EntitiesProjectionRoot
GraphQLQueryRequest request = new GraphQLQueryRequest(
entitiesQuery,
new EntitiesProjectionRoot().onMovie().movieId().script().title());
String query = request.serialize();
// pass in the constructed _entities query with the variable map containing representations
DocumentContext context = queryExecutor.executeAndGetDocumentContext(query, entitiesQuery.getVariables());
GraphQLResponse response = new GraphQLResponse(context.jsonString());
Movie movie = response.extractValueAsObject("data._entities[0]", Movie.class);
assertThat(movie.getScript().getTitle()).isEqualTo("Top Secret");
}