如何在 Graphql 响应中排除可为空的字段

问题描述 投票:0回答:1

我正在开发一个 graphql 服务,它返回一些响应元素。在我的 graphql 模式中,我定义了非 nullabale 和一些可为 null 的字段。我的期望是,如果任何可为 null 的字段为 null 作为值,则应从结果中排除该字段。

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.17'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}

group = 'com.learntech'
version = '1.0.0-SNAPSHOT'

java {
    sourceCompatibility = '11'
}

dependencyManagement {
    imports {
        mavenBom 'com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:5.5.5'
    }
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    //implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation(platform("com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:5.5.5"))
    implementation 'com.netflix.graphql.dgs:graphql-dgs-webflux-starter:5.5.5'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'com.h2database:h2'
    compileOnly 'org.projectlombok:lombok'
    //developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

数据获取器

@DgsComponent
@Slf4j
public class UserDataFetcher {

    private final UserSearchService userSearchService;

    public UserDataFetcher(UserSearchService userSearchService) {
        this.userSearchService = userSearchService;
    }


    @DgsQuery
    public Mono<List<User>> users(@InputArgument SearchInput searchInput) throws JsonProcessingException {
        log.info("users() Starts");
        log.info("First Name {}", searchInput.getFirstName());
        log.info("Last Name {}", searchInput.getLastName());

        List<User> users = userSearchService.searchUser(searchInput);
        return Mono.just(users);
    }

    @DgsQuery
    public Mono<User> user(@InputArgument UUID id){
        log.info("user() Starts");
        User user = userSearchService.searchById(id);
        return Mono.just(user);
    }
}

型号

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User {
    private UUID id;
    private String firstName;
    private String lastName;
    private String dateOfBirth;
    private String gender;
    private List<Address> address;
    private List<Phone> phone;
}

架构

type Query {
    users(searchInput: SearchInput) : [User]
    user(id : ID!): User
}

type UserResponse{
    users: [User]
}

type User {
    id: ID!
    firstName: String!
    lastName: String!
    dateOfBirth: String
    gender: String
    address: [Address]
    phone: [Phone]
}

type Address {
    type: String
    street1: String
    street2: String
    city: String
    state: String
    zip: Int
}

type Phone {
    type: String
    number: String
    countryCode: String
}

input SearchInput {
    firstName: String
    lastName: String
    dateOfBirth: String
}

根据此架构,用户对象 dateOfBirth 是 nullabale 字段。因此,我希望当该字段的值为空时,应从我的响应中排除该字段。根据响应模型定义 @JsonInclude(JsonInclude.Include.NON_NULL) 应将其从响应中排除。但当该字段具有空值时,我总是得到 null 作为该字段的值。

索取样品

{
    "query": "query { user(id: \"USERID\") { id firstName lastName dateOfBirth } }"
}

响应示例

{
    "data": {
        "user": {
            "id": "2c628b4e-bef3-4794-9af5-5e8deb69817e",
            "firstName": "Jhon",
            "lastName": "Victor",
            "dateOfBirth": null // This is not suppose to be in response.
        }
    }
}
spring-graphql netflix-dgs
1个回答
0
投票

这是预期的行为。

Spring for GraphQL 应用程序仅使用 Jackson 进行

Map
转换。实际的 JSON 格式是 graphql-java 的控制器,它遵循规范(此处,使用请求的选择集进行响应)。添加 Jackson 注释对此没有任何影响。

如果您想更多地自定义响应,可以使用自定义标量。

© www.soinside.com 2019 - 2024. All rights reserved.