API Documentation¶
This document provides documentation for the GraphQL queries and mutations used in the Peachfuzz Frontend project.
Overview¶
The Peachfuzz Frontend uses GraphQL to communicate with the backend API. GraphQL provides a more efficient and flexible alternative to REST by allowing the client to request exactly the data it needs.
GraphQL Client Setup¶
The project uses a GraphQL client configured in Nuxt to handle API requests. The client is set up to handle authentication, error handling, and caching.
Common Queries¶
GetCart¶
Retrieves the current user's cart with all items, pricing information, and available shipping methods.
Usage:
Returns: - cart: The current cart with items, totals, and shipping information - customer: Customer information if the user is logged in - viewer: Viewer information - paymentGateways: Available payment gateways - nextShipment: Information about the next available shipment
GetProduct¶
Retrieves detailed information about a specific product.
Usage:
Returns: - product: Detailed product information including price, description, images, and variants
Common Mutations¶
AddToCart¶
Adds a product to the cart.
Usage:
const { addToCart } = await GqlAddToCart({
input: {
productId: 123,
quantity: 1,
variationId: 456 // Optional, for variable products
}
});
Parameters: - input: An object containing: - productId: ID of the product to add - quantity: Number of items to add - variationId: (Optional) ID of the product variation
Returns: - addToCart: Object containing the updated cart
UpdateItemQuantity¶
Updates the quantity of an item in the cart.
Usage:
const { updateItemQuantities } = await GqlUpDateCartQuantity({
key: "unique_cart_key",
quantity: 3
});
Parameters: - key: The unique key of the cart item - quantity: The new quantity (0 to remove the item)
Returns: - updateItemQuantities: Object containing the updated cart
EmptyCart¶
Removes all items from the cart.
Usage:
Returns: - emptyCart: Object containing the empty cart
ApplyCoupon¶
Applies a coupon code to the cart.
Usage:
Parameters: - code: The coupon code to apply
Returns: - applyCoupon: Object containing the updated cart with the applied coupon
RemoveCoupon¶
Removes a coupon from the cart.
Usage:
Parameters: - codes: Array of coupon codes to remove
Returns: - removeCoupons: Object containing the updated cart
Session on server render¶
app/plugins/02-init.server.ts loads the cart, customer and logged-in user with initServer only when the request has one of the cookies gql-at, gql-rt or shop-session. Without them the visitor has no cart and is not logged in, so the cart store stays null and the session query is not sent to WordPress. That keeps bots, crawlers and visitors without a session from calling WordPress on every page render. /api/init (shared config) still runs; it is cached. shop-session is a session cookie, so a guest whose browser was closed counts as anonymous too. Code that reads cartStore.cart must therefore handle null.
Error Handling¶
GraphQL errors are handled using the logGQLError function from the useHelpers composable. This function logs the error and can perform additional actions like clearing cookies or showing notifications.
Example:
try {
const { cart } = await GqlGetCart();
// Process cart data
} catch (error) {
useHelpers().logGQLError(error);
// Handle error (e.g., show error message, redirect)
}
Best Practices¶
- Use TypeScript Types: Always use the generated TypeScript types for GraphQL queries and mutations
- Error Handling: Always include error handling for GraphQL operations
- Loading States: Manage loading states for better user experience
- Caching: Utilize the GraphQL client's caching capabilities for better performance
- Query Optimization: Request only the fields you need to minimize response size