Overview
Let's get things going in the schema, starting with the @source
directive. 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
directive in the next lesson.
In this lesson, we will:
- Use the
@source
directive 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.
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 directive.
At the top of the
listings.graphql
file is the schema definition—it lets us extend our schema with extra capabilities, like the GraphQL Federation support already included.We'll append a new
@link
, with aurl
to the connectors spec, and animport
array that contains"@source"
.listings.graphqlextend 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!
Right below the
@link
where we imported the directive, we'll add a new line that applies@source
.If you're using a supported editor, you should see a prompt to auto-complete the required parameters by hitting Enter. Pretty handy!
listings.graphqlextend 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: "" })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")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, Rover detects the changes and checks to see if the new schema is valid. In this case, we've got 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 Rover 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
directive.
Practice
@source
directive@source
directive takes in a few parameters to help us define a shared data source for multiple connectors. The name
parameter is a Drag items from this box to the blanks above
connector
http.baseURL
http.restHeaders
http.restAPI
unique identifier
http.headers
headers
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
.
Key takeaways
- The
@source
directive defines a shared data source for multiple connectors. - The
@source
directive takes in two parameters:name
andhttp
.
Up next
Let's fix those build errors in the next lesson!
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.