Requesting a Single Set of GraphQL Fields

In this module, we begin building the server and client implementation that will help allow us to retrieve and display information for listings in our application. We'll begin by brainstorming the GraphQL fields we'll need to query listing data.

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

In the last module, we managed to build the server GraphQL fields and client representation of a user. In this module, we'll look to build the server and client portions of getting information for a single listing. In order to display information about our listings in our client app, we'll need some kind of functionality to query and resolve listing data in our server. Just like how we have only a single root level field to query for a user, we'll have a single root query field to query for a certain listing. Pretty straightforward as well. The one thing we'll keep in mind is with this field is that we should only return the sensitive listing data if the user is requesting his or her own listing. For example, the sensitive data we're referring to is the bookings made for the listing. When we build the client page, we'll want a user that queries for their own listing to see the bookings that have been made. So we'll implement this authorization in the next lesson or so. For now, let's prepare the GraphQL field and its resolver function. We'll first define the field in the root query object in the type definitions file. We'll label the field as listing. And just for this lesson, we'll say its expected return type is a string and it should be defined. Next, we'll construct the resolver for the listing field. When we built the user module, we had to define the interface of a listing since listings can be queried within a user. As a result, we also had to define the explicit resolver function for the ID field for the listing object. So we've created the listing resolver's map to contain the resolvers for the listing module. Right now, we'll just need to create the resolver for the root level listing field from the query root object. It will just say it's to return a string that is to say query.listing. If we head over to localhost 9000/api and our server is running, if we were to query for the listing field, we'll see the expected query.listing output. Great. In the next lesson, we'll build a functionality for the listing resolver to actually query for listing information from the database. [BLANK_AUDIO]