Build with SvelteKit and GraphQL

Rate this content
Bookmark

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

Scott Spence
Scott Spence
140 min
08 Dec, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This workshop focuses on using Svelte and SvelteKit to build a GraphQL-based CMS. It covers project setup, configuration, and working with GraphCMS. The workshop also explores fetching data from endpoints, styling with SvelteAdd, and working with blog post data and routing. Troubleshooting Tailwind CSS configuration and deployment with Versel are also discussed.

Available in Español: Construir con SvelteKit y GraphQL

1. Introduction to Svelte and Agenda

Short description:

Hi, my name's Scott. We are one of the first GraphQL based CMSs. We're all in on GraphQL. Let's take a quick look at the agenda. We'll create a front end and a back end. We'll query data with GraphQL. We'll display data in our Svelte project. We'll manage data with Svelte directives. We'll look at rooting in Svelte and endpoints. We'll cover deploying to production. Svelte is a compiler framework that builds HTML, CSS, and JavaScript. SvelteKit is the one-stop shop for building projects in Svelte.

Hi, my name's Scott. I'm a Developer Advocate over at GraphCMS. We are one of the, if not the, only, sorry, the first, sort of, GraphQL based CMSs out there. We're all in on GraphQL and that's pretty much how we deliver our content for our users.

So, let's take a quick look at the agenda. And I know I've not updated the date on these slides. It's showing as So, we're going to create a front end and a back end. We're going to look at some querying data with GraphQL. We're going to take a look at displaying that data in our Svelte project. We'll take a quick look at Svelte directives for managing data on the Svelte file, once it's coming to the component. We'll be looking at rooting in Svelte, along with endpoints. And we'll take a look at deploying to production, if we have enough time.

Okay. So, what is Svelte? So, it's a compiler framework which allows you to break up your projects into two components, much like Vue and Angular. What is different with Svelte as opposed to Vue is there's no virtual DOM. Svelte builds HTML CSS and JavaScript in the build step, so what the browser gets is just plain old HTML, CSS and JavaScript, nice and easy to manage. Svelte isn't particularly new, V1 was released in late 2016, around November I think. So what I'm going to be concentrating on is SvelteKit. It is going to be the one stop shop for building any of your projects in Svelte.

2. SvelteKit and Project Setup

Short description:

At the moment, if you're going to create a new project with SvelteKit, it's still in public beta. SvelteKit provides modern web development best practices out of the box, including code splitting and routing. Svelte is like React for vegans, offering HTTP endpoints and file-based routing. Svelte also introduces Visvelte, a superset of HTML, which allows for reactivity within components. The each directive is used for looping around objects. Let's move on to the front end and initialize a Svelte project using the terminal. We'll be using Versell as the deployment target. The project includes a README, package.json, and JS config file for defining paths. We'll also configure the prettier file.

So at the moment if you're going create a new project with SvelteKit, at next, Svelte at next and give it a project name. Once it goes to V1 because SvelteKit is still in public beta, once it goes to V1, at next it will fall away and how anyone builds a project in Svelte. So there's modern web development best practices out of the box with SvelteKit. It's got code splitting built in and fire routing and all the niceness as, you know, we as front end developers are accustomed to.

So Svet is React for vegans. Love it. You get HTTP end points. We're going to go into detail on them. So you have HTM points, you get your get, what HTM points, get, put, delete. But it's not delete, because delete is a reserve word in JavaScript, so it's just DEL. So you can manage stuff that way, which is great. I've already covered file based routing. And it's very similar to NextJS or Next. But you use this instead of React or Vue. Okay. I don't think I sent out the resources for this. So I'm going to gloss over this and skip over to the next slide here.

So Visvelte, which I didn't cover, it's a super set of HTML. So this would look very familiar if you're coming from a sort of an HTML CSS and JavaScript background, where we can define things in some script tags here. We can define our count variable and then increment it and decrement it with sorry, just increment it here with the on click for our button here. And we have declarations. So this is how we get around state changes within a component. We use this dollar colon syntax here. If you're not familiar with it, I know it might look a bit funky, but this is like a JavaScript label. It's a valid JavaScript. But it's not used anywhere. So Rich Harris and the team have taken it on themselves to use this for reactivity within components. So we've got directives here, which is we've got an if block. We can use else as well. So we use the curly braces to define our directive and then we can start working with it within those. We're going to come to using those in our project very soon. So, yeah, this is going to be like the meat and potatoes for the each directive, which you just use for looping around blocks of like for looping around objects. So let's get started. I think these are just toilet slides now. Let's just go to the front end. I'm going to need a terminal. So everyone okay with, let's just bump up this font size here. So let's see. Everyone see the screen okay? Thumbs up from Robert, thank you. Um, okay. So, let's go into my example projects here. And I'm just going to NPM init Svelte next and what I'm going to do here, we're going to say computation.v1. Zip code for 20. 21, let's see, this is going to create our Svelte projects inside a folder defined as GraphQL Galaxy Workshop 2021 so let's hit enter here. And first I'm just going to say skeleton projects. We're not going to use TypeScript or ES Lint. If I go into the workshop folder now, change into here, it just changed my view. If anyone's got any issues just shoot out, possibly at me in the Discord so in this channel here. Move this over to the side. Let's just switch back on here. Right, let's take a quick look at this project here. So if I go into VS Code, my code editor. Right, so we've got our Svelte config. So this is a new addition to the Svelte Kit starter. This is Adapto Auto. So in the past, I still need to look into this, but I know for the purposes of this I think this will be fine. With Svelte Kit, it doesn't really have any opinions on like deployment targets so there's several available. Let's see if we can get one up here. And again, if there's any issues to shout, I'm just going to try my biggest possible here. And here we go, Adapters. So I'm not sure if these have been updated since the inception of this adaptor also here, but we've got build environments for Netlify, Versell, and Cloudflare, and of course you can build your own adapter if none of those fit. For the purposes of this workshop, I think I'll just be using Versell, and if there's anything specific you need to know about any other ones, if you're just going to be doing a pure static build, there's some gotchas around that as well, so just shout if there's anything you need information on with regards to that. Anyway, moving on, we've got our README, we've got our package.json here, everything is installed as a dev dependency. There's no run time. I'm talking, I'm actually looking at my camera, and I realise there's no camera on, so I'll stop doing that. So we've got a JS config file here. This is so we can define like our short paths for our lib, we can also put in components, anything else you want to define this will give us a little bit of syntactic sugar for importing files into our projects. We've got a prettier file here. I'm just going to replace this with my own configuration. I'm going to need to install as well.

Watch more workshops on topic

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.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura
Building GraphQL APIs on top of Ethereum with The Graph
GraphQL Galaxy 2021GraphQL Galaxy 2021
48 min
Building GraphQL APIs on top of Ethereum with The Graph
WorkshopFree
Nader Dabit
Nader Dabit
The Graph is an indexing protocol for querying networks like Ethereum, IPFS, and other blockchains. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.

In this workshop you’ll learn how to build a subgraph that indexes NFT blockchain data from the Foundation smart contract. We’ll deploy the API, and learn how to perform queries to retrieve data using various types of data access patterns, implementing filters and sorting.

By the end of the workshop, you should understand how to build and deploy performant APIs to The Graph to index data from any smart contract deployed to Ethereum.

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

Full Stack Documentation
JSNation 2022JSNation 2022
28 min
Full Stack Documentation
Top Content
Interactive web-based tutorials have become a staple of front end frameworks, and it's easy to see why — developers love being able to try out new tools without the hassle of installing packages or cloning repos.But in the age of full stack meta-frameworks like Next, Remix and SvelteKit, these tutorials only go so far. In this talk, we'll look at how we on the Svelte team are using cutting edge web technology to rethink how we teach each other the tools of our trade.
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!
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
The Guild has recently released Envelop - a new, modern GraphQL Server Framework and plugin system. In this talk I’ll share a brief overview of Envelop and why you should probably upgrade your existing GraphQL server to it.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
In this talk, we'll look at some of the modern options for building a full-stack React and GraphQL app with strong conventions and how this can be of enormous benefit to you and your team. We'll focus specifically on RedwoodJS, a full stack React framework that is often called 'Ruby on Rails for React'.