-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f51f5c
commit 09147ff
Showing
7 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
query getErrorWithData { | ||
getProducts { | ||
name | ||
price | ||
} | ||
deals { | ||
id | ||
title | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { ApolloError } from "apollo-server-micro"; | ||
import { Field, ObjectType, Query, Resolver } from "type-graphql"; | ||
|
||
@ObjectType() | ||
class Product { | ||
@Field() | ||
name: string; | ||
|
||
@Field({ nullable: true }) | ||
price: number; | ||
} | ||
|
||
@Resolver(Product) | ||
export class ProductResolver { | ||
private products = [ | ||
{ name: "Product A", price: 100 }, | ||
{ name: "Product B", price: 200 }, | ||
{ name: "Product C", price: 200 }, | ||
{ name: "Product D", price: 200 }, | ||
{ name: "Product E", price: 200 }, | ||
{ name: "Product F", price: 200 }, | ||
]; | ||
|
||
@Query(() => [Product!]) | ||
async getProducts(): Promise<Product[]> { | ||
return this.products.map((product) => { | ||
if (Math.random() < 0.3) { | ||
throw new ApolloError("Custom error from products"); | ||
} | ||
return product; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import ExampleExplanation from "@/components/ExampleExplanation"; | ||
import { useGetErrorWithDataQuery } from "@/generated/graphql"; | ||
|
||
const EXPLANATION = { | ||
title: "Error handling - Partial Data", | ||
description: | ||
"Error handling pokud příjdou pouze - partial data a je potřeba zobrazit data pouze, které příjdou.", | ||
}; | ||
|
||
export default function ErrorHandlingPage() { | ||
const { data, error, loading } = useGetErrorWithDataQuery({ | ||
errorPolicy: "all", | ||
}); | ||
|
||
if (loading) { | ||
return <div>Načítám...</div>; | ||
} | ||
|
||
if (data?.deals && data?.getProducts) { | ||
return ( | ||
<main className="max-w-5xl mx-auto px-3"> | ||
<ExampleExplanation | ||
title={EXPLANATION.title} | ||
description={EXPLANATION.description} | ||
/> | ||
<div className="py-10"> | ||
{data.deals.map((deal) => ( | ||
<div key={deal.id}> | ||
<h1>{deal.title}</h1> | ||
</div> | ||
))} | ||
</div> | ||
<div className="py-10"> | ||
{data.getProducts.map((product) => ( | ||
<div key={product.name}> | ||
<h1>{product.name}</h1> | ||
</div> | ||
))} | ||
</div> | ||
</main> | ||
); | ||
} | ||
|
||
if (error) { | ||
console.log("Error:", error); | ||
} | ||
|
||
return ( | ||
<main className="max-w-5xl mx-auto px-3"> | ||
<ExampleExplanation | ||
title={EXPLANATION.title} | ||
description={EXPLANATION.description} | ||
/> | ||
{data.deals ? ( | ||
<div className="py-10"> | ||
{data.deals.map((deal) => ( | ||
<div key={deal.id}> | ||
<h1>{deal.title}</h1> | ||
</div> | ||
))} | ||
</div> | ||
) : data.getProducts ? ( | ||
<div className="py-10"> | ||
{data.getProducts.map((product) => ( | ||
<div key={product.name}> | ||
<h1>{product.name}</h1> | ||
</div> | ||
))} | ||
</div> | ||
) : ( | ||
<div>Nic tu není</div> | ||
)} | ||
</main> | ||
); | ||
} |