Slide 1

Slide 1 text

Replacing REST with GraphQL Gamechanger for both front- and backend Die Socialisten / Swat.io, @jollife CTO, #proudDadOfAGirl, Life Long Learner

Slide 2

Slide 2 text

Swat.io Social Media Management for Teams — 5 Developers, 7 servers, 2 TB of database — PHP7.2, PostgreSQL, ElasticSearch, Laravel, CakePHP, Queues, Queues, Queues — Lots of high volume customers — 5 Mio API outgoing requests/day — 0.5 Mio incoming API requests/day — Only constant: Change

Slide 3

Slide 3 text

Skills "We <3 to take full advantage of our abilities and talents, as well as further developing them together." Action "We don't default to blindly following the rules. We encourage everyone to give feedback and show initiative at any time."

Slide 4

Slide 4 text

Rest APIs — Pros: — Client <> Server architecture — Stateless — Cons: — Every REST API looks differently — Client can't define the response structure1 — Documentation separated from code — Deprecations not possible 1 Sparse fieldsets might be supported - see Facebook Graph API

Slide 5

Slide 5 text

Rest API v1.0 GET /users POST /users GET /users/:id GET /users/:id/todos

Slide 6

Slide 6 text

Rest API v1.1 GET /users POST /users GET /users/:id GET /users/:id/todos GET /users_with_todos

Slide 7

Slide 7 text

Rest API v1.2 GET /users POST /users GET /users/:id GET /users/:id/todos GET /users_with_todos GET /todos_with_users

Slide 8

Slide 8 text

Facebook @ Scale — Facebook has to support ~ 20 Different App Versions at same time. — All are using different fields / endpoints from their APIs.

Slide 9

Slide 9 text

Facebook @ Scale — Facebook has to support ~ 20 Different App Versions at same time. — All are using different fields / endpoints from their APIs. What the hell?

Slide 10

Slide 10 text

Introducing GraphQL A query language for your API

Slide 11

Slide 11 text

“GraphQL is a query language for APIs […]. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more ….” — https://graphql.org/

Slide 12

Slide 12 text

GraphQL APIs — Pros: — Client <> Server architecture — Stateless — All GraphQL APIs work the same — Consumer defines what fields/relations should be queried and how the response should look like — Combine whatever data you would like — Documentation/Deprecations are part of your schema

Slide 13

Slide 13 text

Rest in practice — Backend is adding new fields to existing endpoints — Existing implementations need to be checked — Backend is adding new endpoints — Frontend needs to wait for new endpoints — Frontend receives unneccessary data GraphQL in practice — Backend defines the schema once — Frontend is asking for all required fields without backend devs adding new endpoints

Slide 14

Slide 14 text

Key terminology — Define your graph as a Schema — Receive Data through Queries — Write Data with Mutations — Learn about your schema with Introspection There's more, of course! — Have a look yourselves! https://graphql.org/learn/ — Fields, Arguments, Aliases, Fragments, Variables, Directives, Subscriptions, …

Slide 15

Slide 15 text

GraphQL Schema Definition Language Example: type BlogPost { title: String! author: User body: String } type User { id: Id! firstName: String lastName: String }

Slide 16

Slide 16 text

GraphQL Query Example 1 Request: POST /graphql { users { id first_name } } Response: { "data": { "users": [{ "id": "7631", "first_name": "Johannes" }] } }

Slide 17

Slide 17 text

GraphQL Query Example 2 Request: POST /graphql { users { id first_name last_name } } Response: { "data": { "users": [{ "id": "7631", "first_name": "Johannes", "last_name": "Nagl" }] } }

Slide 18

Slide 18 text

GraphQL Query Example 3 Demo time! ! — GraphiQL Explorer

Slide 19

Slide 19 text

GraphQL Query Real Life Example In one request, we're fetching — current client — all channels — all users — all permissions of these users — current client member — permissions for all channels — client list — current User — all groups the user owns — last 10 notifications Frontend can, at any time, add even more fields …

Slide 20

Slide 20 text

GraphQL Mutation Example 1 Request: POST /graphql mutation { clientUpdate(id:159, name:"Testing") { id name } } Response: { "data": { "clientUpdate": { "id": "159", "name": "Testing" } }

Slide 21

Slide 21 text

GraphQL Benefits — Type Safety — Documentation — Deprecations

Slide 22

Slide 22 text

GraphQL Type Safety: Request: ! mutation newClient { clientCreate(group_id: 7, name:"New Client Name") { id name } } Response: ! { "data": { "clientCreate": { "id": "3720", "name": "New Client Name" } } }

Slide 23

Slide 23 text

GraphQL Type Safety: mutation newClient { clientCreate(group_id: asdf) }

Slide 24

Slide 24 text

GraphQL Documentation: name' => [ 'type' => Type::nonNull(Type::string()), 'description' => "The name of this channel, usually as provided by the external network. …" ],

Slide 25

Slide 25 text

GraphQL Deprecations: 'client_id' => [ 'type' => Type::nonNull(Type::id()), 'description' => 'The ID of the client this channel belongs to.', 'deprecationReason' => 'Use `channel.client` instead.', ],

Slide 26

Slide 26 text

I can haz bugs in the frontend?

Slide 27

Slide 27 text

No

Slide 28

Slide 28 text

Swat.io Frontend Build Process What's new? — Our new SPA is different. It's not using JavaScript — We use TypeScript now! — You named it! TypeScript uses … Types! — Even better: It uses the GraphQL typings! — The best: The typings are generated automatically!

Slide 29

Slide 29 text

Old Swat.io Frontend Build Process So, that's the deal: 1. A developer implements something 2. Code is reviewed, tested locally and then deployed 3. Developers tend to forget impact in other repos 4. Customers find out in production: 5. Customer talks to Support talks to Developer talks to Developer: Ah, I forgot to change X! 6. Bugfix Time ⏰

Slide 30

Slide 30 text

New Swat.io Frontend Build Process So, here's the deal: 1. A developer implements something 2. Afterwards, the build process is triggered 3. The build process downloads the latest GraphQL schema 4. It uses the generated typings for type checking the whole code base 5. There is no 5. step !

Slide 31

Slide 31 text

Swat.io Frontend Build Process If the build is fine: 1. Instant profit ! If the build fails: 1. Meaningful error messages 2. We fix the bugs before hitting production

Slide 32

Slide 32 text

Demo Time

Slide 33

Slide 33 text

Tooling / Links — GraphiQL: https://github.com/graphql/graphiql (A graphical interactive in-browser GraphQL IDE) — GraphQL VS Code extension: https://github.com/ prisma/vscode-graphql (autocompletion, go-to definition, syntax highlighting) — GraphQL PHPStorm plugin: https:// plugins.jetbrains.com/plugin/8097-js-graphql (auto-completion, go-to definition, syntax highlighting)

Slide 34

Slide 34 text

Links — https://nicolasdular.com/blog/2018/05/06/rest-in-peace- welcome-to-graphql/ — https://facebook.github.io/relay/ (Facebook Javascript Framework for React-Apps) — https://www.apollographql.com/ (Server, Client for JavaScript) — https://www.onegraph.com/ (Connect SalesForce, Stripe, GitHub, Intercom, Slack, Paypal … into your GraphQL Service) — https://github.com/Folkloreatelier/laravel-graphql (GraphQL Package for Laravel)

Slide 35

Slide 35 text

Questions? Answers!

Slide 36

Slide 36 text

Questions? Answers! Of course, we're looking for new team members!