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

Building my first GraphQL server in PHP

Building my first GraphQL server in PHP

Davide Fedrigo

July 10, 2019
Tweet

Other Decks in Programming

Transcript

  1. About me About me Software Developer 2017 Started working with

    GraphQL 2017 Started working with GraphQL
  2. About me About me Software Developer 2017 Started working with

    GraphQL 2017 Started working with GraphQL 2019 Still in love with it! 2019 Still in love with it!
  3. About me About me Software Developer 2017 Started working with

    GraphQL 2017 Started working with GraphQL 2019 Still in love with it! 2019 Still in love with it! linkedin.com/in/davidefedrigo
  4. GraphQL has been released GraphQL has been released only as

    a specification only as a specification
  5. A GraphQL service is created by de ning types and

    elds on those types type Query { randomMovie: Movie } type Movie { id: Int! title: String! }
  6. A GraphQL service is created by de ning types and

    elds on those types Along with functions for each eld on each type type Query { randomMovie: Movie } type Movie { id: Int! title: String! }
  7. GraphQL Query JSON Result { randomMovie { title } }

    { "data": { "randomMovie": { "title": "Back to the Future" } } }
  8. User Input User Input By using the type system, it

    can be predetermined whether a GraphQL query is valid or not
  9. Field Arguments Field Arguments A way for users to parameterize

    queries query { movies(year: 1985) { title(language: "it") } } { "data": { "movies": [ { "title": "Ritorno al Futuro" }, { "title": "I Goonies" } ] } }
  10. Alias Alias query { movies(year:1985) { italianTitle: title(language: "it") englishTitle:

    title(language: "en") } } { "data": { "movies": [ { "italianTitle": "Ritorno al Futuro", "englishTitle": "Back to the Future" }, { "italianTitle": "I Goonies", "englishTitle": "The Goonies" } ] } }
  11. Providing Field Argument Values Providing Field Argument Values As Document

    Literals As Document Literals query { movies(year: 1985) { title(language: "it") } }
  12. Providing Field Argument Values Providing Field Argument Values As Variables

    As Variables query ($year: Int, $language: String) { movies(year: $year) { title(language: $language) } } { "year": 1985, "language": "it" }
  13. Using Enumeration Types Using Enumeration Types A GraphQL enum is

    a special type of scalar that has a de ned, nite set of possible values. query ($language: Language) { movies { title(language: $language) } } { "language": "IT" }
  14. User Input Validation User Input Validation query { actors(matching: 1)

    { name } } { "errors": [ { "message": "Field \"actors\" argument \"matching\" requires type String!, found 1.", "extensions": { "category": "graphql" }, "locations": [ { "line": 2, "column": 20 } ] } ] }
  15. type Query { searchMovies(matching: String!): [ Movie! ]! searchTVSeries(matching: String!):

    [ TVSeries! ]! } query { searchMovies(matching: "game") { title runningTime } searchTVSeries(matching: "game") { title seasons { episodes { runningTime } } } }
  16. Union Types Union Types A GraphQL union type is an

    abstract type that represents a set of speci c concrete types
  17. Union Types Union Types type Query { searchMovies(matching: String!): [

    Movie! ]! searchTVSeries(matching: String!): [ TvSeries! ]! } type Query { search(matching: String!): [ SearchResult! ]! } union SearchResult = Movie | TvSeries
  18. query { search(matching: "game") { ... on Movie { title

    runningTime } ... on TvSeries { title seasons { episodes { runningTime } } } } }
  19. { "data": { "search": [ { "title": "Avengers: Endgame", "runningTime":

    181 }, { "title": "Game of Thrones", "seasons": [ { "episodes": [ { "runningTime": 62 }, { "runningTime": 56 } ] } ] } ] } }
  20. Interfaces Interfaces An Interface is an abstract type that includes

    a certain set of elds that a type must include to implement the interface
  21. Interfaces Interfaces interface BasicSearchResult { title: String! } type Movie

    implements BasicSearchResult { title: String! runningTime: Int } type TvSeries implements BasicSearchResult { title: String! seasons: [Season!]! }
  22. query { search(matching: "game") { title } } } {

    "data": { "search": [ { "title": "Avengers: Endgame" }, { "title": "Game of Thrones" } ] } }
  23. Mutations Mutations type Mutation { rateMovie(movieId: Int!, stars: Int!, comment:

    String): MovieReview } type MovieReview { movie: Movie! stars: Int! comment: String }
  24. Modeling Input Objects Modeling Input Objects type Mutation { rateMovie(movieId:

    Int!, review: ReviewInput!): MovieReview } input ReviewInput { stars: Int! comment: String }
  25. mutation($movieId: Int!, $review: ReviewInput!) { rateMovie(movieId: $movieId, review: $review) {

    movie { title stars } stars } } {"movieId": 1, "review": {"stars": 5, "comment": "Awesome!"}} { "data": { "rateMovie": { "movie": { "title": "Avengers: Endgame", "stars": 4.2 }, "stars": 5, "comment": "Awesome!" } } }
  26. Modeling Input Objects Modeling Input Objects type Mutation { rateMovie(movieId:

    Int!, review: ReviewInput!): Review rateTVSeries(tvSeriesId: Int!, review: ReviewInput!): Review } union Review = MovieReview | TvSeriesReview
  27. Modeling Input Objects Modeling Input Objects type Mutation { rate(id:

    Int!, showType: Show!, review: ReviewInput!): Review } enum Show { MOVIE TVSERIES DOCUMENTARY }
  28. lighthouse-php.com lighthouse-php.com (1031 github stars, 59 contributors) Lighthouse is a

    PHP package that allows you to serve a GraphQL endpoint from your Laravel application
  29. GraphQL Playground GraphQL Playground GraphQL Playground is a graphical, interactive,

    in- browser GraphQL IDE. Integrated with Apollo Server.
  30. Additional Interesting Topics Additional Interesting Topics Errors and exceptions Keep

    resolvers and domain logic separated N+1 problem Authentication
  31. Additional Interesting Topics Additional Interesting Topics Errors and exceptions Keep

    resolvers and domain logic separated N+1 problem Authentication Testing
  32. Additional Interesting Topics Additional Interesting Topics Errors and exceptions Keep

    resolvers and domain logic separated N+1 problem Authentication Testing Custom Scalar Types