1

I am replacing an existing REST endpoint with GraphQL.

In our existing REST endpoint, we return a JSON array.

[{
    "id": "ABC"
  },
  {
    "id": "123"
  },
  {
    "id": "xyz"
  },
  {
    "id": "789"
  }
]

GraphQL seems to be wrapping the array in two additional object layers. Is there any way to remove the "data" and "Client" layers?

Response data:

{
  "data": {
    "Client": [
      {
        "id": "ABC"
      },
      {
        "id": "123"
      },
      {
        "id": "xyz"
      },
      {
        "id": "789"
      }
    ]
  }
}

My query:

{
  Client(accountId: "5417727750494381532d735a") {
    id
  }
}
JTAN
  • 411
  • 2
  • 5
  • 15

1 Answers1

1

No. That was the whole purpose of GraphQL. To have a single endoint and allow users to fetch different type/granularity of data by specifying the input in a query format as opposed to REST APIs and then map them onto the returned JSON output.

'data' acts as a parent/root level container for different entities that you have queried. Without these keys in the returned JSON data, there won't be any way to segregate the corresponding data. e.g.

Your above query can be modified to include another entity like Owner,

{
  Client(accountId: "5417727750494381532d735a") {
    id
  }
  Owner {
    id
  }
}

In which case, the output will be something like

{
  "data": {
    "Client": [
      ...
    ],
    "Owner": [
      ...
    ]
  }
}

Without the 'Client' and 'Owner' keys in the JSON outout, there is no way to separate the corresponding array values.

In your case, you can get only the array by doing data.Client on the returned output.