4. Defining a @source
2m

Overview

Let's get things going in the schema, starting with the @source . Remember, this is where we define the core information about the data source, like its URL and any headers. We'll refer to this @source with the @connect in the next lesson.

In this lesson, we will:

  • Use the @source to define a REST API data source in the schema

The schema

Let's open up the listings.graphql file.

Here we have a very minimal schema to kick off our course project, Airlock.

listings.graphql
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.10", import: ["@key"])
type Query {
"A curated array of listings to feature on the homepage"
featuredListings: [Listing!]!
}
"A particular intergalactic location available for booking"
type Listing {
id: ID!
"The listing's title"
title: String!
}

We have one entry point to start: Query.featuredListings, which returns a list of Listing types. A Listing has an id and title for now; we'll add more later on.

Now where exactly will this data be coming from? We'll be using an existing REST API as our data source: https://airlock-listings.demo-api.apollo.dev/

Let's get this source set up in our schema.

Importing the @source directive

First, we'll need to import the .

  1. At the top of the listings.graphql file is the schema definition—it lets us extend our schema with extra capabilities, like the support already included.

  2. We'll append a new @link, with a url to the connectors spec, and an import array that contains "@source".

    listings.graphql
    extend schema
    @link(url: "https://specs.apollo.dev/federation/v2.10", import: ["@key"])
    @link(
    url: "https://specs.apollo.dev/connect/v0.1"
    import: ["@source"]
    )

Applying the @source directive

Now we're ready to use it!

  1. Right below the @link where we imported the , we'll add a new line that applies @source.

  2. If you're using a supported editor, you should see a prompt to auto-complete the required parameters by hitting Enter. Pretty handy!

    listings.graphql
    extend schema
    @link(url: "https://specs.apollo.dev/federation/v2.10", import: ["@key"])
    @link(
    url: "https://specs.apollo.dev/connect/v0.1"
    import: ["@source"]
    )
    @source(name: "", http: { baseURL: "" })
  3. Let's fill these in. Our data source requires a name, which we'll use to uniquely identify it. You can choose to name the source anything you want. We'll name it listings.

    listings.graphql
    @source(
    name: "listings"
    )
  4. Finally, we need to set the baseURL to point to our REST API: https://airlock-listings.demo-api.apollo.dev. This gives us the foundational URL where all of our requests will be directed. We'll see how to apply specific paths and endpoints later on.

    listings.graphql
    @source(
    name: "listings"
    http: { baseURL: "https://airlock-listings.demo-api.apollo.dev" }
    )

Uh-oh! Build errors in rover dev

When we save our file, detects the changes and checks to see if the new schema is valid. In this case, we've got errors!

Rover errors
error[E029]: Encountered 1 build error while trying to build a supergraph.
Caused by:
QUERY_FIELD_MISSING_CONNECT: [listings] The field `Query.featuredListings`
has no `@connect` directive.

By adding the link to the connectors spec, we've essentially told to expect a schema with connectors. In this case, we haven't added any yet! Fortunately, we see a helpful hint on how to move forward: using the @connect .

Practice

The @source directive
The @source directive takes in a few parameters to help us define a shared data source for multiple connectors. The name parameter is a 
 
 for the data source. The 
 
 parameter defines the REST API endpoint we want to connect to. We can also set headers that we want to pass to the REST API using the 
 
 parameter.

Drag items from this box to the blanks above

  • connector

  • http.baseURL

  • http.restHeaders

  • http.restAPI

  • unique identifier

  • http.headers

  • headers

Code Challenge!

Define a @source named outerspace that points to the URL http://outerspace-api.example/. Include a header named x-caller with a hard-coded value of space-schema.

Loading...
Loading progress

Key takeaways

  • The @source defines a shared data source for multiple connectors.
  • The @source takes in two parameters: name and http.

Up next

Let's fix those build errors in the next lesson!

Previous

Share your questions and comments about this lesson

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.