Overview
Apollo Connectors transform the way we connect to REST APIs, simplifying API orchestration in a declarative, configuration-first approach.
All of the work happens in the schema. First, we define the types and fields 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 GraphQL directives:
@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
baseURL: "https://products.example.com",
headers: [{ name: "Authorization", from: "Authorization" }] }
The @connect
directive
Next we have the heart of Connectors: the @connect
directive.
The @connect
directive attaches to a field or object type 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
{ name: "Authorization", from: "Authorization" },
{ name: "X-Api-Key", value: "ODYSSEY"}
The response
To configure the response, the Connector needs a selection
mapping: how the JSON response maps to the types and fields in our schema.
Sometimes the mapping is simple, one-to-one, where the property names in the response match our schema types and fields 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 fields. 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
headers: [{ name: "Authorization", from: "Authorization" }]
And that's it for Connectors in the schema, just directives 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 router.
API orchestration with 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 GraphQL Federation, you might be thinking: "This router 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 operation to the subgraph responsible for it. We can mix and match subgraphs and Connectors.
All we need to do is give the router our schema, and it takes care of the rest!