Odyssey

Intro to GraphQL with Java & DGS
beta

Course overview and setupGraphQL basicsSchema definition language (SDL)Building the schemaDatafetchersApollo Sandbox ExplorerThe listings REST APIConsuming a data sourceQuery argumentsAdding the Amenity typeResolver chainsMutationsWrapping up
6. Apollo Sandbox Explorer
6m

Overview

It's time to write some queries, and see our server return some (hardcoded) data!

In this lesson, we will:

  • Learn how to navigate Apollo Sandbox Explorer
  • Save operations for future use

🚀 Exploring our first query

To write our test query, we'll use Apollo Sandbox. Sandbox is free to use and doesn't require an account. It's part of the Apollo GraphOS platform, and helps with local graph development.

Apollo GraphOS is a complete cloud platform for building, managing, and scaling your graph. GraphOS provides a set of tools and services so that product developers can focus on building better apps, faster.

With Sandbox, we can load a GraphQL server's schema and explore it using some cool GraphOS features such as a schema reference and the Explorer.

The Explorer is a powerful web IDE for creating, running, and managing GraphQL operations. It lets us build operations easily and quickly, look at our operation history, peek at response hints and share operations with others.

Let's make sure our server is running, then open up a browser and head to Apollo Sandbox Explorer!

Task!

Exploring the Explorer

When we land in the Explorer, we'll see an interface that's mostly empty.

https://studio.apollographql.com/sandbox/explorer

The Apollo Sandbox Explorer, not connected to any API

This is because it hasn't yet connected to a GraphQL API—specifically, the one we've just spent some time building!

Let's get that hooked up first. At the top of the Explorer, we'll see an input for the location where our server is running. By default, our GraphQL server is available to query on http://localhost:8080/graphql, so we can paste that URL into this input.

http://localhost:8080/graphql

Ordinarily, our server restricts who can access it and send requests—so you might be wondering how we're able to connect to the Apollo Sandbox Explorer right out of the box.

Our starter code has actually taken care of allowing this connection right from the start. In our main/java/com.example.listings directory, you'll find a class called WebConfiguration. Here, we've included a method called addCorsMappings that explicitly permits requests from "https://studio.apollographql.com" right from the start. CORS errors, be gone!

WebConfiguration
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/*").allowedOrigins("https://studio.apollographql.com/").allowedMethods("*");
}

After a moment, we should see the little dot turn green—this means the Explorer has connected successfully! And we'll also see that the UI has updated with some new elements. Let's explore those next.

https://studio.apollographql.com/sandbox/explorer

The Apollo Sandbox Explorer, connected to the running DGS server endpoint

If you're having trouble connecting to Sandbox, first check that you've pasted in the following URL.

http://localhost:8080/graphql

If that doesn't solve the issue, stop and restart your Java server.

Still having trouble? Visit the Odyssey forums to get help.

Building a query

The Operation panel in the middle is where we create queries. The Explorer might have already filled in a default operation. Let's open up a new workspace tab with the plus (+) button for a fresh start.

We can write the operation manually, or add fields from the Documentation panel on the left: it enables you to drill down into your schema's fields, starting at the entry points of the Query type.

https://studio.apollographql.com/sandbox/explorer

The Explorer, with the Documentation panel highlighted to show the Query entrypoints

Clicking on the plus (⊕) button next to a field automatically adds that field to our current operation. This is a handy way to assemble complex queries without needing to remember your schema's exact structure or GraphQL syntax.

Let's add featuredListings to our first query.

https://studio.apollographql.com/sandbox/explorer

Adding the featuredListings field to the Operation panel

We'll see that the Documentation tab has updated, showing us the different fields that we can add to get data for each object returned as a Listing type. We can click on the plus button by id to add it to our query.

https://studio.apollographql.com/sandbox/explorer

Highlighting the id field we want to add to our query

This automatically updates the Operation panel with the field we selected.

https://studio.apollographql.com/sandbox/explorer

Adding the id field to the Operation panel

Here's what our query should look like:

query FeaturedListings {
featuredListings {
id
}
}

At the top of the Operation panel is the button to run our query. Let's click it now and see what happens:

https://studio.apollographql.com/sandbox/explorer

Running the operation and seeing featured listing data in the Response panel

The Response panel on the right contains an object with our list of listing IDs!

Response object
{
"data": {
"featuredListings": [
{
"id": "1"
},
{
"id": "2"
}
]
}
}

You might have noticed a field called _service in the Documentation panel.

This field appears in the schema because our DGS-powered GraphQL server is federation-compatible out of the box. Federation is an architecture for creating modular graphs. It allows us to build smaller pieces of our graph individually, then compose them to work together. Each of these pieces is called a subgraph, and that's exactly what our DGS server is.

The _service field lets us query the entirety of our subgraph schema as an SDL string, and it's useful when composing multiple graphs together. We'll explore Apollo Federation in the next course in the series, Federation with Java & DGS.

If the Response panel shows an error along the lines of: "The field at path '/featuredListings' was declared as a non null type..." this means that our server isn't returning any data for the Query.featuredListings field we defined in our schema.

Try the following steps to resolve this error.

  1. Double check that you've saved the changes made to the schema.graphqls and ListingDataFetcher files.
  2. Ensure that the ListingDataFetcher file is annotated with @DgsComponent. (If not, it won't be picked up as relevant to the DGS side of things!)
  3. Restart the server.

Still having trouble? Visit the Odyssey forums to get help.

Adding all fields

Right now, we've selected only the featuredListings and id fields, but the Explorer also has a way to add all fields to an operation at once. When we click the dropdown by the Fields subheading, we'll see that we have two options.

https://studio.apollographql.com/sandbox/explorer

Clicking the Fields dropdown in the Documentation panel

First, we have the option to Select all scalar fields. This will select all of the fields on the Listing type that return a value that does not have additional subfields.

Click Select all scalar fields. We'll see the title, numOfBeds, costPerNight, and closedForBookings fields have been added to our query.

https://studio.apollographql.com/sandbox/explorer

All scalar fields added to the query

All of our scalar fields are taken care of, but we'll notice there's a second option in the dropdown: Select all fields recursively. This option lets us add all of a type's fields, and any of those field's subfields, all at once; this is particularly important when a field returns an object type that has its own set of fields. We'll see this shortly when we add to our schema!

Running the query

Your completed operation should match the following:

query FeaturedListings {
featuredListings {
id
title
numOfBeds
costPerNight
closedForBookings
}
}

Take it for a spin in the Explorer!

Here's the response we should see:

{
"data": {
"featuredListings": [
{
"id": "1",
"title": "Beach house on the edge of the Laertes meteor",
"numOfBeds": 3,
"costPerNight": 360,
"closedForBookings": false
},
{
"id": "2",
"title": "Unforgettable atmosphere, unbeatable heat, tasteful furnishings",
"numOfBeds": 4,
"costPerNight": 124,
"closedForBookings": true
}
]
}
}
Task!

Saving an operation

We've already spent the time building out the FeaturedListings operation, so let's take advantage of another Explorer feature that makes constructing it again even easier. (You'll need a Studio account for this part!)

At the top of the Operation panel, we'll find a save icon button.

https://studio.apollographql.com/sandbox/explorer

The Save dropdown in the Explorer, showing the Save as option

Clicking the Save as option from the dropdown opens a modal where we can give our operation a name, and save it to an operation collection.

With a collection, we can store a group of operations in a place that's quick to access when we want to test future changes to our schema.

Let's start by giving our operation the name FeaturedListings so that we can locate it when we need it again. Next, in the Select a collection dropdown, let's select the option under Sandbox to create a new default collection.

https://studio.apollographql.com/sandbox/explorer

Saving the FeaturedListings operation in a new Sandbox collection

After we click the Save button, we'll see that our operation's tab now has the name we assigned to it! Additionally, we'll see a bookmark icon that indicates that this operation is saved to a collection.

https://studio.apollographql.com/sandbox/explorer

A tab showing the named operation and a bookmark icon

We can use the panel on the left of the Explorer to access operations that we've saved to a collection. Clicking the bookmark icon at the top of the menu opens up all of our Operation Collections, where we can see our FeaturedListings operation has been saved for quick access!

https://studio.apollographql.com/sandbox/explorer

Clicking the bookmark icon to access saved operations

The Schema tab

Before we leave the Sandbox environment, let's check out our schema; it's accessible under the first tab in our main navigation.

https://studio.apollographql.com/sandbox/schema/reference

A screenshot of the Schema page in Sandbox, with the FeaturedListings field outlined

Here we have a full view and reference into our schema! It's pretty sparse right now, but we can see our Query type with a featuredListings field that returns a [Listing!]! type.

In the SDL tab, we can also see the schema in SDL syntax. This is the actual schema that our server is running its requests against!

https://studio.apollographql.com/sandbox/schema/sdl

SDL page

You might have noticed the _service field listed on the Schema Reference page, and there's unfamiliar syntax here as well: we see the SDL page is peppered with the word "directive", and a number of other definitions: @extends, @key, and so on.

These additional elements appear because our DGS-powered GraphQL server is federation-compatible out of the box. Federation is an architecture for creating modular graphs. It allows us to build pieces of our graph individually, then compose them to work together. Each of these pieces is called a subgraph, and that's exactly what our DGS server is. Both the _service field in our schema and the directives that appear here become extremely useful mechanisms when building federated graphs.

We'll explore Apollo Federation in the next course in the series, Federation with Java & DGS.

Practice

Which of these are benefits of using the Apollo Sandbox Explorer?

Key takeaways

  • The Explorer is a full-fledged IDE that introspects a running server's schema and allows us to build and send queries.
  • We can use operation collections to store operations we've already built for future use.

Up next

The Explorer can do so much more, but this is all we'll cover for now. It's time to move onto our next topic—hooking up a real data source!

Previous
Next

Share your questions and comments about this lesson

This course is currently in

beta
. Your feedback helps us improve! If you're stuck or confused, let us know and we'll help you out. All comments are public and must follow the Apollo Code of Conduct. Note that comments that have been resolved or addressed may be removed.

You'll need a GitHub account to post below. Don't have one? Post in our Odyssey forum instead.

              Apollo Sandbox

              A part of GraphOS Studio focused on local development, available at https://studio.apollographql.com/sandbox. Apollo Sandbox does not require an Apollo account.

              operations

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              Apollo GraphOS

              A platform for building and managing a supergraph. It provides a management plane to test and ship changes and runtime capabilities to secure and monitor the graph.

              graph

              A schema-based data model representing how different data elements interconnect and can be accessed.

              GraphOS

              A platform for building and managing a supergraph. It provides a management plane to test and ship changes and runtime capabilities to secure and monitor the graph.

              GraphQL server

              A server that contains a GraphQL schema and can resolve client-requested operations that are executed against that schema.

              GraphOS

              A platform for building and managing a supergraph. It provides a management plane to test and ship changes and runtime capabilities to secure and monitor the graph.

              GraphQL

              An open-source query language and specification for APIs that enables clients to request specific data, promoting efficiency and flexibility in data retrieval.

              operations

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              GraphQL

              An open-source query language and specification for APIs that enables clients to request specific data, promoting efficiency and flexibility in data retrieval.

              GraphQL server

              A server that contains a GraphQL schema and can resolve client-requested operations that are executed against that schema.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              Apollo Sandbox

              A part of GraphOS Studio focused on local development, available at https://studio.apollographql.com/sandbox. Apollo Sandbox does not require an Apollo account.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              fields

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              fields

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              GraphQL

              An open-source query language and specification for APIs that enables clients to request specific data, promoting efficiency and flexibility in data retrieval.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              fields

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              GraphQL server

              A server that contains a GraphQL schema and can resolve client-requested operations that are executed against that schema.

              graphs

              A schema-based data model representing how different data elements interconnect and can be accessed.

              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              subgraph schema

              A schema for a subgraph server. A subgraph schema must be compliant with the GraphQL and Apollo Federation specs to be composed into a supergraph.

              SDL

              GraphQL's schema definition language (SDL). The syntax for writing GraphQL schemas. All GraphQL APIs can use SDL to represent their schema, regardless of the API's programming language.

              Apollo Federation

              Apollo’s implementation of GraphQL Federation—an architecture for orchestrating multiple APIs into a single GraphQL API.

              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              fields

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              fields

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              fields

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              fields

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              query

              A request for specific data from a GraphQL server. Clients define the structure of the response, enabling precise and efficient data retrieval.

              scalar

              A "base" type that resolves to a single value. GraphQL includes the following scalar types by default: Int, Float, String, Boolean, and ID.

              fields

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              fields

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              object type

              A type in a GraphQL schema that has one or more fields. User is an object type in the following example:

              type User {
              name: String!
              }
              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              operations

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              operations

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              SDL

              GraphQL's schema definition language (SDL). The syntax for writing GraphQL schemas. All GraphQL APIs can use SDL to represent their schema, regardless of the API's programming language.

              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              directive

              A GraphQL annotation for a schema or operation that customizes request execution. Prefixed with @ and may include arguments. For example, the @lowerCase directive below can define logic to return the username field in lowercase:

              type User {
              username: String! @lowerCase
              }
              GraphQL server

              A server that contains a GraphQL schema and can resolve client-requested operations that are executed against that schema.

              graphs

              A schema-based data model representing how different data elements interconnect and can be accessed.

              field

              A unit of data that belongs to a type in a schema. Every GraphQL query requests one or more fields.

              type Author {
              # id, firstName, and lastName are all fields of the Author type
              id: Int!
              firstName: String
              lastName: String
              }
              directives

              A GraphQL annotation for a schema or operation that customizes request execution. Prefixed with @ and may include arguments. For example, the @lowerCase directive below can define logic to return the username field in lowercase:

              type User {
              username: String! @lowerCase
              }
              graphs

              A schema-based data model representing how different data elements interconnect and can be accessed.

              Apollo Federation

              Apollo’s implementation of GraphQL Federation—an architecture for orchestrating multiple APIs into a single GraphQL API.

              operation

              A single query, mutation, or subscription that clients send to a GraphQL server to request or manipulate data.

              NEW COURSE ALERT

              Introducing Apollo Connectors

              Connectors are the new and easy way to get started with GraphQL, using existing REST APIs.

              Say goodbye to GraphQL servers and resolvers—now, everything happens in the schema!

              Take the course