type Movie @key(fields: "movieId") @extends {
movieId: Int @external
script: MovieScript
}
type MovieScript {
title: String
director: String
actors: [Actor]
}
type Actor {
name: String
gender: String
age: Int
}
query ($representations: [_Any!]!) {
_entities(representations: $representations) {
... on Movie {
movieId
script { title }
}}}
@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");
}