====== 3.1 Osnove GraphQL-a ====== GraphQL((GraphQL specifikacija: https://spec.graphql.org/)) omogućuje fleksibilne, precizne upite podataka. ===== Što je GraphQL? ===== GraphQL((GraphQL Foundation: https://graphql.org/)) je jezik upita za API-je i runtime okruženje za izvršavanje tih upita. Za razliku od REST-a((Representational State Transfer: https://de.wikipedia.org/wiki/Representational_State_Transfer)), klijenti mogu zatražiti točno one podatke koji su im potrebni. ===== Endpoint ===== POST /api/v1/dsn/{dbName}/graphql Content-Type: application/json ===== Jednostavan upit ===== { "query": "{ customers { id name country } }" } ===== S parametrima ===== { "query": "{ customers(first: 10, filter: \"Country eq 'DE'\") { id name } }" } ===== Dohvaćanje sheme ===== GET /api/v1/dsn/demo/graphql/schema Vraća GraphQL shemu u SDL((Schema Definition Language: https://graphql.org/learn/schema/)) formatu. ===== Prednosti u odnosu na REST ===== ^ Aspekt ^ REST ^ GraphQL ^ | Endpointi | Mnogo (po resursu) | Jedan | | Over-fetching | Često | Nikad | | Under-fetching | Često (N+1) | Nikad | | Tipska sigurnost | Opcionalno | Ugrađeno | | Dokumentacija | Vanjska (OpenAPI((OpenAPI Specification: https://www.openapis.org/))) | Integrirana (Introspection) | ===== Izvori ===== * [[https://graphql.org/|GraphQL Foundation - Službena stranica]] * [[https://spec.graphql.org/|GraphQL specifikacija]] * [[https://graphql.org/learn/|GraphQL Learn]]