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

Introduction to GraphQL - PHP UK 2018

Michael C.
February 15, 2018

Introduction to GraphQL - PHP UK 2018

GraphQL is rapidly becoming a high-profile alternative to RESTful APIs. This talk will go into its advantages, disadvantages, and when it’s appropriate to use as well as how we can interact with it, both as an API consumer, and API producer.

Michael C.

February 15, 2018
Tweet

More Decks by Michael C.

Other Decks in Programming

Transcript

  1. @MICHAELCULLUMUK GRAPHS USER ORGANISATION REPOSITORY ORGANISATION REPOSITORY COMMIT COMMIT COMMIT

    USER USER USER COMMIT COMMIT COMMIT USER USER USER USER USER USER
  2. @MICHAELCULLUMUK REST API GET /repos/:id
 GET /orgs/
 GET /orgs/:id/
 GET

    /orgs/:id/repos/
 GET /orgs/:id/repos/:id/
 POST /orgs/ 200 OK
 200 OK
 200 OK
 200 OK
 200 OK
 201 Created
  3. @MICHAELCULLUMUK REST API { "_links": { "self": “http://api.github.com/orgs/20/repos“ }, "limit":

    100, "results": [ { "_expandable": { "commits": “/repos/105/commits“, "collaborators": "/repos/105/collaborators", }, "id": "3965071", "title": “symfony/symfony", "org": “Symfony", ... }, { ... } ], "size": 2, "start": 0 }
  4. @MICHAELCULLUMUK { me { name } } { "data": {

    "me": { "name": "Michael Cullum" } } } Request (Graph QL) Response (JSON) BASIC PARAMETER FETCHING
  5. @MICHAELCULLUMUK { me { name company } } { "data":

    { "me": { "name": "Michael Cullum", "company": "SamKnows" } } } Request (Graph QL) Response (JSON) BASIC PARAMETER FETCHING
  6. @MICHAELCULLUMUK { me { name repos { name } }

    } { "data": { "me": { "name": "Michael Cullum", "repos": [ { "name": "michaelcullum.com" }, { "name": "my-cool-library" } ] } } } Request (Graph QL) Response (JSON) BASIC PARAMETER FETCHING
  7. @MICHAELCULLUMUK { user(login: "michaelcullum") { name } } Request (Graph

    QL) Response (JSON) ARGUMENTS { "data": { “user": { "name": "Michael Cullum" } } }
  8. @MICHAELCULLUMUK { me { name company } } { "data":

    { "me": { "name": "Michael Cullum", "company": "SamKnows" } } } Request (Graph QL) Response (JSON) RECAP
  9. @MICHAELCULLUMUK { user { name repos { name } }

    } { "data": { “user”: { "name": "Michael Cullum", "repos": [ { "name": "michaelcullum.com" }, { "name": "my-cool-library" } ] } } } Request (Graph QL) Response (JSON) RECAP
  10. @MICHAELCULLUMUK { fabien: user(login:"fabpot") { name } andrew: user(login:”andrew") {

    name } } Request (Graph QL) Response (JSON) ALIASES { "data": { "fabien": { "name": "Fabien Potencier" }, “andrew": { "name": "Andrew Nesbitt" } } }
  11. @MICHAELCULLUMUK { fabien: user(login: "fabpot") { name email company }

    michael: user(login: "michaelcullum") { name email company } } Without Fragments With Fragments FRAGMENTS { fabien: user(login: "fabpot") { ...userdetails } michael: user(login: "michaelcullum") { ...userdetails } } fragment userdetails on User { name email company }
  12. @MICHAELCULLUMUK QUERY OPERATIONS query getUserAndCompany ($login: String!) { user(login: $login)

    { name company isHireable } } {"login": "michaelcullum"} Request (Graph QL) Variables
  13. @MICHAELCULLUMUK QUERY OPERATIONS query getUserAndCompany ($login: String!) { user(login: $login)

    { name company isHireable @include(if: $hireable) } } { "login": "michaelcullum", “hireable”: true } Request (Graph QL) Variables
  14. @MICHAELCULLUMUK MUTATION VS QUERYING mutation CreateRepoForUser($user: User!, $repo: RepoInput!) {

    createRepo(user: $user, repo: $repo) { name Repo } } { "user": "michaelcullum", "repo": { "name": “mailing-lib”, "description": “A library for emails” } }
  15. @MICHAELCULLUMUK TYPES $userType = new ObjectType( [ 'name' => 'User',

    'description' => 'Our blog visitor', 'fields' => [ 'firstName' => [ 'type' => Type::string(), 'description' => 'User first name', ], 'email' => Type::string(), ], 'resolve' => function() { return [ 'id' => 1, 'title' => 'Example blog post', 'authorId' => 1 ]; } ] );
  16. @MICHAELCULLUMUK ‣ REST AND GRAPHQL ARE TOTALLY DIFFERENT ‣ GRAPHQL

    ISN'T A MAGIC BULLET, NOR IS IT “BETTER" ‣ YOU CAN DEFINITELY USE BOTH AT THE SAME TIME ‣ GRAPHQL IS DOPE IF USED FOR THE RIGHT THING Phil Sturgeon