|
| 1 | +package graphql.kickstart.graphql.annotations; |
| 2 | + |
| 3 | +import com.graphql.spring.boot.test.GraphQLResponse; |
| 4 | +import com.graphql.spring.boot.test.GraphQLTestTemplate; |
| 5 | +import graphql.kickstart.graphql.annotations.test.interfaces.Car; |
| 6 | +import graphql.kickstart.graphql.annotations.test.interfaces.Truck; |
| 7 | +import graphql.schema.GraphQLNamedType; |
| 8 | +import graphql.schema.GraphQLScalarType; |
| 9 | +import graphql.schema.GraphQLSchema; |
| 10 | +import org.junit.jupiter.api.DisplayName; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.test.context.SpringBootTest; |
| 14 | +import org.springframework.test.context.ActiveProfiles; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +@DisplayName("Testing interface handling (ignore abstract implementations).") |
| 23 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, |
| 24 | + properties = "graphql.annotations.ignore-abstract-interface-implementations=true") |
| 25 | +@ActiveProfiles({"test", "interface-test"}) |
| 26 | +class GraphQLInterfaceQueryIgnoreAbstractInterfaceImplementationsTest { |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private GraphQLTestTemplate graphQLTestTemplate; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private GraphQLSchema graphQLSchema; |
| 33 | + |
| 34 | + @Test |
| 35 | + @DisplayName("Assert that GraphQL interfaces and their implementations are registered correctly.") |
| 36 | + void testInterfaceQuery() throws IOException { |
| 37 | + // WHEN |
| 38 | + final GraphQLResponse actual = graphQLTestTemplate |
| 39 | + .postForResource("queries/test-interface-query.graphql"); |
| 40 | + // THEN |
| 41 | + assertThat(actual.get("$.data.vehicles[0]", Car.class)) |
| 42 | + .usingRecursiveComparison().ignoringAllOverriddenEquals() |
| 43 | + .isEqualTo(Car.builder().numberOfSeats(4).registrationNumber("ABC-123").build()); |
| 44 | + assertThat(actual.get("$.data.vehicles[1]", Truck.class)) |
| 45 | + .usingRecursiveComparison().ignoringAllOverriddenEquals() |
| 46 | + .isEqualTo(Truck.builder().cargoWeightCapacity(12).registrationNumber("CBA-321").build()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @DisplayName("Assert that abstract GraphQL interface implementations are excluded from the schema.") |
| 51 | + void testInterfaceImplementationDetection() { |
| 52 | + // THEN |
| 53 | + Set<String> vehicleDomainTypes = graphQLSchema.getAllTypesAsList().stream() |
| 54 | + .filter(type -> !(type instanceof GraphQLScalarType)) |
| 55 | + .map(GraphQLNamedType::getName) |
| 56 | + .filter(name -> !name.startsWith("__")) |
| 57 | + .filter(name -> !"PageInfo".equals(name)) |
| 58 | + .collect(Collectors.toSet()); |
| 59 | + // Must not contain "AbstractVehicle" |
| 60 | + assertThat(vehicleDomainTypes) |
| 61 | + .containsExactlyInAnyOrder("InterfaceQuery", "Vehicle", "Car", "Truck"); |
| 62 | + } |
| 63 | +} |
| 64 | + |
0 commit comments