GraphQL

Stop wRESTling with your API. Use GraphQL

Gliederung

GraphQL

Motivation

Praxis

Theorie

Ausblick

Einfache Todo Application

  • Aufgabe (Task)

    • Notizen

    • Deadline

  • Kategorie (Category)

Title Category Place Date State
Buying apples shopping Walmart 2018-03-10 Done
Buying tv shopping Pending
Program graphql university 2018-03-08 Done

Frontend

Backend

?

GET myapi.com/v1/todo/1
{
  "title": "Buying apples",
  "note": "Buying apples at Walmart",
  "category": "shopping",
  "date": 1524123265,
  "place": "Walmart",
  "sameCategory": [
     2
    ],
  "sameDate": [
     3
    ]
}
GET myapi.com/task_with_images/2
GET myapi.com/task_with_images_and_date/2
GET myapi.com/task_with_images_and_date_and_description/2
GET myapi.com/task_with_images_and_description_and_date_and_owner/2
GET myapi.com/task?include=image,description,date,owner,created_at

Dokumentation

Probleme bei REST

  • Custom Endpoints vs. Over-Fetching
  • Dokumentation
  • Versionierung
  • Backend-Driven-Development

Was ist GraphQL?

  • Abfragesprache für API
  • 2015 von Facebook veröffentlicht
  • "SQL für API's"
  • es gibt nur einen Endpoint
  • basiert auf einem Graphen

gleiche

Kategorie

gleiche

Kategorie

gleiches

Datum

gleiches

Datum

hat

hat

hat

hat

Kategorie

Kategorie

Aufgabe

Aufgabe

Aufgabe

Aufgabe

Aufgabe

Alternative zu einer

Wie funktioniert GraphQL?

Abfrage

Query vs Mutation

Vergleichbar mit den HTTP-Methods von REST

Type System

  • Vergleichbar mit Objekten
  • primitive Datentypen
  • Listen
  • komplexe (selbst entwickelte Datentypen)

Type System

  • jede Ressource muss in einem Type definiert werden

type Query {

  category (title: String): [task]

  tasks (state: Boolean): [task]

  task (id: ID): task

  date (from: Date, to: Date): [task]

}

type Task {

  _id: ID!

  title: String

  state: Boolean

  notes: String

  date: Date

  place: String

  category: String

  notes: String

  sameDate: [Task]

  samePlace: [Task]

  sameCategory: [Task]

}

Text

Versionierung

Vorteile von GraphQL

  • einfache Auswahl der gewünschten Daten
  • Kein Overfetching
  • Objektorientierung
  • GraphQL Schema als Beschreibungsdatei der API
  • Dokumentation wird automatisch generiert

Nachteile von GraphQL

  • Aufwandskalkulation schwierig (Keine Requests/day)
  • Dateiverwaltung (Upload und Download)
  • Caching ist schwierig (viele unterschiedliche Queries)
  • Fehlende Standards für z.B. Paging

Dinge die nicht besprochen wurden

  • Input Fields
  • Authentication über JWT's
  • MIME Types (Bilder, Audio, etc.)
  • Fragmente
  • Exceptions
  • Security
  • Caching
  • Subscription

Ausblick

  • erste Unternehmen verwenden auf GraphQL

Ausblick

  • hohes Potenzial in vielen Bereichen REST abzulösen
  • hoher Nutzen in
    • Web-Apps
    • Mobile Anwendungen
    • Open API

Praxis

DevCamp GraphQL

By finally

DevCamp GraphQL

  • 775