Building full-stack GraphQL applications with Hasura and Vue 3

Rate this content
Bookmark

The frontend ecosystem moves at a breakneck pace. This workshop is intended to equip participants with an understanding of the state of the Vue 3 + GraphQL ecosystem, exploring that ecosystem – hands on, and through the lens of full-stack application development.

Table of contents
- Participants will use Hasura to build out a realtime GraphQL API backed Postgres. Together we'll walk through consuming it from a frontend and making the front-end reactive, subscribed to data changes.
- Additionally, we will look at commonly-used tools in the Vue GraphQL stack (such as Apollo Client and Urql), discuss some lesser-known alternatives, and touch on problems frequently encountered when starting out.
- Multiple patterns for managing stateful data and their tradeoffs will be outlined during the workshop, and a basic implementation for each pattern discussed will be shown.
Workshop level

NOTE: No prior experience with GraphQL is necessary, but may be helpful to aid understanding. The fundamentals will be covered.

FAQ

GraphQL is a query language for APIs that provides a more efficient, powerful, and flexible alternative to the traditional REST API. It allows clients to request exactly the data they need, making it possible to get all required data in a single request, whereas REST typically requires multiple requests to different endpoints to gather related data.

Hasura is a GraphQL engine that instantly provides a real-time GraphQL API on top of a new or existing database. It allows you to connect multiple databases, manage CRUD operations with real-time capabilities, and handle complex queries efficiently. Additionally, it integrates authentication and permission management, making it easier to secure your data.

Key features of Hasura include automatic generation of CRUD GraphQL APIs from your databases, real-time queries through subscriptions, built-in support for handling database migrations, permission and role-based access control, and the ability to join data across multiple databases.

GraphQL subscriptions in Hasura allow you to maintain a real-time link with the database. When data changes in the database that matches the subscription query, Hasura sends the updated data to the client instantly. This feature is particularly useful for applications that require real-time functionalities like live updates.

Hasura supports various authentication methods including webhooks and JWT (JSON Web Tokens). You configure Hasura with your authentication provider, and it validates JWTs or webhook responses to authenticate and authorize users. This system allows you to implement complex security models including role-based access control.

GraphQL Code Generators are tools that automatically generate code for client-side GraphQL operations. You define your queries and mutations, and the tool generates corresponding types and hooks that you can use directly in your application. This helps maintain type safety and reduces boilerplate code.

Yes, Hasura can be connected to existing databases. It provides a flexible and powerful way to instantly create a real-time GraphQL API on top of existing database schemas. This makes it an ideal choice for projects that need to leverage existing database assets.

GraphQL Zeus provides a unique approach to working with GraphQL APIs by generating an SDK that allows for type-safe, dynamic query construction. It enables you to write queries that automatically adjust their types based on the fields you request, ensuring type safety and reducing the chance of runtime errors.

In GraphQL applications using Apollo, state can be managed using Apollo Client's local state management features. This allows you to manage both server-side and client-side state through a unified interface, using GraphQL for all data management. The approach can optimize data fetching and caching, although it may introduce complexity regarding cache management.

Gavin Ray
Gavin Ray
115 min
19 Oct, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

GraphQL is a powerful tool for building APIs, offering flexibility in data retrieval and real-time functionality. It simplifies API documentation and enhances development with type checking and code generation. Hasura is a popular GraphQL engine that supports subscriptions, permissions, and database integrations. The Workshop covers topics such as setting up GraphQL APIs, handling permissions and security, integrating authentication, and using GraphQL with Vue.js. It also discusses various libraries and tools for working with GraphQL APIs and state management.

1. Introduction to GraphQL API

Short description:

We'll cover the comparison between RESTful architecture and building GraphQL APIs. GraphQL engine by Hasura allows you to create a unified data graph by spinning up a real-time CRUD API with auth and other features. It's open source. In a REST API, making an API call requires multiple calls and can lead to complex API architecture. GraphQL solves this problem by automatically providing the data you request.

So, um, we'll try to go at a moderate pace. Again some people might be super familiar with this, others not so much. But, uh, yeah, we're just gonna cover this. So there's a couple of really awesome things about GraphQL API, GraphQL and one of them is the ergonomics of the API. So this is basically going to walk through, um, RESTful architecture in comparison with building GraphQL APIs, which I think is something that everybody will be able to draw parallels sure we've all built a CRUD app, uh, in our lives.

So I am a technical evangelist for a company called Hasura. It's GraphQL engine. Um, really cool. You basically, uh, it allows you to take a data source, so that could be Postgres, could be MySQL, SQL Server, it could be other APIs that can be REST or a GraphQL and it's sort of spins up an automatic real-time CRUD API with auth and some other neat stuff on top of it and lets you provision permissions, uh, and join data across data sources as if it were just one big, uh, unified data graph. Uh, oh, and it's also open source.

So uh, we'll talk through an API call. So in a REST API, when you make an API call, right? Um, you do something like get API, slash user, and you pass an ID and you'd get this, uh, HTTP response. Uh, let's check in. Sorry. Does anybody have sound? Sorry, there's like a ton of messages. Uh, does my audio work for everybody? Yes. Yes. Okay. Okay. Thank you. Thank you everyone. Um, okay. So yeah, so you have some app, uh, and your app is going to make a call to an API. I get requests might look like this and it's, it's going to return an HTTP response, right? Some, some Jason that looks like this, right? So we have Elmo. If we want more info, right? Like, so what if we want Elmo's, uh, addressed, then we need to hit the address API and we need to give it the user ID that we just asked for over rest. So that's two API calls. And that only gets worse. Um, and you can parallelize these API calls the rest, you can batch them. You can make bespoke API's that return, you know, exact data shapes. But essentially this problem is going to remain no matter what you do because that's the nature of rest. So I'm gonna move this chat window.

If you think about a page, right? You're not rendering just an isolated entity. So if you think about a user and you're having a hierarchical data model, you're probably going to want to render that user. You might want to render their address, you might want to render their order history, you might want to render a list of friends. There's this cascading hierarchy of dependencies and interrelationships, and all these need to be resolved against the API requests. So let's say we have a problem, right? We get a new... Like a mobile app designed for a page and it's really complicated, and it would require like eight REST calls. So we go back to our API developers and they're like, hey... Could you just write me like this one special API call that returns this exact data shape? Because that would make my life so much easier. And so now you get this new bespoke API call, right? So they say, okay, fine, sure. And you get this user info API endpoint and then now you have Elmo and his address. So the problem is, if you continue to do this, what's going to happen is that your API architecture is going to turn into absolute spaghetti. And so you wind up with more and more of these bespoke API calls, or you're just having more and more cascading sort of like intermingled and hierarchical dependent REST calls. And so one thing that you could do is say, well, okay, you know what, I'm going to implement some kind of like fields filter. So I'm just going to ask for the fields that I want. And that works. It's a little bit of a duct tape hack, but it works. But with GraphQL, you don't have to do that. With GraphQL, the way that it works is the data that you want is automatically works is the data that you want, that's the data you asked for. So if you want elmo, you want his name, you want his address in the street, you just send a payload over the wire.

2. GraphQL API Basics

Short description:

API models are often graph-like, making relational databases popular. GraphQL allows for flexible data retrieval with control over the hierarchy. GraphQL is a specification, not a special technology, for handling API requests and responses. It uses a single endpoint and supports POST requests with query and variables fields. Mutations in GraphQL act as posts, patches/puts, and deletes. Query is for querying information, while mutations are for mutating data.

This is, Hey, I'd like JSON in this shape and the API says, okay, here you go. It's a little bit more complicated than that, but that's the premise. Um, and so there's a couple of key insights here. And one of them is that almost always your API models, uh, are somewhat graph like, uh, and this is why relational databases are so popular. Um, it's not very often that you find yourself, uh, writing entities that don't have some sort of relationship that are just flat models and disconnected from everything.

When you're traversing this data graph, um, you want to control the data that you get, because you don't want to, you don't want to, you don't want to pull this entire hierarchy of data. Uh, but at the same time you really want the flexibility to say like, uh, I need this model, but then also just like maybe one or two fields from a child or a parent model. Um, and GraphQL allows you to do that.

So the way that a graph, the way the GraphQL works is, uh, you have three operation types. So you have queries mutations and subscription subscriptions and, um, where he's, uh, queries map to the, uh, get verb, right. When you think about an arrest API, they fulfill the role of what would typically be used for get requests. So, um, this GraphQL query is saying, Hey, I want a user. I would like their ID name. I want the user's address, uh, relationship. Um, and I want the street, uh, from user address, right. So user address the street. And I want that to be only for the user with the ideas. One and kind of the way that this works under the hood, um, is, is not very, uh, there's not a lot of magic here. Um, so GraphQL is just a regular, uh, HTTP requests. So if you know how to make a post request or a get request and a pull request, using the curl or Postman or using the fetch API, you already know how to use GraphQL. Um, a lot of people think that GraphQL is some kind of like special technology or like protocol, uh, that you support, but it's not, uh, it's just a specification for how your API should deal with requests and responses just like REST.

And so the way that GraphQL works is you have a single endpoint and, uh, it takes post requests. Typically you can allow it to take requests, but it's unusual. And you basically just send a, uh, a fetch request to this API, that's a application slash JSON type. And there's only two fields, uh, that you need to put in here. So one is the query. So here's our query. And, um, this would just go in like a fetch, uh, a fetch requests, uh, body object in JavaScript. Uh, the other one, uh, that we'll probably see in a second is variables, uh, which will allow you to substitute an actual variable for things like the ID. And then the third one, uh, this is optional is giving it an operation name and that's mostly used for things like, uh, metrics, analytics and tracking, just for you to be able to tell what operations are being run. It's like, um, it's like an operation tag, right? So we send the graphQL query as a string in Jason to our API. And what we get back, uh, is just a Jason object that looks exactly like the, uh, query object that we sent, uh, in the request. So When he would write it to a rest API, uh, you typically make a post request to something like slash API slash to do, uh, you'd give it a to do, and then it would send this 200. Okay. And it'd be like, all right, man, I made this to do, and here's your new to do ID. Um, the way you do this in graph QL is you use mutations and mutations to fill the role of multiple HTTP verbs. Um, which we might see in a second. So mutations act as posts, mutations act as patch or put and mutations also act as delete. So, um, it might be obvious based on the name, but query is for querying information and mutations is for mutating, uh, data. So here we see some example mutation where we added to do, and it takes this a dollar sign is a variable. So we're saying it takes a to-do um, variable called new to do and it returns the ID. And then when we, uh, send this across in our HTTP request, the only other thing we need to do is give it a new variables key that has the new to do and the to-do we want. So, uh, you can see it right here is the new to do graph, graph QL. Let's back the slide. Um, now I want to go back. Uh, it's the same thing. So just take, take your post data and put it under the variable name and the mutation. And then again, you'll get this 200 response and it's going to give us this ID because ID is what we asked for. So, um, that might not be very intriguing.

QnA

Watch more workshops on topic

Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
Vue3: Modern Frontend App Development
Vue.js London Live 2021Vue.js London Live 2021
169 min
Vue3: Modern Frontend App Development
Top Content
Featured WorkshopFree
Mikhail Kuznetcov
Mikhail Kuznetcov
The Vue3 has been released in mid-2020. Besides many improvements and optimizations, the main feature of Vue3 brings is the Composition API – a new way to write and reuse reactive code. Let's learn more about how to use Composition API efficiently.

Besides core Vue3 features we'll explain examples of how to use popular libraries with Vue3.

Table of contents:
- Introduction to Vue3
- Composition API
- Core libraries
- Vue3 ecosystem

Prerequisites:
IDE of choice (Inellij or VSC) installed
Nodejs + NPM
Developing Dynamic Blogs with SvelteKit & Storyblok: A Hands-on Workshop
JSNation 2023JSNation 2023
174 min
Developing Dynamic Blogs with SvelteKit & Storyblok: A Hands-on Workshop
Top Content
Featured WorkshopFree
Alba Silvente Fuentes
Roberto Butti
2 authors
This SvelteKit workshop explores the integration of 3rd party services, such as Storyblok, in a SvelteKit project. Participants will learn how to create a SvelteKit project, leverage Svelte components, and connect to external APIs. The workshop covers important concepts including SSR, CSR, static site generation, and deploying the application using adapters. By the end of the workshop, attendees will have a solid understanding of building SvelteKit applications with API integrations and be prepared for deployment.
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Everything Beyond State Management in Stores with Pinia
Vue.js London Live 2021Vue.js London Live 2021
34 min
Everything Beyond State Management in Stores with Pinia
Top Content
When we think about Vuex, Pinia, or stores in general we often think about state management and the Flux patterns but not only do stores not always follow the Flux pattern, there is so much more about stores that make them worth using! Plugins, Devtools, server-side rendering, TypeScript integrations... Let's dive into everything beyond state management with Pinia with practical examples about plugins and Devtools to get the most out of your stores.
Welcome to Nuxt 3
Vue.js London Live 2021Vue.js London Live 2021
29 min
Welcome to Nuxt 3
Top Content
Explain about NuxtJS codebase refactor and challenges facing to implement Vue 3, Vite and other packages.
One Year Into Vue 3
Vue.js London Live 2021Vue.js London Live 2021
20 min
One Year Into Vue 3
Top Content
Vue 3 may still sound new to many users, but it's actually been released for over a year already. How did Vue 3 evolve during this period? Why did it take so long for the ecosystem to catch up? What did we learn from this process? What's coming next? We will discuss these questions in this talk!
Utilising Rust from Vue with WebAssembly
Vue.js London Live 2021Vue.js London Live 2021
8 min
Utilising Rust from Vue with WebAssembly
Top Content
Rust is a new language for writing high-performance code, that can be compiled to WebAssembly, and run within the browser. In this talk you will be taken through how you can integrate Rust, within a Vue application, in a way that's painless and easy. With examples on how to interact with Rust from JavaScript, and some of the gotchas to be aware of.
Vue: Feature Updates
Vue.js London 2023Vue.js London 2023
44 min
Vue: Feature Updates
Top Content
The creator of Vue js gives an update on the new features of the technology.
From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!