GraphQL: change for the best
Anastasiia Lysenko
GraphQL Resolver
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
CreateWishlistResolver
Input types:
CreateWishlistResolver
WishlistSchema
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
CollectionPresenter
AbstractPresenter
ResultHandler
use GraphQL\Error\ClientAware;
GraphQLException
Request Clients
GraphQL Resolver
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
Thank you!
#StandWithUkraine
WSAS: GraphQL: change for the best (en)
By Anastasia Lysenko
WSAS: GraphQL: change for the best (en)
How one can use GraphQL with PHP from scratch. Practical cases and optimization advice.
- 35