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
AI駆動で0→1をやって見えた光と伸びしろ
passion0102
1
820
Claude Agent SDK を使ってみよう
hyshu
0
1.4k
SwiftDataを使って10万件のデータを読み書きする
akidon0000
0
240
Software Architecture
hschwentner
6
2.3k
なぜGoのジェネリクスはこの形なのか? - Featherweight Goが明かす設計の核心
qualiarts
0
250
Range on Rails ―「多重範囲型」という新たな選択肢が、複雑ロジックを劇的にシンプルにしたワケ
rizap_tech
0
6.8k
TransformerからMCPまで(現代AIを理解するための羅針盤)
mickey_kubo
7
5k
テーブル定義書の構造化抽出して、生成AIでDWH分析を試してみた / devio2025tokyo
kasacchiful
0
280
What's new in Spring Modulith?
olivergierke
1
170
バッチ処理を「状態の記録」から「事実の記録」へ
panda728
PRO
0
190
CSC509 Lecture 07
javiergs
PRO
0
240
他言語経験者が Golangci-lint を最初のコーディングメンターにした話 / How Golangci-lint Became My First Coding Mentor: A Story from a Polyglot Programmer
uma31
0
340
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1371
200k
Practical Orchestrator
shlominoach
190
11k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
34
2.3k
The Invisible Side of Design
smashingmag
302
51k
Typedesign – Prime Four
hannesfritz
42
2.8k
RailsConf 2023
tenderlove
30
1.3k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.7k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Embracing the Ebb and Flow
colly
88
4.9k
Visualization
eitanlees
149
16k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
23
1.5k
Leading Effective Engineering Teams in the AI Era
addyosmani
7
600
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