π Welcome to Intro to GraphQL with Java & DGS!
Your GraphQL journey begins now! Ready to embark?
In this series, we'll start from scratch and build a full-fledged GraphQL server using a REST API.
In this lesson, we will:
- Learn about what we're building, and the technologies that help us build it
- Set up our project environment
What is GraphQL?
Let's begin with the most important question of the course. What is GraphQL?
GraphQL is the developer-friendly query language for the modern web. It transforms how apps fetch data from an API, enabling you to get exactly what you need with a single queryβinstead of wrangling responses from a patchwork of REST endpoints.
With a strongly typed schema at its core, GraphQL helps you define relationships between data across any number of systems, empowering you to focus on what your data can do instead of where it's stored.
Throughout this course, we're going to learn how GraphQL fits into our existing architecture and how it works with existing REST APIs and other data sources. We'll learn how to use queries, mutations, arguments, the schema, and resolvers in our GraphQL API. Get ready to roll up your sleeves, write some code, test your understanding, and build something cool!
What is DGS?
GraphQL by itself is a specification, a query language for APIs. To implement GraphQL in a server, we typically use a GraphQL framework.
DGS, or Domain Graph Service, is a framework created by the Netflix team that enables GraphQL in a Spring Boot application with just a few dependencies. As we explore the fundamental topics of GraphQL, we'll learn to work with DGS annotations and methods, and see how we can wire up our Java classes with GraphQL capabilities.
Prerequisites
We'll start with a boilerplate Spring Boot project and gradually add our DGS dependencies.
To follow along...
- You should be familiar with Java basic programming concepts.
- You'll need JDK 17 installed as well as an IDE (we recommend IntelliJ IDEA Community Edition).
- We also recommend being familiar with the basics of Spring Boot, as well as how to use REST APIs and HTTP requests.
- Bonus: Many popular IDEs offer plugins that enable GraphQL syntax highlighting. In IntelliJ, we recommend the GraphQL plugin.
What we're building
Want to book a trip to new, exciting, sometimes-fictional places in the vast universe? Enter Airlock!
With Airlock, you can find listings that meet your dates of choice and the number of beds you'll need. Learn what each place is all about and what amenities it offers, and if you're interested, you can book your stay in one click (provided you have enough space credits in your wallet of course)!
For this first iteration of the Airlock API, we'll focus on listings: showcasing featured intergalactic locations, detailing their features and amenities, and adding new listings. In future courses, we'll grow our API using GraphQL Federation.
Clone the repository
In the directory of your choice with your preferred terminal, clone the app's starter repository:
git clone https://github.com/apollographql-education/odyssey-intro-dgs.git
Note: If you get stuck at any time during the course, feel free to check out the final
branch for some help.
Project structure
Our project already contains the files we need to get started and run our Spring Boot server, but we'll work primarily out of the java
and resources
packages highlighted below.
π¦ odyssey-intro-dgsβ£ π gradleβ£ π srcβ β£ π mainβ β β£ π javaβ β β β£ π com.example.listingsβ β β β β£ π ListingsApplicationβ β β β β π WebConfigurationβ β β π resourcesβ β β π schemaβ β π testβ£ π build.gradleβ£ π gradlewβ£ π gradlew.batβ£ π settings.gradleβ π README.md
Now, open the repository in your favorite IDE. We're using IntelliJ IDEA Community Edition in our examples.
Let's build and run the app
In your IDE, open the main ListingsApplication
file located in the com.example.listings
package. This is the starting point for our app.
@SpringBootApplicationpublic class ListingsApplication {public static void main(String[] args) {SpringApplication.run(ListingsApplication.class, args);}}
In IntelliJ, we have the handy green play button in the margin next to the main
function, or the one located at the top of the interface.
Alternatively, you can open a new terminal to the root of your project and run the following command:
./gradlew bootRun
In the IDE Run output, we should see that our app is running!
> Task :ListingsApplication.main(). ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v3.2.5)
Of course, it's not doing much yet. Let's jump in!
Key takeaways
- GraphQL enables precise data retrieval with a single query, eliminating the need to navigate multiple REST endpoints on the client app side.
- Domain Graph Service (DGS) is a Netflix-developed framework for Java developers.
Up next
We've laid the groundwork for the tools we'll use, but we still need to talk about HOW they actually work together. In the next lesson, we'll jump into the basics and follow the journey of a GraphQL operation.
Share your questions and comments about this lesson
This course is currently in
You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.