This is a simple example of an API build with Graphql engine to show the basic concepts of it.
tba
Query:
{
products {
id
name
price
}
}
Result:
{
"data": {
"products": [
{
"id": 1,
"name": "Mountain Walkers",
"price": 219.5
},
{
"id": 2,
"name": "Blue Racer",
"price": 350
},
{
"id": 3,
"name": "Orange Demon",
"price": 450
}
]
}
}
Support to find item by id:
query($productId: ID!){
product(id: $productId){
name
price
}
}
{
"data": {
"product": {
"id": 1,
"name": "Mountain Walkers",
"price": 219.5
}
}
}
tbc
Let's imagine a query that wants all products and their reviews.
The app fetchs all the products. Now, for each product the app fetchs their review (using the product ID). This process will create an amount of queries to the database equal to 2 times the amount of products in the database.
The DataLoader concept is basicaly an intermediary between the app and the database that caches previous results removing the need to query so many times the database for reviews. In this implementation the DL is basicaly a dictionary or more specificaly a ILookup<int, ProductReview>>
. The type ILookup
is a key-value storage with search features inside the .NET Core framework.
The implementation can be seen inside the class ProductGraphType.cs
in the resolver for the field reviews
.
For a mutation such as one to create a ProductReview resource and in the end show us the id of the new resource.
mutation ($review: productReviewInput!) {
createReview(review: $review){
id
}
}
We could then create a new one by supplying an input such as:
{
"review": {
"title": "My First Review!!!",
"review": "This is the content of my First review.",
"productId": 3
}
}
The expected result would be:
{
"data": {
"createReview": {
"id": 5
}
}
}