Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
78
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
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
160
マスタデータ問題、マイクロサービスでどう解くか
kts
0
120
JETLS.jl ─ A New Language Server for Julia
abap34
2
440
堅牢なフロントエンドテスト基盤を構築するために行った取り組み
shogo4131
8
2.5k
從冷知識到漏洞,你不懂的 Web,駭客懂 - Huli @ WebConf Taiwan 2025
aszx87410
2
3k
Rubyで鍛える仕組み化プロヂュース力
muryoimpl
0
160
Cap'n Webについて
yusukebe
0
150
Graviton と Nitro と私
maroon1st
0
130
AIコーディングエージェント(Manus)
kondai24
0
210
リリース時」テストから「デイリー実行」へ!開発マネージャが取り組んだ、レガシー自動テストのモダン化戦略
goataka
0
140
これならできる!個人開発のすゝめ
tinykitten
PRO
0
130
メルカリのリーダビリティチームが取り組む、AI時代のスケーラブルな品質文化
cloverrose
2
360
Featured
See All Featured
Visualization
eitanlees
150
16k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
260
The Cult of Friendly URLs
andyhume
79
6.7k
Context Engineering - Making Every Token Count
addyosmani
9
550
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
0
250
Music & Morning Musume
bryan
46
7k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.1k
Discover your Explorer Soul
emna__ayadi
2
1k
The SEO Collaboration Effect
kristinabergwall1
0
310
How to make the Groovebox
asonas
2
1.8k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
0
2.2k
Writing Fast Ruby
sferik
630
62k
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