Build and deploy a simple Apollo GraphQL federated schema using AWS EKS (Kubernetes)-Pt.2
This tutorial is part 2 of this series.
- Part 1: Building a simple GraphQL federated schema
- Part 2: REST API Data fetching with REST Data Source
- Part 3: Deploy using AWS EKS Kubernetes & AWS Fargate
In this section we’ll add the ability to retrieve crypto currency data from Coinmarketcap’s REST APIs using Apollo REST Data Source, and extend our Wallet API to use real data 🚀.
What is Apollo REST Data Source?
It’s simply a subclass that allows us to fetch data from a given REST API data source, in this case CoinMarketCap.
Why use Apollo REST Data Source?
We could use an HTTP client such as Axios or simply fetch data using the Fetch API, but the advantage of using the
RESTDataSource library over using an abstracted HTTP client is request deduplication and resource caching.
Request duplication happens when our resolvers request data from the same resource multiple times. For example, if your REST API exposes data about an event and it’s attendees, we know that an event could have multiple attendees and each attendee could attend multiple events. So having the power to cache our data and not make duplicate requests to the data source to resolve the data is a powerful set of tools to have out of the box. Using Axios or the Fetch API would require us to do a lot of cache and request optimisation.
- Gateway update and API Key
First we’ll update the Gateway /apollo-federation-starter/gateway.js to support the new service and send an example auth header to demonstrate how we could secure access to our implementing services. The screenshots below will illustrate how we’ll approach the changes (full code snippet below screenshots)
We’ll need to add the new service to the service list:
Add an example auth token to the gateway’s context:
We’ll retrieve the auth token from the context and add it to all requests made to implementing services by adding it to the auth token to request headers
In a production environment we would handle secret keys very differently!
Now that we’ve identified the key server updates let’s jump back into the code and write the full Gateway server definition:
npm install email@example.com @firstname.lastname@example.org email@example.com
Now that our gateway has been extended to support the new Cryptocurrency service let’s define it.
Signup to coinmarketcap’s developer API here: https://pro.coinmarketcap.com/signup
Once you’ve got your API key, run the following command from the root directory /apollo-federation-starter
Open the .env file in your editor and add the following lines, replacing “*YOUR API KEY*” with your API key from coinmarketcap.
COINMARKETCAP_API_KEY=*YOUR API KEY*
Open the /cryptocurrency-api/index.js file in your editor, which will be used as the entry point into this service and add the following server definition:
Next we’ll define our cryptocurrency schema by first creating the following folder and file in the root of the /cryptocurrency-api directory
mkdir schema && cd schema && touch index.js
Open cryptocurrency-api/schema/index.js in your editor and add the following schema definition:
Next we’ll define out REST data source by creating a class that acts as an interface into the Coinmarketcap REST API. Let’s start by runnning the following command in the cryptocurrency api root directory /cryptocurrency-api :
mkdir rest-apis && cd rest-apis && touch index.js && touch coinmarketcap-api.js
Open /cryptocurrency-api/index.js and add the following lines:
Open /cryptocurrency-api/coinmarketcap-api.js and add the following lines:
Let’s have a look at the data that the ‘getCurrencyQuote’ method should return. Jump into your browser and paste the following (substituting your API key):
https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?&convert=USD&symbol=BTC&CMC_PRO_API_KEY=*YOUR API KEY*
You should see a Bitcoin quote returned as a JSON object. This gives us a great insight into our data structure so we can start working on how we approach resolving the data. We’ll define our cryptocurrency resolver by creating the resolver file. From the root of the /cryptocurrency-api directory, type the following:
mkdir resolvers && cd resolvers && touch index.js
Open /cryptocurrency-api/resolvers/index.js in your editor and add the following resolver definition:
From the root of the /cryptocurrency-api directory, you should now be able to start the GraphQL playground and query a single Crypto price quote:
Start the api:
npm run start
Visit http://localhost:5003 in your browser and run the following query:
Stop the server on http://localhost:5003.
Now that we can retrieve live crypto prices, let’s extend our Wallet-api to show the live value of the currencies in a user’s wallet.
Let’s start by simply extending the CryptoAssetData Type and defining it in the Wallet-api schema. Open the /wallet-api/schema/index.js file and update it with the following:
Next we’ll define how the wallet-api should resolve the crypto currency data. Open /wallet-api/resolvers/index.js and update it with the following:
Now, from the root directory of the project /apollo-federation-starter let’s start the services followed by the gateway by running the following command in your terminal:
npm run start-services
Open a new terminal window and run:
npm run start-gateway
Open your browser, navigate to the Gatway playground site http://localhost:4000/
Now when you run the query to retrieve a user’s wallet information (Don’t forget to add the query variables), it will contain a live price quote from Coinmarketcap and calculate the value of the user’s holdings 🤑🚀💸
That’s it for part 2!
In Part 3 we’ll learn how to deploy our federated graph using AWS EKS(Kubernetes).