spring-webfluxapollospring-graphql

Spring webflux and spring boot starter graphql doesn't return nullable field


I have created spring web flux with graphql using the following dependenies

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-graphql</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.graphql</groupId>
        <artifactId>spring-graphql-test</artifactId>
        <scope>test</scope>
    </dependency>

and my graphql endpoint

@Controller
public class UserController {
private final UserService userService;


public UserController(UserService userService) {
    this.userService = userService;
}

@SneakyThrows
@QueryMapping
public Flux<User> getUser() {
    return userService.findAllUsers();
}

and User Object

public class User extends ReusableField implements Serializable {

private static final long serialVersionUID = 1L;

@Id
private Long id;
private String email;

private String fieldNullable;
....

The field fieldNullable is not returned from the graphql response hence apollo-garaphql for angular display console error "Missing field 'fieldNullable' while writing result"

how can I make spring return this field whether it is null or not


Solution

  • I found the issue.I had a bean definition

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
    
        mapper.registerModule(new ParameterNamesModule())
                .registerModule(new Jdk8Module())
                .registerModule(new JavaTimeModule());
    
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jsonConverter.setObjectMapper(mapper);
        return jsonConverter;
    }
    

    The issue is ith this line mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

    Changing to mapper.setSerializationInclusion(JsonInclude.Include.USE_DEFAULTS);

    solved the problem`