Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Building my first GraphQL server in PHP
Search
Davide Fedrigo
July 10, 2019
Programming
1
77
Building my first GraphQL server in PHP
PHP User Group Milan
https://www.meetup.com/MilanoPHP/events/262097221/
Davide Fedrigo
July 10, 2019
Tweet
Share
Other Decks in Programming
See All in Programming
知っているようで知らない"rails new"の世界 / The World of "rails new" You Think You Know but Don't
luccafort
PRO
1
180
Deep Dive into Kotlin Flow
jmatsu
1
360
ProxyによるWindow間RPC機構の構築
syumai
3
1.2k
Namespace and Its Future
tagomoris
6
710
AWS発のAIエディタKiroを使ってみた
iriikeita
1
190
AIを活用し、今後に備えるための技術知識 / Basic Knowledge to Utilize AI
kishida
22
5.8k
FindyにおけるTakumi活用と脆弱性管理のこれから
rvirus0817
0
530
GitHubとGitLabとAWS CodePipelineでCI/CDを組み比べてみた
satoshi256kbyte
4
240
go test -json そして testing.T.Attr / Kyoto.go #63
utgwkk
3
310
Compose Multiplatform × AI で作る、次世代アプリ開発支援ツールの設計と実装
thagikura
0
170
そのAPI、誰のため? Androidライブラリ設計における利用者目線の実践テクニック
mkeeda
2
1.8k
個人軟體時代
ethanhuang13
0
330
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
54
11k
Designing for humans not robots
tammielis
253
25k
Visualization
eitanlees
148
16k
[RailsConf 2023] Rails as a piece of cake
palkan
57
5.8k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
61k
Fireside Chat
paigeccino
39
3.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Unsuck your backbone
ammeep
671
58k
Become a Pro
speakerdeck
PRO
29
5.5k
Documentation Writing (for coders)
carmenintech
74
5k
Building Adaptive Systems
keathley
43
2.7k
Transcript
Building my first GraphQL Building my first GraphQL server in
PHP server in PHP @davidefedrigo
About me About me Software Developer
About me About me Software Developer 2017 Started working with
GraphQL 2017 Started working with GraphQL
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!
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
Agenda Agenda Meet GraphQL From theory to practice PHP libraries
Additional topics
Disclaimer Disclaimer Never done it before! (using PHP)
GraphQL has been released GraphQL has been released only as
a specification only as a specification
A GraphQL service is created by de ning types and
elds on those types type Query { randomMovie: Movie } type Movie { id: Int! title: String! }
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! }
GraphQL Query JSON Result { randomMovie { title } }
{ "data": { "randomMovie": { "title": "Back to the Future" } } }
Client-Server Interaction Client-Server Interaction
GraphQL Advantages GraphQL Advantages Declarative Data Fetching Strongly Typed Expressiveness
GraphQL Disadvantages GraphQL Disadvantages Instrumentation Caching
Coding Session Coding Session Minimal Example
User Input User Input By using the type system, it
can be predetermined whether a GraphQL query is valid or not
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" } ] } }
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" } ] } }
Providing Field Argument Values Providing Field Argument Values As Document
Literals As Document Literals query { movies(year: 1985) { title(language: "it") } }
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" }
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" }
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 } ] } ] }
Coding Session Coding Session User Input Examples
Abstract Types Abstract Types
type Query { searchMovies(matching: String!): [ Movie! ]! searchTVSeries(matching: String!):
[ TVSeries! ]! }
type Query { searchMovies(matching: String!): [ Movie! ]! searchTVSeries(matching: String!):
[ TVSeries! ]! } query { searchMovies(matching: "game") { title runningTime } searchTVSeries(matching: "game") { title seasons { episodes { runningTime } } } }
Union Types Union Types A GraphQL union type is an
abstract type that represents a set of speci c concrete types
Union Types Union Types type Query { searchMovies(matching: String!): [
Movie! ]! searchTVSeries(matching: String!): [ TvSeries! ]! } type Query { search(matching: String!): [ SearchResult! ]! } union SearchResult = Movie | TvSeries
query { search(matching: "game") { ... on Movie { title
runningTime } ... on TvSeries { title seasons { episodes { runningTime } } } } }
{ "data": { "search": [ { "title": "Avengers: Endgame", "runningTime":
181 }, { "title": "Game of Thrones", "seasons": [ { "episodes": [ { "runningTime": 62 }, { "runningTime": 56 } ] } ] } ] } }
Interfaces Interfaces An Interface is an abstract type that includes
a certain set of elds that a type must include to implement the interface
Interfaces Interfaces interface BasicSearchResult { title: String! } type Movie
implements BasicSearchResult { title: String! runningTime: Int } type TvSeries implements BasicSearchResult { title: String! seasons: [Season!]! }
query { search(matching: "game") { title } } } {
"data": { "search": [ { "title": "Avengers: Endgame" }, { "title": "Game of Thrones" } ] } }
Coding Session Coding Session Abstract Types Examples
Operations Operations
Mutations Mutations type Mutation { rateMovie(movieId: Int!, stars: Int!, comment:
String): MovieReview } type MovieReview { movie: Movie! stars: Int! comment: String }
Modeling Input Objects Modeling Input Objects type Mutation { rateMovie(movieId:
Int!, review: ReviewInput!): MovieReview } input ReviewInput { stars: Int! comment: String }
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!" } } }
Modeling Input Objects Modeling Input Objects type Mutation { rateMovie(movieId:
Int!, review: ReviewInput!): Review rateTVSeries(tvSeriesId: Int!, review: ReviewInput!): Review } union Review = MovieReview | TvSeriesReview
Modeling Input Objects Modeling Input Objects type Mutation { rate(id:
Int!, showType: Show!, review: ReviewInput!): Review } enum Show { MOVIE TVSERIES DOCUMENTARY }
PHP Libraries PHP Libraries
webonyx/graphql-php webonyx/graphql-php (3175 github stars, 68 contributors) Symfony: overblog/GraphQLBundle Laravel:
rebing/graphql-laravel API Platform relies on it.
youshido-php/GraphQL youshido-php/GraphQL (698 github stars, 34 contributors) Symfony: youshido-php/GraphQLBundle Looking
for Maintainers!
digiaonline/graphql-php digiaonline/graphql-php (190 github stars, 9 contributors)
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
GraphQL Clients GraphQL Clients
GraphiQL GraphiQL
GraphQL Playground GraphQL Playground GraphQL Playground is a graphical, interactive,
in- browser GraphQL IDE. Integrated with Apollo Server.
Insomnia Insomnia
Postman Postman
Additional Interesting Topics Additional Interesting Topics
Additional Interesting Topics Additional Interesting Topics Errors and exceptions
Additional Interesting Topics Additional Interesting Topics Errors and exceptions Keep
resolvers and domain logic separated
Additional Interesting Topics Additional Interesting Topics Errors and exceptions Keep
resolvers and domain logic separated N+1 problem
Additional Interesting Topics Additional Interesting Topics Errors and exceptions Keep
resolvers and domain logic separated N+1 problem Authentication
Additional Interesting Topics Additional Interesting Topics Errors and exceptions Keep
resolvers and domain logic separated N+1 problem Authentication Testing
Additional Interesting Topics Additional Interesting Topics Errors and exceptions Keep
resolvers and domain logic separated N+1 problem Authentication Testing Custom Scalar Types
Questions? Questions?
Thank you! Thank you! github.com/davidefedrigo/graphql-pug