Apollo Connectors Quickstart
Integrate REST APIs into your graph
⏱️ Estimated time: 10 minutes
Hello! 👋 By completing this tutorial, you'll transform data from a fictional REST API into a GraphQL API that lets clients request exactly what they need.
If you completed the Apollo GraphOS quickstart and already have a graph initiated with rover init
using the example ecommerce REST API, you can skip to Step 2: Add new fields.
Prerequisites
To get started, you need:
A GraphOS Studio account. Sign up for a Free plan if needed.
The latest version of the Rover CLI. Install or update it like so:
terminalcurl -sSL https://rover.apollo.dev/nix/latest | sh
Authentication for Rover with your Studio account. Set it up like so:
terminalrover config auth
Step 1. Create a graph
This quickstart uses Rover to create a graph with Apollo Connectors—the fastest way to integrate REST APIs. Though this graph uses REST, graphs work with any data source or combination of sources.
In your terminal, create a working directory for your graph:
terminalmkdir my-apollo-graph && cd my-apollo-graph
Initialize your graph:
terminalrover init
Follow the prompts, selecting Create a new graph and one or more REST APIs as your starting point. Store the generated credentials securely, particularly the
APOLLO_KEY
, as it's a graph API key.
Explore example
The best way to explore the example is to run a test request in Apollo Sandbox, a local GraphQL server. To get it started:
In your terminal, follow the Next steps presented by
rover init
:terminalAPOLLO_KEY=<key> \ APOLLO_GRAPH_REF=<graph-ref> \ rover dev --supergraph-config supergraph.yaml
Once
rover dev
is running, the output in your terminal should end with something like this:terminal==> Attempting to start router at http://localhost:4000. ==> Health check exposed at http://127.0.0.1:8088/health WARN: Connector debugging is enabled, this may expose sensitive information. ==> Your supergraph is running! head to http://localhost:4000 to query your supergraph
Running into errors?- If you receive an
Error: Not connected to GraphOS.
, ensure you've included yourAPOLLO_KEY
andGRAPH REF
. See Graph API keys for instructions on how to obtain a new one. - If you receive an
Error: License not found.
, it means you need a GraphOS account with a current Free or Enterprise plan. Verify your organization's plan by navigating to GraphOS Studio, clicking on Settings, then selecting the Billing tab. Sign up for a new Free plan if necessary.
The router is the single entry point to the graph. It lets clients fetch exactly what they need with one request, even across multiple APIs.
- If you receive an
To make requests to your router, open
http://localhost:4000
in your browser.Copy and paste the following query in the central Operation panel to get all products:
GraphQLExample queryquery Products { products { id name description } }
Run the request by clicking the ▶️ Products button.
Your response should show a list of space-related products.
That's it—a fully functional graph! Next, you'll add and transform more data from the /products
endpoint.
Step 2. Add new fields
To expose additional data through your graph, you update the GraphQL schema:
Open the
products.graphql
file generated byrover init
and read the comments to get a brief overview of the file.Notice the selection mapping:
GraphQLproducts.graphqltype Query { products: [Product] @connect( source: "ecomm" http: { GET: "/products" } # GET, POST, PUT, PATCH, DELETE. selection: """ $.products { id name description } """ ) }
This is the part you'll update next.
The best way to simultaneously explore what data an endpoint offers and update your Connector accordingly is with the Connectors Mapping Playground. This link is prepopulated with data from the
/products
endpoint and embedded below:
Try adding
slug
to the selection mapping Mapping panel.GraphQL$.products { id name description slug }
Notice the products in the Results panel now include
slug
s.Imagine you also want your graph to expose a new field called
primaryTag
. This field should return thename
of the first tag in a product'stags
array. Using the->first
method, play with the Mapping panel until you've achieved this.Check your selection mappingGraphQLMapping$.products { id name description slug primaryTag: tags->first.name }
Update your
products.graphql
:Add
slug
andprimaryTag
fields to yourProduct
type.Copy and paste the mapping you've finessed in the Mapping Playground into the
selection
field of your Connector.Save your
products.graphql
.
Check your ConnectorGraphQLproducts.graphqltype Product { id: ID! name: String description: String slug: String primaryTag: String } type Query { products: [Product] @connect( source: "ecomm" http: { GET: "/products" } selection: """ $.products { id name description slug primaryTag: tags->first.name } """ ) }
To test the updated schema as expected, run the following query in your Sandbox, which should still be running on http://localhost:4000.
GraphQLSandbox requestquery products { products { name slug primaryTag } }
You should see the new fields you mapped from the /products
endpoint. Congrats—you've integrated more data into your graph!
Next steps
Learn more about what the mapping language is capable of.
Check out the Apollo Connectors Community to integrate prebuilt Connectors into your graph.