Build and Deploy a GraphQL API to the Edge with Fauna — Part 8
Fetch all Products Query
We're now ready to finish the last piece of the GraphQL API by adding a new query that fetches all products from the Fauna Database.
Inside grafbase/grafbase.config.ts
add the following query definition:
g.query('products', {
resolver: 'products/all',
returns: g.ref(product).optional().list().optional()
})
Now create the file grafbase/resolvers/products/all.ts
and add the following:
import { Client, fql, FaunaError } from 'fauna'
const client = new Client()
export default async function ProductsAll() {
try {
const documentQuery = fql`
products.all() {
id,
name,
price
}
`
const { data } = await client.query(documentQuery)
return data?.data || []
} catch (error) {
if (error instanceof FaunaError) {
console.log(error)
}
return []
}
}
💡
There's more we can do here to optimize the request to fetch all products and make sure of cursor-based pagination, but we'll do that in the next step.
Now open Pathfinder at http://127.0.0.1:4000
and execute the following GraphQL query:
query {
products {
id
name
price
}
}
You should see a response that contains your products and looks something like this:
{
"data": {
"products": [
{
"id": "372390606454915278",
"name": "Shoes",
"price": 1000
},
{
"id": "372397709974307023",
"name": "Cap",
"price": 2000
}
]
}
}