How to remove the duplicates from the response of Graphql automatically

Lets see the below graphql request I have.

query {
  test1(request:{
            userId: "test1@test.com"
  }) {
       id
       name
       product
  }
}

And the Response is below:

{
    "data": {
        "test1": [{
                "id": 1,
                "name": "Test",
                "product": "ABCD"
            }, {
                "id": 1,
                "name": "Test",
                "product": "ABCD-2"
            },
        ]
    }
}

Now if I remove the "product" in the query response field, the reponse looks like below. That is where the duplicates come.

{
    "data": {
        "test1": [{
                "id": 1,
                "name": "Test"
            }, {
                "id": 1,
                "name": "Test"
            },
        ]
    }
}

Let me know if we have any library available to do this. We are using Spring Boot with below dependency.

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-graphql</artifactId>
</dependency>

Comment From: ashok2ashok

@miriyalajanardhan - GraphQL is responsible for responding to your request. It is GraphQL server's responsibility to apply business logic to suppress duplicates or normalize/iron out response before returning it to the client.

Also explore if

  • WebGraphQlInterceptor is helpful Ref: https://docs.spring.io/spring-graphql/docs/1.0.3/reference/html/#server-interception

  • DataLoader is helpful. Ref: https://docs.spring.io/spring-graphql/docs/1.0.3/reference/html/#execution-batching-dataloader

Comment From: bclozel

Duplicates graphql-java/graphql-java#3064