Authors:
Cameron Johns, Cille Schliebitz, Anita Gu
Changed on:
3 Feb 2025
`useQuery` hook. We'll move away from relying on page query data and instead directly query the Fluent GraphQL API to retrieve a list of nearby locations with product stock. This involves defining data structures for query results, storing the query in a constant, and using the `useQuery` hook to execute the query with dynamic parameters derived from the form's context and the user's context (via `useAuth`). We will then render the results in a selectable list using radio buttons.`useQuery` hook, rather than relying on potentially unavailable page query data.`searchVirtualInventory`) is used to retrieve nearby locations with stock.`useQuery` Hook: The `useQuery` hook is used to execute the GraphQL query with dynamic parameters.`productRef`, `locationRef`, `lat`, `lng`) are derived from the form's entity context (product) and the user's context (location) using the `useAuth` hook.`useQuery` hook to pass in the parameters and execute the query`interface SVINode {`` location: { name: string; ref: string };`` virtualPositions: Array;``}`Next, define a structure for SearchVirtualInventory results:`interface SVIResult {`` searchVirtualInventory: Connection;``}`Store the query in a string constant:Refer to the video below to complete the remaining steps:`useQuery` hook [0:52] to pass in the parameters [1:45] and execute the query. `productRef` from the `entityContext` on `FormFieldProps`, which is the current Product we're performing the availability lookup on [2:22]`locationRef`, `lat` and `lng` from the currently logged-in user's context, using the `useAuth` hook. [3:08]`FormControlLabel` from the standard Material UI component library [1:06]`onChange` handler to update the `location` state variable [1:54]`useEffect` hook to print out the currently selected `location` state variable value [2:42]1const searchVirtualInventoryQuery = `query availability($productRef: String!, $locationRef: String!, $lat: Float!, $lng: Float!) {
2 searchVirtualInventory(
3 virtualCatalogue: { ref: "VC:ATS:CC:1" },
4 productQuantities: [{ productRef: $productRef, quantity: 1 }],
5 excludedLocationRefs: [$locationRef],
6 orderByProximity: { longitude: $lng, latitude: $lat }
7 ) {
8 edges {
9 node {
10 location { ref name }
11 virtualPositions { quantity }
12 }
13 }
14 }
15 }`;