Every dog owner wants to find the perfect friends for their new puppy. Now we have an app for that! You can browse through various puppy profiles and swipe right or left to find your new puppy friend. Setting up puppy playdates has never been easier.

OK, not really. But we do have a wacky demo app built with React, Material-UI, Apollo Client, and Slash GraphQL (a hosted GraphQL back end from Dgraph).

In this article, we’ll explore how I built the app and also look at some of the basics of the technologies I used.

Ready to unleash the fun?

Overview of the Demo App

Puppy Playdate app

Our app is a Tinder clone for puppy playdates. You can view our puppy profiles, which are pregenerated seed data I included in the database. You can reject a puppy by swiping left or by clicking the X button. You can show interest in a puppy by swiping right or by clicking the heart button.

After swiping left or right on all the puppies, your results are shown. If you’re lucky, you’ll have matched with a puppy and will be well on your way to setting up your next puppy playdate!

Check out the demo app here. You can also see the code on GitHub.

In this article, we’ll walk through setting up the schema for our app and populating the database with seed data. We’ll also examine how the puppy profiles are fetched and how the match updates are done.

The Architecture

As noted above, the four core technologies behind this app are React, Material-UI, Apollo Client, and Slash GraphQL.

I chose React because it’s an excellent front-end library for developing UIs in a declarative way with reusable components.

Material-UI helped provide the building blocks for the UI components. For example, I used their ButtonCardCircularProgressFloatingActionButton, and Typography components. I also used a couple icons. So I had some base component layouts and styles to use as a starting point.

I used Apollo Client for React to facilitate communication between my front-end components and my back-end database. Apollo Client makes it easy to execute queries and mutations using GraphQL in a declarative way, and it also helps handle loading and error states when making API requests.

Finally, Slash GraphQL is the hosted GraphQL back end which stores my puppy data in the database and provides an API endpoint for me to query my database. Having a managed back end means I don’t need to have my own server up and running on my own machine, I don’t need to handle database upgrades or security maintenance, and I don’t need to write any API endpoints. As a front-end developer, this makes my life a lot easier.

Getting Started With Slash GraphQL

Let’s first walk through creating a Slash GraphQL account, a new back end, and a schema.

You can create a new account or log into your existing Slash GraphQL account online. Once authenticated, you can click the “Launch a New Backend” button to view the setup screen shown below.

Creating a new back end with Slash GraphQL

Next, choose your back end’s name (puppy-playdate, in my case), subdomain (puppy-playdate again for me), provider (AWS only, currently), and zone (choose one closest to you or your user base, ideally). When it comes to pricing, there’s a generous free tier that’s enough for this app.

Click the “Launch” button to confirm your settings, and in a few seconds you’ll have a new back end up and running!

Once the back end is created, the next step is to specify a schema. This outlines the data types that your GraphQL database will contain. In our case, the schema looks like this:

GraphQL schema for our database

Here we’ve defined a Puppy type that has the following fields:

  • id, which is a unique ID generated by Slash GraphQL for each object stored in the database
  • name, which is a string of text that’s also searchable
  • age, which is an integer
  • matchedCount, which is also an integer and will represent the number of times a puppy has matched with someone
  • profilePic, which is a string that contains the image URL to be shown in the app
  • bio, which is a string that contains a short description about the puppy
  • interests, which is an array of strings representing the puppy’s interests and is also searchable

Now that we have a back-end endpoint and schema set up, it’s time to add some puppies! The API Explorer in the Slash GraphQL web console allows us to easily execute GraphQL queries and mutations against our database without having to write or run any additional code within our app. We’ll insert data into the database using this mutation:

Mutation to add all puppies to the database

We can then query our database to fetch the puppy data as a quick sanity check that our seed data was inserted properly. The query looks like this:

Query to get all puppies from the database

The data is then shown in the API Explorer’s results panel, like so:

Querying the database for all our puppies

Fetching Puppies (Haha…)

Now that we have our database populated with seed data, we can work on getting our puppies to show up in our app. I used React to build the UI and Material-UI for my component library to help speed up the development process. Rather than executing GraphQL queries and mutations directly, I chose to use Apollo Client for React to declaratively handle interacting with my back-end API and database.

Apollo Client utilizes React’s Context API. To get started, you first initialize a new client and then wrap your root component with a provider component. This makes the database data available anywhere in the app through the context.

Creating a new Apollo client
Wrapping the app in the Apollo provider component

Then in our App.js file, we can define a GraphQL query to fetch all the puppies:

GraphQL query to fetch all puppies

We then declaratively execute the query inside our App component and work with the response data by using Apollo Client’s useQuery hook:

Executing the query with the useQuery hook

The result of calling that method is an object which contains properties for the response data, loading state, error info, and a method to refetch the data. We only need access to the data property and the refetch method, so we destructure those two items from the object and then pass them down into child components as needed.

Welcome screen in the Puppy Playdate app

Loading screen in the Puppy Playdate app as the puppy data is being fetched

Updating Puppy (Love)

Once the puppy data is fetched, the puppies are shown one by one as interactive cards. The Tinder-swipe effect is implemented using an npm package called react-tinder-card.

When a puppy card is swiped to the right (or when the heart button is clicked), an API request is made to the back end to increment the puppy’s matchedCount value by one. This is done through Apollo Client again but this time using the useMutation hook since this is a GraphQL mutation.

Just as before, we first write our GraphQL mutation:

GraphQL mutation to update a puppy’s data

Then we execute the mutation inside our component, this time as part of our swipe event-handler method called swiped:

Executing the mutation with the useMutation hook and swiped event handler
Doug is a good boy

Each liked dog is recorded. Once you’ve swiped through all 11 dogs in our database, your match results are shown!

You matched with Louie!

Next Steps

That’s it for our demo app! If you as the reader wanted to continue to build on this project, you could extend the app by creating an authentication workflow, allowing users to create accounts and post their own profiles. You could also allow users to actually match each other and send them notifications when that happens.

Wrapping Up

As I built this app and considered the features and functionalities I wanted to include, the database schema changed over time. I started without including the puppies’ ages or their interests. When I decided I did want to show that info on the puppy cards, I simply edited my schema in the Slash GraphQL web console to include the age and interests fields.

I also originally started with a boolean matched field to show whether or not you were matched with each puppy. However, since this app includes no authentication and can be used by any user, it felt more appropriate to instead use a matchedCount field that recorded how many times each puppy had previously been liked by any user.

Making this tweak to the schema was again as simple as replacing the matched boolean type with the matchedCount int type.

The flexibility of GraphQL in allowing me to edit my schema on the fly without having to rewrite several API endpoints greatly sped up the development process. And Slash GraphQL’s API Explorer allowed me to easily execute queries and mutations directly against my database to experiment with the syntax and the fields I’d need before having to write any app code.

The architecture I chose was perfect for developing this app — it made rapid prototyping easy! The paw-sibilities are endless!



Source link

Write A Comment