GraphQL Resolver
PHP Ports for GraphQL
1. Design and develop resolver for queries, process schema and presenters
2. Choose between pagination options and implement chosen one
3. Create ResultHandler and ErrorHandler (who said it would be easy)
4. Create endpoint
5. Send a query
TODO list:
Resolver.php
CreateWishlistResolver.php
Input types:
CreateWishlistResolver.php
WishlistSchema.php
wishlists (first: 10, after: "cursor") {
edges {
cursor,
node {
id,
name,
items {
id,
type
}
}
},
totalCount,
pageInfo {
startCursor,
endCursor,
hasNextPage,
hasPreviousPage
}
}
Cursor
Offset
wishlists (first: $first, offset: $offset) {
edges {
id,
name,
items {
id,
type
}
},
pageInfo {
first,
offset,
total,
hasNextPage,
hasPreviousPage
}
}
NodePresenter.php
CollectionPresenter.php
AbstractPresenter.php
ResultHandler.php
use GraphQL\Error\ClientAware;
GraphQLException.php
Request Clients
Controller.php
CruiseType {
reservationNumber,
checkInTime,
departureTime,
departureCity,
departurePort,
cruiseShip,
cruiseLine,
arrivalTime,
checkOutTime,
arrivalCity,
arrivalPort,
stops,
bookingClass,
additionalInformation
}
RailwayType {
ticketNumber,
departureTime,
departureCity,
trainNumber,
trainPlace,
arrivalTime,
arrivalCity,
bookingClass
}
BookingType {
id
}
BookingType {
… on CruiseType {
reservationNumber,
checkInTime,
departureTime,
departureCity,
departurePort,
cruiseShip,
cruiseLine,
arrivalTime,
checkOutTime,
arrivalCity,
arrivalPort,
stops,
bookingClass,
additionalInformation
},
… on RailwayTripType {
ticketNumber,
departureTime,
departureCity,
trainNumber,
trainPlace,
arrivalTime,
arrivalCity,
bookingClass
}
}
UnionType
'bookingType' =>
$this->getBookingUnionType('BookingTypeInterfaceType')
SchemaDecorator.php
CreateWishlistResolver.php
File Upload
'args' => [ 'file' => new UploadType()]
FileUploaderResolver.php
mutation(
$file: Upload
) {
uploadYourFileHere(
file: $file
) {
File {
path,
name
}
}
FileUpload Mutation Example
Don't query unreliable data! You don't need it.
Wishlist {
id,
name
}
Wishlist {
id,
name,
items {
id,
type,
name,
category,
price,
currency
}
}
Optimization
No recursions! You can do better than this!
Author {
name,
books {
title,
authors {
name,
books
}
}
}
wishlists (first: $first, offset: $offset) { #fetches wishlists (N queries)
edges {
id,
name,
items { #fetches items for each wishlist (M queries per wishlist)
id,
type
}
},
pageInfo {
first,
offset,
total,
hasNextPage,
hasPreviousPage
}
} # therefore M*N queries
N + 1
Solutions
NoSQL
Cache
GraphQL: change for the best
By Anastasia Lysenko
GraphQL: change for the best
How one can use GraphQL with PHP from scratch. Practical cases and optimization advice.
- 601