====== 3.1 Fondamenti GraphQL ======
GraphQL((Specifica GraphQL: https://spec.graphql.org/)) permette query dati flessibili e precise.
===== Cos'è GraphQL? =====
GraphQL((GraphQL Foundation: https://graphql.org/)) è un linguaggio di query per API e un runtime per l'esecuzione di queste query. A differenza di REST((Representational State Transfer: https://it.wikipedia.org/wiki/Representational_State_Transfer)) i client possono richiedere esattamente i dati di cui hanno bisogno.
===== Endpoint =====
POST /api/v1/dsn/{dbName}/graphql
Content-Type: application/json
===== Query Semplice =====
{
"query": "{ customers { id name country } }"
}
===== Con Parametri =====
{
"query": "{ customers(first: 10, filter: \"Country eq 'DE'\") { id name } }"
}
===== Recuperare Schema =====
GET /api/v1/dsn/demo/graphql/schema
Restituisce lo schema GraphQL in formato SDL((Schema Definition Language: https://graphql.org/learn/schema/)).
===== Vantaggi rispetto a REST =====
^ Aspetto ^ REST ^ GraphQL ^
| Endpoint | Molti (per risorsa) | Uno |
| Over-fetching | Frequente | Mai |
| Under-fetching | Frequente (N+1) | Mai |
| Type safety | Opzionale | Integrata |
| Documentazione | Esterna (OpenAPI((OpenAPI Specification: https://www.openapis.org/))) | Integrata (Introspection) |
===== Fonti =====
* [[https://graphql.org/|GraphQL Foundation - Sito Ufficiale]]
* [[https://spec.graphql.org/|Specifica GraphQL]]
* [[https://graphql.org/learn/|GraphQL Learn]]