A template repository for Deno, GraphQL, and Svelte projects

Overview

The SWEET Stack

This is a template repository for Deno, GraphQL, and Svelte projects


Table of Contents


Introduction

Svelte is an innovative approach to frontend software development, the component model, and reactivity features creates simplistic approach to some hard problems. Svelte runs on the front end and is known as a Single Page Application.

Deno is a JavaScript Server side platform similar to NodeJs but focused on Web Standards, supports linting, formatting, testing, and typescript out of the box.

I love both of these technologies, but I also enjoy simplicity, this stack represents simplicity, I know there are trade-offs with SPA architecture, but one advantage of Deno, is Deno Deploy and the ability to push your app to the Edge. This offers localized access to assets and server-based functionality, if you wish to serve a static web page, just put it in the public folder, if you wish to serve dynamic pages then use Svelte and compile to JS. I think the target for this stack is small applications, if your needs are very complex, you may want to consider SvelteKit or something similar. If your needs are static website, checkout Astro or Gatsby.

How to use this template

In this readme, I will go through how to manually build this stack, but if you rather jump to just building a project you can fork this repo and go, or you can use degit to pull this repo down and start building as well.

npx degit hyper63/sweet-stack myproject

You will need to have both NodeJS and Deno installed.

Prerequisites

NOTE: we will be using Makefile to run our scripts, your OS may not support this feature without installing the make command-line application.

Deno and GraphQL

Setting up Deno and GraphQL is straight forward, now like everything, there are many opinions, we will be using a Makefile to manage our build scripts. We will be placing our code in the src directory and we will be using import-maps feature of Deno to manage our dependencies for Deno.

Setup Project

touch import_map.json Makefile
mkdir src

Makefile

dev:
	@deno run --allow-net --allow-read --allow-env --import-map=./import_map.json src/server.js

test:
	@deno fmt src && deno lint src && deno test src

import_map.json

{
  "imports": {
    "std/": "https://deno.land/std/",
    "gql": "https://deno.land/x/[email protected]/mod.ts",
    "graphql_tools": "https://deno.land/x/[email protected]/mod.ts",
    "graphql_tag": "https://deno.land/x/[email protected]/mod.ts",
    "dotenv": "https://deno.land/x/[email protected]/load.ts"
  }
}

Create a server.js and api.js files in the src folder

touch src/server.js src/api.js

server.js

{ const routes = { "/": serveStatic("./app/public/index.html", "text/html"), "/favicon.png": serveStatic("./app/public/favicon.png", "image/png"), "/build/bundle.css": serveStatic( "./app/public/build/bundle.css", "text/css", ), "/build/bundle.js": serveStatic( "./app/public/build/bundle.js", "text/javascript", ), "/graphql": graphql, }; // get path from req object const { pathname } = new URL(req.url); // log request console.log(`${req.method} ${pathname}`); // simple match to handle the request return routes[pathname] ? routes[pathname](req) : routes["/"](req); }); /** * @param {string} file * @param {string} type * @returns {Response} */ function serveStatic(file, type) { return async () => new Response( await Deno.readTextFile(file), { headers: { "content-type": type }, }, ); }">
import { serve } from "std/http/server.ts";
import { graphql, org } from "./api.js";

/**
 * @param {Request} reg
 * @returns {Response}
 */
serve((req) => {
  const routes = {
    "/": serveStatic("./app/public/index.html", "text/html"),
    "/favicon.png": serveStatic("./app/public/favicon.png", "image/png"),
    "/build/bundle.css": serveStatic(
      "./app/public/build/bundle.css",
      "text/css",
    ),
    "/build/bundle.js": serveStatic(
      "./app/public/build/bundle.js",
      "text/javascript",
    ),
    "/graphql": graphql,
  };

  // get path from req object
  const { pathname } = new URL(req.url);

  // log request
  console.log(`${req.method} ${pathname}`);

  // simple match to handle the request
  return routes[pathname] ? routes[pathname](req) : routes["/"](req);
});

/**
 * @param {string} file
 * @param {string} type
 * @returns {Response}
 */
function serveStatic(file, type) {
  return async () =>
    new Response(
      await Deno.readTextFile(file),
      {
        headers: { "content-type": type },
      },
    );
}

api.js

Promise.resolve("Hello World!"), }, }; /** * @param {Request} req * @returns {Response} */ export const graphql = async (req) => await GraphQLHTTP({ schema: makeExecutableSchema({ resolvers, typeDefs }), graphiql: true, })(req);">
import "dotenv";
import { GraphQLHTTP } from "gql";
import { makeExecutableSchema } from "graphql_tools";
import { gql } from "graphql_tag";

const typeDefs = gql`
  type Query {
    hello : String
  }
`;

const resolvers = {
  Query: {
    hello: () => Promise.resolve("Hello World!"),
  },
};

/**
 * @param {Request} req
 * @returns {Response}
 */
export const graphql = async (req) =>
  await GraphQLHTTP({
    schema: makeExecutableSchema({ resolvers, typeDefs }),
    graphiql: true,
  })(req);

Run

make

Navigate to http://localhost:8000/graphql

and run the following query

query {
  hello
}

Test

make test

SvelteJS

Setting up Svelte is the exact same process as https://svelte.dev

npx degit sveltejs/template app
cd app
yarn

Now we do need to make some adjustments:

edit the rollup.conf.js file

We want to comment out the !production && serve() line, and the !production && livereload('public') line.

// // In dev mode, call `npm run start` once
// // the bundle has been generated
// !production && serve(),

// // Watch the `public` directory and refresh the
// // browser on changes when not in production
// !production && livereload('public'),

We can run Svelte separately in another terminal by running:

yarn dev

But lets create one startup script, using foreman

npm i -g foreman

Create a Procfile in the project root directory

server: make
app: cd app && yarn && yarn dev

Now, we can run both our server and app with one command:

nf start

Bonus

  • Comming Soon

Contributing

We welcome suggestions and improvements to this stack, especially if there are better approaches to running parallel tasks or any other items, like dependency management, etc.

License

MIT

You might also like...

A MERN boilerplate repository with RBAC feature, following all production best practices.

A MERN boilerplate repository with RBAC feature, following all production best practices.

Welcome to the RBAC MERN Boilerplate project A MERN boilerplate repository with RBAC feature, following all production best practices. In this reposit

Dec 27, 2022

A highly opinionated and complete starter for Next.js projects ready to production

A highly opinionated and complete starter for Next.js projects ready to production

The aim for this starter is to give you a starting point with everything ready to work and launch to production. Web Vitals with 100% by default. Folder structure ready. Tooling ready. SEO ready. SSR ready.

Nov 27, 2022

Hi there! This is a react native starter which used to build a awesome Event Booking App based on the Figma design. You can download or clone it to speed up your projects.

Hi there! This is a react native starter which used to build a awesome Event Booking App based on the Figma design. You can download or clone it to speed up your projects.

mcrn-event-booking-app-starter Hi there! This is a react native starter which used to build a awesome Event Booking App based on the Figma design. You

Dec 19, 2022

Very minimalistic boilerplate to start most Typescript Express projects. Includes Mongodb connection via Mongoose with a sample post model.

Very minimalistic boilerplate to start most Typescript Express projects. Includes Mongodb connection via Mongoose with a sample post model.

Minimalist express-typescript-boilerplate Available Scripts - `tsc' - builds the tsc project to the dist folder - `build` - cleans the previous build

Dec 13, 2022

A professional front-end template for building fast, robust, and adaptable web apps or sites.

HTML5 Boilerplate HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites. This project is t

Dec 28, 2022

This is a template project demonstrating how the MERN stack(Mongo, Express, React, Node) can be used, here we have the back end implementation and there is the React implementation as the front end

This is a template project demonstrating how the MERN stack(Mongo, Express, React, Node) can be used, here we have the back end implementation and there is the React implementation as the front end

Versão em português MERN stack This is a template project demonstrating how the MERN stack(Mongo, Express, React, Node) can be used, here we have the

Jan 22, 2022

A simple Vue3, Nuxt3 and Tailwind3 Starter Template

Nuxt 3 + Tailwind CSS 3 Starter This is a minimal starter template for Nuxt 3 projects with Tailwind CSS 3. It includes a simple template pages/index.

Feb 11, 2022

A Node template that follows the Clean Architecture principles and encourages established practices.

Clean Architecture Template A Node template that follows the Clean Architecture principles and encourages established practices. Features TypeScript f

Dec 25, 2022

DEPRECATED - A front-end template that helps you build fast, modern mobile web apps.

Deprecation Note Mobile Boilerplate is no longer maintained. Please use HTML5 Boilerplate as a decent starting point for your project. Mobile Boilerpl

Dec 14, 2022
Owner
hyper
hyper
Nestjs boilerplate template (mongodb, typeorm, graphql - code first, i18n, global exception handling etc.)

Description City-Library-Demo project for boilerplate. (node version: 16.14.0, npm version: 8.5.2) First Steps Create .env file in root directory. (Th

Mert Kaygusuz 7 Nov 21, 2022
Next Boilerplate was created to be a template for starting NextJS projects with pre-configured settings like Linters, Test Setup, Storybook and Commit Hooks.

Next Boilerplate was created to be a template for starting NextJS projects with pre-configured settings like Linters, Test Setup, Storybook and Commit Hooks.

Claudio Orlandi 4 Feb 22, 2022
A generic template for starting Web3 Projects

Redwood WARNING: RedwoodJS software has not reached a stable version 1.0 and should not be considered suitable for production use. In the "make it wor

Dennison Bertram 18 Jun 28, 2022
👩🏻‍💻 Developer Ready: A comprehensive template. Works out of the box for most Node.js projects.

node-typescript-boilerplate ????‍?? Developer Ready: A comprehensive template. Works out of the box for most Node.js projects. ???? Instant Value: All

IAD S.r.l. 6 Dec 15, 2022
The next small thing in web development, powered by Svelte

sapper The next small thing in web development, powered by Svelte. What is Sapper? Sapper is a framework for building high-performance universal web a

Svelte 7.1k Jan 2, 2023
Node Express Template (NET.ts) - a small template project which help you to speed up the process of building RESTful API

Node Express Template (NET.ts) - a small template project which help you to speed up the process of building RESTful API

Przemek Nowicki 26 Jan 4, 2023
Stepzen-sveltekit - An Example Blog with SvelteKit, the DEV API, and a StepZen GraphQL Backend.

Building a Serverless Blog with StepZen, SvelteKit, and the DEV API SvelteKit is a serverless first Svelte metaframework for building web applications

Anthony Campolo 0 Jan 17, 2022
React Starter Kit — isomorphic web app boilerplate (Node.js, Express, GraphQL, React.js, Babel, PostCSS, Webpack, Browsersync)

React Starter Kit — "isomorphic" web app boilerplate React Starter Kit is an opinionated boilerplate for web development built on top of Node.js, Expr

Kriasoft 21.7k Jan 1, 2023
A starter for nextjs that pulls in tailwindui, graphql

NextJS TypeScript TailwindUI GraphQL Boilerplate Demonstrates a NextJS application with TypeScript and TailwindUI with a ASP.Net Core GraphQL based AP

RDA Corporation 5 Nov 17, 2022
Express Graphql Clean Architecture

Folder pattern for express graphql starterkit clean architecture, easy to scalable and easy to maintenance, explore documentation for more about this starterkit https://typegraphql.com

Restu Wahyu Saputra 10 Aug 14, 2022