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
74
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
はてなにおける CSS Modules、及び CSS Modules に足りないもの / CSS Modules in Hatena, and CSS Modules missing parts
mizdra
6
880
Rubyでたのしむクリエイティブコーディング/Enjoy Creative coding with Ruby
chobishiba
1
170
Zero Waste, Radical Magic, and Italian Graft – Quarkus Efficiency Secrets
hollycummins
0
230
ONE WEDGE_company_guide
1wedge_one
0
440
FigmaとPHPで作る1ミリたりとも表示崩れしない最強の帳票印刷ソリューション
ttskch
43
18k
エンターテイメント業界で利用されるAWS
demuyan
0
210
#phpcon_odawara オープン・クローズドなテストフィクスチャを求めて / open closed test fixtures
77web
3
220
StoreKit2によるiOSのアプリ内課金のリニューアル
kangnux
0
110
Build with AI 2024 Seoul - 제로부터 시작하는 Flutter with Gemini 생활 - 박제창
itsmedreamwalker
0
200
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
190
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
"config" ってなんだ? / What is "config"?
okashoi
0
240
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
27
6.3k
Bash Introduction
62gerente
604
210k
4 Signs Your Business is Dying
shpigford
175
21k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
1
1.3k
Why Our Code Smells
bkeepers
PRO
331
56k
Making the Leap to Tech Lead
cromwellryan
123
8.5k
A Tale of Four Properties
chriscoyier
150
22k
Build your cross-platform service in a week with App Engine
jlugia
225
17k
Writing Fast Ruby
sferik
620
60k
Reflections from 52 weeks, 52 projects
jeffersonlam
344
19k
Six Lessons from altMBA
skipperchong
20
3k
Fashionably flexible responsive web design (full day workshop)
malarkey
398
65k
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