Uma Alternativa ao GraqhQL com API Rest — Pattern Partial Response

Tiago Lima
Comunidade XP
2 min readJan 20, 2023

--

Compartilhando com vocês uma alternativa ao GraphQL bem bacana para responder o payload exatamente que o FrontEnd ou Mobile precisa na resposta do BackEnd utilizando o pattern Partial Response.

O que é o Pattern Partial Response?

Partial Response é como um SQL Select onde você específica os campos que deseja recuperar.

Segue um exemplo chamando uma API de Users sem o pattern retornando todos os campos do payload:

GET /users
{
"results": [
{
"gender": "male",
"name": {
"title": "mr",
"first": "andy",
"last": "pawlik"
},
"location": {
"street": "meisenweg 164",
"city": "halle (westf.)",
"state": "schleswig-holstein",
"postcode": 81023,
"coordinates": {
"latitude": "-37.4061",
"longitude": "-95.1859"
},
"timezone": {
"offset": "+4:00",
"description": "Abu Dhabi, Muscat, Baku, Tbilisi"
}
},
"email": "andy.pawlik@example.com",
"login": {
"uuid": "0aaaa5ec-ab09-4720-b092-81610a218d55",
"username": "orangecat573",
"password": "111111",
"salt": "OUdLDkdm",
"md5": "64b62e0595cff0e112ed8d08364acc55",
"sha1": "84523e164a58b81f379b7cc86330dcaeeeee47cc",
"sha256": "1d5e441f6d2b5cb98c88741efe4993afe48327f18b6097010ca37f8c9eda3088"
},
"dob": {
"date": "1950-05-19T13:38:56Z",
"age": 69
},
"registered": {
"date": "2009-01-05T22:06:17Z",
"age": 10
},
"phone": "0061-0583330",
"cell": "0171-3132822",
"id": {
"name": "",
"value": null
},
"picture": {
"large": "https://randomuser.me/api/portraits/men/11.jpg",
"medium": "https://randomuser.me/api/portraits/med/men/11.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/men/11.jpg"
},
"nat": "DE"
},
{
"gender": "female",
"name": {
"title": "miss",
"first": "inés",
"last": "van herk"
},
"location": {
"street": "1201 voorstraat",
"city": "ridderkerk",
"state": "noord-brabant",
"postcode": 13096,
"coordinates": {
"latitude": "-34.5827",
"longitude": "-162.2972"
},
"timezone": {
"offset": "-6:00",
"description": "Central Time (US & Canada), Mexico City"
}
},
"email": "inés.vanherk@example.com",
"login": {
"uuid": "36164c34-dcf6-4d13-9eb8-0681227d7648",
"username": "crazylion993",
"password": "genesis1",
"salt": "xGhkkDZB",
"md5": "1b662495a9619402ff22219cb13b31a8",
"sha1": "39fcb31864451020f63275bddbd53c05ff353eeb",
"sha256": "63152369be81139d6c0e457c319416b4b457dc3badf2e6ad702d594fa1fac6cb"
},
"dob": {
"date": "1980-07-15T13:38:56Z",
"age": 38
},
"registered": {
"date": "2008-05-14T16:59:07Z",
"age": 11
},
"phone": "(141)-015-6780",
"cell": "(726)-722-0668",
"id": {
"name": "BSN",
"value": "49767252"
},
"picture": {
"large": "https://randomuser.me/api/portraits/women/13.jpg",
"medium": "https://randomuser.me/api/portraits/med/women/13.jpg",
"thumbnail": "https://randomuser.me/api/portraits/thumb/women/13.jpg"
},
"nat": "NL"
}
]
}

Agora com o uso do Pattern:

GET /users?fields=results(gender,name)
{
"results": [
{
"gender": "male",
"name": {
"title": "mr",
"first": "andy",
"last": "pawlik"
}
},
{
"gender": "female",
"name": {
"title": "miss",
"first": "inés",
"last": "van herk"
}
}
]
}

Podendo ir um pouco mais além trazendo campos aninhados:

GET /users?fields=results(gender,name(first|last))
{
"results": [
{
"gender": "male",
"name": {
"first": "andy",
"last": "pawlik"
}
},
{
"gender": "female",
"name": {
"first": "inés",
"last": "van herk"
}
}
]
}

Perceba que a resposta reduziu em quase 90% em comparação sem o uso do pattern.

Dessa forma podemos fornecer uma resposta mais compacta e exatamente o que o usuário precisa e podendo diminuir o consumo de banda desnecessário no mobile.

espero que ajude.

--

--

Tiago Lima
Comunidade XP

Staff Software Engineer at XP Inc. and Tech Enthusiast