Slide 1

Slide 1 text

How I went from MySQL DB to GraphQL API in 7 days Ricardo (Alco) Alcocer [email protected] · https://alco.rocks

Slide 2

Slide 2 text

- Originally from Puerto Rico 󰐦 - Building technology solutions for over 25 years - 4x Founder - Former Director of Developer Relations for Contentful - Former Director of Developer Relations and Training for Appcelerator (now tidev.io) - Author of 4.5 books - Curious Computer Scientist always looking for challenges and opportunities to bring value - Instrumental Guitarist 🎸 (https://alco.ws/spotify) Hi there! I’m Alco

Slide 3

Slide 3 text

Agenda - What is GraphQL? - A practical example - The server-side implementation in vanilla PHP - Live Demo

Slide 4

Slide 4 text

What problem is GraphQL attempting to solve?

Slide 5

Slide 5 text

RESTful always feels like the first option for APIs - Mature - It’s an architectural style - Based on functionality of the HTTP Protocol - Stateless - Uses HTTP Verbs to define actions (GET, POST, PUT, DELETE) - It was designed to solve the problem of accessing data resources - REST came to replace technologies like S.O.A.P. and others

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Facebook thought so too… https://techcrunch.com/2012/09/11/mark-zuckerberg-our-biggest-mistake-with-mobile-was-betting-too-much-on-html5

Slide 8

Slide 8 text

They noticed that the existing tools didn’t really solve their problem https://www.youtube.com/watch?v=783ccP__No8

Slide 9

Slide 9 text

They created GraphQL, a query language for APIs - It’s not a product or a technology, it is a technical specification - Creates a new paradigm for having conversations with your data sources - It offers libraries for virtually any programming language - While REST was designed to solve the “server” problem, GraphQL was designed to solve the problem of who is consuming the data resources - A single request can handle multiple operations - It is self-documenting - It is divided into two main areas: - The GraphQL Server - Implements data definitions, ORMs, data relationships, etc. - The GraphQL Client - Allows for a rich JSON-like language for building requests that return predictable result sets

Slide 10

Slide 10 text

Let’s look at a practical example

Slide 11

Slide 11 text

MEMBERS TABLE MESSAGES TABLE

Slide 12

Slide 12 text

MEMBERS TABLE MESSAGES TABLE

Slide 13

Slide 13 text

In REST-land GET https://api.somedomain.com/v1/members GET https://api.somedomain.com/v1/member/12345 GET https://api.somedomain.com/v1/messages GET https://api.somedomain.com/v1/messages/12345 ENDPOINTS DEFINED BEFOREHAND POST https://api.somedomain.com/v1/…. PUT https://api.somedomain.com/v1/…. DELETE https://api.somedomain.com/v1/….

Slide 14

Slide 14 text

In GraphQL-land POST https://api.somedomain.com/gql There’s only ONE endpoint query{ members(first: 1000, skip: 0) { id fname lname email interests active joined } } { "data": { "members": [ { "id": 17, "fname": "Ricardo", "lname": "Alcocer", "email": "[email protected]", "interests": "lifehacking,music", "active": "1", "joined": "2021-07-14 12:51:57" }, { "id": 18, "fname": "Vic", REQUEST RESPONSE

Slide 15

Slide 15 text

In GraphQL-land POST https://api.somedomain.com/gql There’s only ONE endpoint query{ members(first: 1000, skip: 0) { id fname lname email interests active joined } } { "data": { "members": [ { "id": 17, "fname": "Ricardo", "lname": "Alcocer", "email": "[email protected]", "interests": "lifehacking,music", "active": "1", "joined": "2021-07-14 12:51:57" }, { "id": 18, "fname": "Vic", REQUEST RESPONSE

Slide 16

Slide 16 text

In GraphQL-land POST https://api.somedomain.com/gql There’s only ONE endpoint query{ members(first: 1000, skip: 0) { id fname lname email interests active joined } } { "data": { "members": [ { "id": 17, "fname": "Ricardo", "lname": "Alcocer", "email": "[email protected]", "interests": "lifehacking,music", "active": "1", "joined": "2021-07-14 12:51:57" }, { "id": 18, "fname": "Vic", REQUEST RESPONSE 15

Slide 17

Slide 17 text

In GraphQL-land POST https://api.somedomain.com/gql There’s only ONE endpoint mutation{ editMember(id: 2508 active:0){ id } } { "data": { "editMember": { "id": 2508 } } } REQUEST RESPONSE

Slide 18

Slide 18 text

In GraphQL-land POST https://api.somedomain.com/gql There’s only ONE endpoint mutation{ editMember(id: 2508 active:0){ id } } { "data": { "editMember": { "id": 2508 } } } REQUEST RESPONSE

Slide 19

Slide 19 text

The server-side implementation

Slide 20

Slide 20 text

DATA MODELS

Slide 21

Slide 21 text

Main App Entry point index.php main.php

Slide 22

Slide 22 text

Main App Entry point index.php main.php

Slide 23

Slide 23 text

Main App Entry point index.php main.php

Slide 24

Slide 24 text

Main App Entry point index.php main.php

Slide 25

Slide 25 text

Objects.php

Slide 26

Slide 26 text

Objects.php

Slide 27

Slide 27 text

Objects.php

Slide 28

Slide 28 text

Query.php

Slide 29

Slide 29 text

Query.php

Slide 30

Slide 30 text

Mutation.php

Slide 31

Slide 31 text

Let’s look at a live demo!

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Summary - It is a Query Language for your API - It’s not a technology, it’s a specification - Offers client and server libraries for multiple languages - It’s Database-agnostic - Designed for client-side flexibility - Optimized for Mobile use

Slide 34

Slide 34 text

Thank you Questions? Comments? Find this code on Github https://github.com/ricardoalcocer/php_graphql