Spring Boot Graphql Remove duplicates from the Response

162 Views Asked by At

How to remove the duplicates from the response of Graphql automatically

Lets see the below graphql request I have.

query {
  test1(request:{
            userId: "[email protected]"
  }) {
       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>
1

There are 1 best solutions below

0
On

I think there is a flaw in your schema design. If the id is of type ID!, it must be a technical id that's unique in the application. Returning the same id for two different entries should not happen. This must be dealt with at the data fetching layer and in your data model.