Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GraphQL in 7 days

Alco
August 08, 2022

GraphQL in 7 days

This is the English version of my talk about how I went from MySQL DB to GraphQL API in PHP.

Alco

August 08, 2022
Tweet

More Decks by Alco

Other Decks in Programming

Transcript

  1. How I went from MySQL DB to GraphQL API in

    7 days Ricardo (Alco) Alcocer [email protected] · https://alco.rocks
  2. - 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
  3. Agenda - What is GraphQL? - A practical example -

    The server-side implementation in vanilla PHP - Live Demo
  4. 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
  5. They noticed that the existing tools didn’t really solve their

    problem https://www.youtube.com/watch?v=783ccP__No8
  6. 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
  7. 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/….
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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