Skip to main contentCarbon Design System

3. Using APIs

This step takes our static components and populates them with data from the GitHub GraphQL API – loading states and all. We’ll be displaying Carbon repository information in a data table.


The GitHub GraphQL API is very well documented, and even though the focus of this tutorial isn’t learning and using GraphQL, it’s a great opportunity to fetch Carbon-related data for this Carbon tutorial.

To do so, we’ll be using Apollo Client, the front-end component of the Apollo Platform. Apollo provides several open source tools for using GraphQL throughout your application’s stack. Apollo Client is a sophisticated GraphQL client that manages data and state in an application.

A preview of what you will build (see repositories page):

Fork, clone and branch

This tutorial has an accompanying GitHub repository called carbon-tutorial-vue that we’ll use as a starting point for each step. If you haven’t forked and cloned that repository yet, and haven’t added the upstream remote, go ahead and do so by following the step 2 instructions.


With your repository all set up, let’s check out the branch for this tutorial step’s starting point.

git fetch upstream
git checkout -b vue-step-3 upstream/vue-step-3

Build and start app

Install the app’s dependencies:


Then, start the app:

yarn serve

You should see something similar to where the previous step left off. Stop your app with CTRL-C and let’s get everything installed.

Install dependencies

We’ll shortcut this using the Vue CLI, if you’d like more information head over to Vue Apollo Installation for details.

Note: If you have not yet installed the Vue CLI, you will need to install the Vue CLI before running the Vue Apollo Installation.

Install the following

  • apollo-boost - package containing everything you need to set up Apollo Client
  • graphql - parses your GraphQL queries
  • vue-apollo - Apollo integration for Vue

Using the command:

vue add apollo

At the following prompts answer ‘No’ to each of the questions.

  • Add example code? No
  • Add a GraphQL API Server? No
  • Configure Apollo Engine? No

Create access token

You’ll need a personal access token from your GitHub account in order to make requests to the GitHub API. Check out this guide to see how to get one.

When you get to the scope/permissions step, you can leave them all unchecked. We don’t need any special permissions, we just need access to the public API.

Once you have your token, we need to put it in a place where create-vue-app can use it. When your application is being built and developed, create-vue-app will parse environmental variables in any file that starts with .env and make them available under process.env.MY_VARIABLE.

One caveat is that we need to start our variables with VUE_APP_. You can read more about environmental variables in create-vue-app’s guide.

Note: If you already have a valid GitHub Personal Access Token, you can use that token here.

Since we don’t want to commit this file to Git, we can put it in .env.local which is in our .gitignore list. Your file should just have a single line like this one, where the xs are replaced with your unique token.


Go ahead and start your app with yarn serve, or, if your app is running, you’ll need to restart it to get access to this token.

Connect to Apollo

The vue-apollo plugin has made a number of changes to our project.

If you open src/main.js you will see that the CLI has updated this file with the following:.

import { createProvider } from 'vue-apollo';
new Vue({
apolloProvider: createProvider(),
render: (h) => h(App),

This is loading from a file the CLI created for you src/vue-apollo.js which we need to update to target the github api.

Update the following values:

// Use our access token
// Target github api
const httpEndpoint =
process.env.VUE_APP_GRAPHQL_HTTP || '';

Update only the wsEndpoint and getAuth properties of the defaultOptions object:

const defaultOptions = {
// set wsEndpoint to null
wsEndpoint: process.env.VUE_APP_GRAPHQL_WS,
// Use the form expected by github for authorisation
getAuth: (tokenName) => `Bearer ${tokenName}`,

Fetch data


Add the following imports to the top of the script section of RepoPage.vue:

import gql from 'graphql-tag';


Next we’ll assemble our GraphQL query to fetch only the data we need from the GraphQL API. We’ll do this using the gql helper we just imported. The gql helper lets you write GraphQL queries using interpolated strings (backticks) in JavaScript. In addition, we’ll be using the Query component from vue-apollo which gives us some great information about our query’s loading state in addition to the data.

You can use GitHub’s explorer tool to write and test your own queries. Try copying the query below and experiment with changing the properties. You can also click the “Docs” button in the top right of the explorer to view all of the available data and query parameters.

If you’d like some more information regarding writing queries and using the Query component, we recommend Apollo’s documentation on this topic.

Add this after your imports:

const REPO_QUERY = gql`
query REPO_QUERY {
# Let's use carbon as our organization
organization(login: "carbon-design-system") {
# We'll grab all the repositories in one go. To load more resources
# continuously, see the advanced topics.
repositories(first: 75, orderBy: { field: UPDATED_AT, direction: DESC }) {
nodes {

Next, we need to configure apollo in our component script, adding the following after the data() declaration.

apollo: {
organization: REPO_QUERY

At this point, we should run our query view the raw the results to verify that the request is working.

In RepoPage.vue add the following before the RepoTable tag.

{{ this.organization }}

When the data loads you should see the response rendered on your repository page. If not, check the console to see if there are any errors and fix.

Revert this last change and continue.

This data is not quite in the format our RepoTable component is expecting so we’ll use a computed property to transform it. Computed properties in Vue cache and watch their reactive dependencies for us.

Remove the ‘rows’ constant and its use in the data declaration, and add this computed property.

computed: {
rows() {
if (!this.organization) {
return [];
} else {
return => ({
stars: row.stargazers.totalCount,

At this point you have a working table but the links column clearly isn’t what we want.

Helper component

This column in the data table will be a list of repository and home page links, so let’s create a component called LinkList.

Add the following to create your component:

A template section:

<ul class="link-list">
<cv-link :href="url">GitHub</cv-link>
<li v-if="homepageUrl">
<cv-link :href="homepageUrl">Homepage</cv-link>

A script section:

export default {
name: 'LinkList',
props: {
url: String,
homepageUrl: String,

And a style section:

.link-list {
display: flex;

Now let’s make use of this component in our RepoTable component.

At the top of the script section import the link list component:

import LinkList from './LinkList';

And below the name of the component add:

components: { LinkList },

Then make use of it in our template replacing:

v-for="(cell, cellIndex) in"


<cv-data-table-cell v-for="(cell, cellIndex) in" :key="`${cellIndex}`">
<template v-if="!cell.url">
<link-list v-else :url="cell.url" :homepage-url="cell.homepageUrl" />

Here in order to switch between the standard rendering of a data cell we’ve wrapped our standard {{cell}} rendering in a template tag. The template tag is non-rendering so it will disappear, leaving us with the same content as before.

Using the v-if and v-else directives we switch based on the contents of the cell between the standard rendering and the LinkList component.

Checking our output again, you should now see the LinkList component rendering the final column.

Next we’ll update our row description. Update the computed property data() in RepoTable.vue to have the following description:

description: row.description

Check the output again and you should find the descriptions are updated.

After this many refreshes you may have noticed a slight delay in the data loading. As outlined in the documentation, all components contained under one with an apolloProvider have a $apollo attribute. As we added the apolloProvider to our app when creating the Vue instance it is available to us everywhere.

We can use the property to react to loading state.

First let’s demonstrate that this works.

Pass the loading state into our RepoTable component by updating the template with the following:

title="Carbon Repositories"
helperText="A collection of public Carbon repositories."

Next add this property to the RepoTable component:

props: {
headers: Array,
rows: Array,
title: String,
helperText: String,
loading: Boolean,

Making use of the property to display a loading message.




<div v-if="loading">Loading...</div>

Here we have made use of the v-if and v-else directives to switch content based on the state of $apollo.loading. If you refresh your app you should see this take effect.

Now that we know this is works let’s try something a bit more sophisticated and replace the div containing our loading message with use of the CvDataTableSkeleton component.


We need to tell the loading skeleton how many rows to render, so let’s use 10 skeleton rows to prepare for the next enhancement…

Add pagination

Pagination! Instead of rendering every repository, let’s add pagination to the data table to only render 10 at a time. Depending on your specific requirements, you may need to fetch new data each time that you interact with the pagination component, but for simplicity, we’re going to make one request to fetch all data, and then paginate the in-memory row data.

Let’s start by adjusting our PageTable component template to add pagination. If you review the storybook notes you’ll see that pagination is added to the data table by supplying a pagination object and listening for pagination events as follows.

:pagination="{ numberOfItems: this.totalRows }"
@pagination="$emit('pagination', $event)"

Note: :prop is an abbreviation of v-bind:prop. @event is an abbreviation of v-on:event.

In the pagination event we’ve used $emit and $event to re-raise the pagination event to our RepoPage component so that it can arrange to pass only the rows we want to see to the RepoTable component.

We also need to add the totalRows property used in the data tables pagination property.

totalRows: Number,

Next to our RepoPage component, let’s first update our template by updating our RepoTable use with the following attributes.




Next in the data property of our component add values for pageSize, pageStart and page into the data met