How to Query for GraphQL Data With Limits and Filters

With our `listing` query now prepared and available in our GraphQL API, we'll begin building the page shown in the `/listing/:id` route of our client app.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two

Our GraphQL API is now prepared to have us query the listing field to get information for a certain listing. With that said, we'll begin working on the client portion and now create the listing page for when a user visits a listing route with an appropriate ID. The UI for the listing page will look as follows. The listing page shows the details for a certain listing as well as provides the capability for a user to book the listing within certain dates. The user is able to see the details of a listing which consist of the listing image, location, title, host and the host avatar, description as well as the bookings made to the listing. On the right hand side, the user is able to book for a listing by picking the dates they would like for the listing to be booked and the price per day is shown at the top of this booking card. The component breakdown of this page will be as follows. The UI for the listing details will be grouped within a component that is to be called listing details. The listing bookings component is will be responsible in showing a paginated list of bookings for this listing. The listing bookings component will only be shown if the user is viewing their own listing. The capability to create a booking will be part of the listing create booking components. When the user selects dates in this component and clicks the request to book button, a modal will eventually be shown. This modal will be rendered as the child component of the listing page and will allow the user to pay and officially book the listing. We'll create this modal later on and for now just focus on building the listing details, listing bookings and listing create booking components. Finally, when the query for listing information is in the loading state, the page skeleton component or the shared page skeleton components we've created before will be shown. The first thing we'll do is create our GraphQL query document. We'll create an index file within a listing folder that is under the lib Graph QL queries folder where we'll construct the listing document. And we'll export this soon to be created document from the GraphQL queries index file. Our client will pass to the listing query an ID argument, a bookings page argument to reference the page the user is viewing for their bookings list, and a limit argument to constitute the amount of booking objects to be shown for a single page. And we'll practically query every single field within the listing GraphQL object. We'll query for the listing ID, the title, description, image, host, and the host object will get the ID, name, avatar and the has wallet field. We'll query for the listing type, the listing address, the city, the bookings of this listing. We'll get the total amount of the bookings as well as the results. And from the results, we'll try to get the ID of the booking, the tenant of the booking that is to say the person who's made the booking, the person's ID, name, and avatar. And the check in check out fields of the booking. We'll get from the listing object the bookings index field, the price, and finally the number of guests. There is one small, thick mistake we have here. If I recall correctly, the check in and checkout fields are part of the result array being returned within the paginated bookings list. Total gives us the number of bookings and result contains all the information for a single booking. If we want to verify this, we can always look in the schema definition of our GraphQL API, but I believe this was what we had. With the GraphQL listing query defined, we can auto-generate the corresponding TypeScript definitions using the codegen schema and codegen generate scripts available in our app. We'll run the codegen schema script to regenerate the schema JSON file in our client application. and we'll run the codegen generate script to regenerate the TypeScript definitions from our GraphQL queries and mutations. We'll now build the listing page in the listing section component that we've established before. The structure of how we make our query will be very similar to how we set up the user page. We'll import the use query hook from React Apollo. We'll import the listing GraphQL document from the lib GraphQL queries folder. We'll also import the auto-generated TypeScript definitions for the data and variables for the listing GraphQL query. We'll run the query when the component mounts with the use query hook and attempt to return the loading, data and error states of our query request. We'll also pass in the auto-generated type definitions for the data to be returned from the query and the variables that are expected to be passed in. The listing query we make here expects us to pass a few variables such as the ID of the listing that is to be queried, the booking page the user is viewing and the number of bookings to be returned in a page and in other words the limit of our pag ination. The ID will come from the route that the user accesses. We'll get the ID peram from the route from the match prop available in this component since this component is rendered from the route component in React Router. So we'll import route component props from React Router. We'll declare the interface for the params in our route as match params which will consist of an ID of type string and we'll access the match prop available in the component and set the ID variable of our query as match.perams.id. When the component mounts for the first time the page number for the booking should be one or that is to say the first page. So to instantiate this value and have it tracked as component states will import the use state hook from React. We'll use the use state hook at the top of the component and destruct a booking 's page state value and a set bookings page function we'll use shortly to update the booking's page value. We'll initialize the state booking's page value as one and declare it as the value of the booking's page variable in our query. The limit variable is how many bookings will want limited for a certain page. We'll define a constant above and state a page limit of 3 and we'll declare the value of the limit variable to be this page limit. While our listing query is being fetched we'll want to show the shared page skeleton component we've set up before. So we'll import the page skeleton component from the lib components folder and we'll say when the query is loading we'll want this page skeleton component to show. We'll want to wrap the page skeleton component displayed with antdesigns content component from the parent layout component. So we'll import the layout component from antdesign and wrap our page skeleton with content. If our query ever errors we'll want to show the page skeleton component with an error banner being shown above it. So we'll import the shared error banner component we have in the lib components folder and we'll render it in addition to the page skeleton component if our query was to ever error. We'll place a description of the error along the lines of this listing may not exist or we've encountered an error. Let's try again soon. At this point if our query finishes loading and no error arises this means our query has resolved and data is now available. We'll get the listing object from data and the bookings object from listing available to us. With the expected data now here we can begin to build the child components in our UI and surface the information we want. We'll build the child listing details component and child listing bookings component and render it in this parent listing component in the next lesson or so. ( Laughter )