Event triggered GraphQL
Tags: graphql, tutorial
Published: 2019-12-13 19:47:15

---
title: Event triggered GraphQL
published: true
date: 2019-12-12 17:16:29 UTC
tags: graphql, tutorial
canonical_url: https://blog.logrocket.com/event-triggered-graphql/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/qleveo2n3yc4xt5ucxw1.png
---
**Written by [Daniel Phiri](https://blog.logrocket.com/author/malgamves/)**✏️

### Dynamic

I saw this amazing talk by Tanmai Gopal, one of the founders at JAMStack SF, on [Making Static React Sites Dynamic](https://youtube.com/) .

“I could probably cook up a Vue alternative,” I thought.

My [Blog](https://blog.malgamves.dev) happens to be built with Gridsome, a Vue powered static site generator for building blazing-fast static websites.

I’ve played around with Hasura in the past: it’s a super fast GraphQL server that gives you instant, realtime GraphQL APIs over Postgres. So we’re going to build something with Gridsome, deploy it, and then make a portion of it dynamic (sort of) with GraphQL and Netlify.

I have a list of books I’ve read in a GraphQL API and I want to use that as a data source for the content on our Gridsome site. We want this site to update dynamically when ever we add a new book to the list or take one out.

[![LogRocket Free Trial Banner](https://i0.wp.com/blog.logrocket.com/wp-content/uploads/2017/03/f760c-1gpjapknnuyhu8esa3z0jga.png?resize=1200%2C280&ssl=1)](https://logrocket.com/signup/)

I’ll show you how.

TL;DR: Hasura Event Triggers  
[Demo](https://codesandbox.io/s/gridsome-remote-graphql-source-dkhn1?fontsize=14&hidenavigation=1&theme=dark)

I left my GraphQL endpoint open for the sake of this tutorial, but it’s not advisable for a production environment.

Have a look at [this post](https://dev.to/hasurahq/hasura-authentication-explained-2c95) on securing your API by adding authorization and managing users.

We’ll start out by installing the Gridsome CLI tool with `npm install --global @gridsome/cli`.

When that’s done, we create a new Gridsome project with `gridsome create <project name>`.

After that, we `cd <project name>` and then run it locally with `gridsome develop`.

You’ll get a very simple page like this.

![Hello World](https://blog.logrocket.com/wp-content/uploads/2019/12/Hello-world-nocdn.png)

Now go to [this page](https://docs.hasura.io/1.0/graphql/manual/getting-started/heroku-simple.html) and click Deploy to Heroku. You might have to create a new Heroku account.

When that’s sorted, you’ll be prompted to create an app name. Key one in and then click Deploy.

Once it’s deployed, head over to `<your-app-name>.herokuapp.com` to access your Hasura API Explorer.

It should look this:

![Hasura](https://blog.logrocket.com/wp-content/uploads/2019/12/Hasura-nocdn.png)

On our API Console, we’ll head over to the **data** section and click **add table**.

We then create a table called books and go on to create the following columns:

- `id, Integer (AutoIncrement), Unique`
- `name, text`
- `author, text`

Set `id` as the primary key. Then, click create.

Now that we’ve got a books table, we’ve got to insert data into it.

In the data section, click on the books table and pick the insert data tab. Feel free to add any books and authors you enjoy. Mine looks something like this:

![Hasura](https://blog.logrocket.com/wp-content/uploads/2019/12/Hasura-run-query-nocdn.png)

We’re done with the Hasura API console for now.

Next up, we want to be able to query a remote GraphQL source.

First, we need to install the Gridsome source plugin. We’ll enter `npm install gridsome-source-graphql` to do so. Once that’s done, we need to edit our `gridsome.config.js`.

We then paste in the following code:

```jsx
plugins: [
    {
      use: 'gridsome-source-graphql',
      options: {
        url: 'https://example.com/api',
        fieldName: 'puppies',
        typeName: 'puppyTypes',

      },
    },
  ]
```

Be sure to change `url`, `fieldName`, and `typeName` (although the latter is optional).

You can get the `url` from the Hasura API console, i.e  
_<your app name>herokuapp.com/v1alpha1/graphql_.

I’ll name `fieldType` booksList and leave the `typeName` blank. They’re important, so remember them.

Now let’s navigate to the `src > pages > About.vue<`. This is where we want to display the data.

Gridsome uses GraphQL to query data that it displays on pages.

After the closing `<template>`, we paste the following page query:

```jsx
<page-query>  
query {
  books {
    author
    id
    name
  }
}
</page-query>
```

The query fetches the author, id, and name from the books table. Now we need to display the data we’re querying.

To do so, add the following code below the sole `<p>` tag:

```jsx
<h1>Books Read by Me</h1>
<div v-for="book in $page.books" v-bind:key="book.id">
   <li>{{ book.author }} - {{ book.name }}</li>
</div>
```

We use `$page.books` to access the query response and parse through it with the v-for directive, storing each value in the book variable. We then use this to display the book author and book name.

This also happens to be a really nice way of understanding how Gridsome displays data. When we run this locally and click `about`, we can see the books and their authors displayed.

If you head back to the Hasura API Console and insert one row into the books table, you’ll notice that the list on the site updates.

This only happens locally because the data source is continuously refreshed.

When you deploy the app, only the data available on deploy is displayed, meaning when we insert a row in the API console it will not show until the site is rebuilt.

To solve this, we’ll leverage Hasura Event Triggers to trigger a Netlify build that will then update the data in your database.

You will need to push your code to GitHub or GitLab and connect your repository to Netlify.

Gridsome put together a great resource to help out if you have any trouble doing so. Check that out [here](https://gridsome.org/docs/deploy-to-netlify/).

Once deployed to Netlify, we need to create a build hook.

I’ll call mine **hasura-event-trigger**.

When you create one, you get a URL that triggers a build.

Copy this URL and head back to your Hasura API console.

This time, we’ll head over to the events section.

Hasura event triggers capture events on specified tables and invoke webhooks to carry out any custom logic.

Events could be inserts, updates, or deletes on a row. They also give you the flexibility to add manual triggers.

We want to trigger a rebuild whenever we delete or add a book. After clicking `create`, we name it `author-update`, select the books table, tick `insert`, and update as trigger operations.

We’re then asked to add a webhook URL. This is the build hook URL we copied from Netlify.

After pasting that in, click `create`.

Now, every time we insert or delete a row (be it manually in the API console or with a mutation using another web app), the event is triggered and a new build will start.

This updates the side content—some would say dynamically.

Add or delete a few rows to trigger builds and your events tab will look like this:

![books update](https://blog.logrocket.com/wp-content/uploads/2019/12/books-update-nocdn.png)

The builds on Netlify also show what triggered them.

![](https://blog.logrocket.com/wp-content/uploads/2019/12/published-nocdn.png)

### Conclusion

Now we have a sort of dynamic static site that leverages Hasura GraphQL Event Triggers.

Check out the [full repo](https://github.com/malgamves/gridsome-remote-gql-source) on my GitHub and feel free to [tweet](https://github.com/malgamves) me any questions.

* * *
## 200’s only! Monitor failed and slow GraphQL requests in production

While GraphQL has some features for debugging requests and responses, making sure GraphQL continues to serve resources to your app in production is where things get tougher. If you’re interested in ensuring requests to the backend or 3rd party services are successful, [try LogRocket](https://logrocket.com/signup/). [![LogRocket Dashboard Free Trial Banner](https://i1.wp.com/blog.logrocket.com/wp-content/uploads/2017/03/1d0cd-1s_rmyo6nbrasp-xtvbaxfg.png?resize=1200%2C677&ssl=1)](https://logrocket.com/signup/)[https://logrocket.com/signup/](https://logrocket.com/signup/)

[LogRocket](https://logrocket.com/signup/) is like a DVR for web apps, recording literally everything that happens on your site. Instead of guessing why problems happen, you can aggregate and report on problematic GraphQL requests to quickly understand the root cause.

LogRocket instruments your app to record baseline performance timings such as page load time, time to first byte, and slow network requests as well as logs Redux, NgRx. and Vuex actions/state. [Start monitoring for free](https://logrocket.com/signup/).
* * *
The post [Event triggered GraphQL](https://blog.logrocket.com/event-triggered-graphql/) appeared first on [LogRocket Blog](https://blog.logrocket.com).