The content of the endpoint is displayed once the query is applied, as seen in the image above. Since our testing is complete at this point, we may continue.
Setting up our front-end
Now that we have successfully configured our CMS, added some data, and tested our API, we are required to show this data on our website. We will need to set up our front end in order to accomplish this. Enter the following command into your terminal:
npm create vite@latest
npm install
npm run dev
As observed, we will be using React Vite as it is a much faster local development server. This command creates a React folder with all the necessary project-related files. We must then add our components and stylings when the installation is complete.
Once all components are properly set up and styled, we can then proceed to display the content in the API.
Integrating Webiny CMS to fetch and display property data
We will need the access key or token we made before, combined with the read API URL, which can be found in the API playground, to accomplish this. To store them, we'll make a dotenv file in our directory.
VITE_WEBINY_GRAPHQL_TOKEN = Insert your access token here
VITE_WEBINY_GRAPHQL_URL = Insert your Read API url here
For GraphQL-based communication with the Webiny CMS in our application, we'll utilize the Apollo client. To install this, use the CLI command listed below:
npm install @apollo/client graphql
We must initialize ApolloClient after the installation is finished. Sending a configuration object with the uri and cache properties to its constructor in our main.js file.
ApolloClient utilizes an instance of InMemoryCache to cache query results after retrieving them, and the uri gives the URL of our GraphQL server.
Next, we'll use ApolloProvider to cover the App component in our main.js file.
import {
ApolloClient,
InMemoryCache,
ApolloProvider,
} from "@apollo/client";
const client = new ApolloClient({
uri: import.meta.env.VITE_WEBINY_GRAPHQL_URL,
cache: new InMemoryCache(),
headers: {
Authorization: `Bearer ${import.meta.env.VITE_WEBINY_GRAPHQL_TOKEN}`,
},
});
ReactDOM.createRoot(document.getElementById("root")).render(
<ApolloProvider client={client}>
<App />
</ApolloProvider>
);
Having completed setting up ApolloClient, the next step is to extract the data and send to their respective components. To do this, open the FlatPage components , import useQuery and gql in both components, and then enter your query for both components.
import { useQuery, gql } from "@apollo/client"
const GET_HOMES = gql`
query {
listFlats {
data {
coverImage
name
address
price
}
}
}
`;
According to what is shown above, listFlats, which aisre executed by GET_HOMES, return arrays of flats with certain properties in their data objects: coverImage, name, address, and price.
To accommodate the anticipated data, we will add three instances or states. The same notification seen above will appear whenever the data is loading or there is an error. Then the data is saved in a variable.
const { loading, error, data } = useQuery(GET_HOMES);
if (loading) return <p className="container">Getting all avaliable flats...</p>;
if (error) return <p className="container">An error occurred:(</p>;
const flats = data.listFlats.data;
After setting up all instances successfully, we routed all the pages of our project. For this project, we will be using react-router-dom version 6.16.0, as seen below.
const router = createBrowserRouter([
{
path: "/",
element: <Home />,
},
{
path: "/flats-section",
element: <FlatPage />,
},
]);
return (
<RouterProvider router={router} />
);
Since both components have similar functionalities, we will continue to focus on the FlatsPage components in this tutorial.
To get the data value returned based on the query we passed in our components, we will need to loop through the data stored in the flats variable and then display it with some styling.
<div className="cover-2">
{flats.map((flat) => (
<div className="cards" key={flat.id}>
<img src={flat.coverImage} alt={flat.name} />
<h3>{flat.name}</h3>
<p>Address: {flat.address}</p>
<p>Price: ${flat.price}</p>
</div>
))}
</div>
Now our webpage should be all set and should look like this: