3. How connectors work in the schema
3m

Overview

transform the way we connect to REST APIs, simplifying in a declarative, configuration-first approach.

All of the work happens in the schema. First, we define the types and that make up our API. Then, we configure our request to a data source and transform the response to a shape that fits with our schema.

In this lesson, we will:

  • Learn about two : @source and @connect.

The @source directive

With @source, we define the core information about the data source: its name and details, such as the base URL and any HTTP headers we want to include with the request. We could have multiple data sources in one schema, so defining multiple @source instances helps us easily reuse and refer to which one we need.

Example @source syntax
@source(
name: "products" # A unique identifier for the data source.
http: { # An object that defines the baseURL string and an array of headers for the data source
baseURL: "https://products.example.com",
headers: [{ name: "Authorization", from: "Authorization" }] }
)

Source directive

The @connect directive

Next we have the heart of connectors: the @connect .

The @connect attaches to a root or object in the schema. Here, we define the instructions for our request (to get the data) and the response (to map that data to the schema).

The request

To configure the request, we provide @connect with:

  • A source to connect to
  • The HTTP method like GET or POST with the specific path to the endpoint
  • Any additional headers
  • An optional body payload
Example @connect syntax - request
@connect(
source: "products"
http: {
GET: "/products",
headers: [{ name: "Authorization", from: "Authorization" }]
}
selection: """
id
title
description
"""
)

Connect directive - request

The response

To configure the response, the connector needs a selection mapping: how the JSON response maps to the types and in our schema.

Sometimes the mapping is simple, one-to-one, where the property names in the response match our schema types and exactly.

Other times, we may want to rename properties, dig in a level or two deeper to access a particular value, or work with multiple nested types and . All of this happens in the selection property of @connect, explicitly defined and available for everyone on our team to see.

Example @connect syntax - response
@connect(
source: "products"
http: {
GET: "/products",
headers: [{ name: "Authorization", from: "Authorization" }]
}
selection: """
id: upc
title
description
"""
)

Connect directive - response

And that's it for connectors in the schema, just and configuration.

But where does this schema live, and how do clients actually send their requests to our API? We're missing one piece of the puzzle: the .

The role of the router

The router acts as the single access point to our API and coordinates the orchestration for us. It receives requests from a client, then follows the instructions in our schema to connect to the relevant data sources directly.

Now if you're using , you might be thinking: "This thing sounds familiar". And you'd be right! This is the same router we can find in a federated architecture, that routes parts of a GraphQL to the responsible for it. We can mix and match subgraphs and connectors.

All we need to do is give the our schema, and it takes care of the rest!

Router

Practice

What is the purpose of the @source directive?
What does the @connect directive do?

Key takeaways

  • The @source sets up data sources, including their name, base URL, and any necessary HTTP headers.
  • The @connect attaches to root or object , defining instructions for our request and responses to a REST API endpoint.
  • The serves as the single entry point to the API, taking care of the orchestration for us.

Up next

The will help with the setup later on, but we need to get that schema ready 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.