A GraphQL Generator for Mongo and CosmosDB

Related tags

Database velzy
Overview

A GraphQL Function Starter Kit for Cosmos DB

This is a starter kit for rapid development of a GraphQL API using the Mongo driver for Cosmos DB. You create a simple JavaScript file in your function root called db.js and, using that, a GraphQL API is created that you can modify as needed. The project is inspired by [JSON Server](https://github.com/typicode/json-server) and follows in its footsteps - but instead of a REST-y API you get radical GraphQL.

Installation

The first thing to do is to clone this repo:

git clone https://github.com/robconery/velzy

This is an Azure function project and has all the files you’ll need to get your API up and running on Azure quickly using MongoDB locally, Cosmos DB remotely. Before you can get started, however, you need to make sure you have a DATABASE_URL set up locally:

DATABASE_URL="mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb"

If you don’t have Mongo DB installed and you’re on a Mac you can get MongoDB.app, which runs Mongo as a service.

If Mongo is running just pop into the directory and create a db.js file inside of the mongo directory.

The Seed File: db.js

Velzy works by consuming a local db.js file which is for development purposes only. This is what this file looks like:

const faker = require("faker");
exports.rules = {
  authors:{
    fakes: 10
  },
  books: {
    fakes: 10
  }
}
exports.data = {
  authors : [
    {
      name: "{{name.firstName}} {{name.lastName}}",
    }
  ],
  books : [
    {
      title: '{{commerce.productName}}',
      price: faker.datatype.float,
      onHand: 1,
      published: faker.date.future
    }
  ]
}

As of now, there are two main exports: data and rules . For now, the only thing rules does is tell the generator how many documents to seed locally during development. Eventually there will be authentication rules in place. The data export does two things:

  1. Describes your database documents and
  2. Describes the Faker templates for each input.

Faker, if you didn’t know, is a fake data generator that’s very easy to work with. You specify what kind of fake data you want and it takes care of the rest. Here you can see I’m using the library in two ways: directly with a declaration like faker.date.future and using templating which is built into Faker: {{commerce.productName}}. When you start things up the db.js file is pulled in, parsed, and then the database is built from that. Let’s take a look.

The GraphQL Bits

Once you have your ./mongo/db.js file created you’re ready to go:

npm run dev

This will fire up an Apollo GraphQL server:

We can then head over to localhost:4000 and see our API:

That’s it! If you want to change anything, just update your db.js file and the server will automatically restart.

The API

The generated bits are created for you every time the server starts up, which happens every time you change the db.js file. You can see the generated file by opening up schema.js, which is created for you:

The idea here is that you auto-generate as much as possible and, when you’re ready, you turn off the generation bits and build on top of what’s been generated. The easiest way to do this is to create your own schema file and consume it on server start rather than the generated one. To change that, just head to dev.js (which fires up your local dev server) and change this line:

Running in Production

This API uses Azure functions as a simple, easy and extremely cost-effective way to have a public API and you can see the bootup process in ./mongo/index.js. It’s roughly the same as the local development experience, but nothing is generated for obvious reasons. Note: if you renamed your schema file you’ll need to be sure it’s updated in **index.js** as well.

Setting Up Cosmos DB

If you take a look in the ./mongo/scripts directory you’ll see two files: cosmos.sh and function.sh. These are Azure CLI scripts for creating the Azure resources you’re going to need. Here’s the Cosmos DB generator:

#change as you need
RG=velzy
LOCATION=westus
DBNAME=$RG-db
echo "Guessing your external IP address from ipinfo.io"
IP=$(curl -s ipinfo.io/ip)
az group create --name $RG --location $LOCATION
#This might fail if you're not eligible for free tier
#if it does, remove the --enable-free-tier flag
az cosmosdb create -n $DBNAME 
  --enable-free-tier true \
  --ip-range-filter 0.0.0.0,$IP -g $RG \
  --kind MongoDB \
#This command will grab your database keys, which you'll need for your
#Connection string
echo "Pulling your keys from Azure..."
az cosmosdb keys list -n $DBNAME -g $RG --output table

Setting Up Functions

You can deploy your function right from VS Code or you can run the function script:

RG=velzy
LOCATION=westus
DBNAME=$RG-db
COSMOS_KEY="find me by running the cosmos script"
COSMOS_URL="mongodb://$DBNAME:$COSMOS_KEY@$DBNAME.mongo.cosmos.azure.com:10255/?ssl=true&retrywrites=false&maxIdleTimeMS=120000&appName=$DBNAME@" ## Get this from the other script
az group create --name $RG --location $LOCATION
az storage account create \
    --name $RG-store \
    --location $LOCATION \
    --resource-group $RG \
    --sku Standard_LRS

az functionapp create \
    --resource-group $RG \
    --name $RG-api \
    --consumption-plan-location $LOCATION \
    --runtime node \
    --storage-account $RG-store
az functionapp config appsettings set 
    --name $RG-API \
    --resource-group $RG \
    --settings "DATABASE_URL=$COSMOS_URL"

Replace the placeholders as you need.

To Do

In no particular order:

  • Setup simple authentication using Passport, OAuth and JWT
  • Implement authorization based on rules
  • Create a "Deploy to Azure" button
You might also like...

Learn GraphQL PIAIC CNC Class code

GraphQL using React! Steps (for 01 - react-graphql) Generate and copy Access Token from Github Personal Acess Token Create .env file in project folder

Jan 2, 2023

Código desenvolvido na mentoria do Hiring Coders utilizando Express e GraphQL

hiringcoders-graphql Código desenvolvido na mentoria do Hiring Coders utilizando Express e GraphQL Contribuições A ideia do repositório é continuar si

Dec 29, 2022

A lightweight way to cache on graphQL servers

A lightweight way to cache on graphQL servers

cacheflowQL What is cacheflowQL? CacheflowQL is an npm package with complex caching algorithms that provide developers deep insights into their GraphQ

Nov 16, 2022

A template for WebSockets powered Cloudflare Worker project using graphql-ws

🚡 graphql-ws on Cloudflare Workers A template for WebSockets powered Cloudflare Worker project using graphql-ws. The worker serves the following rout

Dec 18, 2022

Unofficial API Documentation for the Axie Infinity's GraphQL Endpoint.

Axie GraphQL Documentation API Documentation for the Axie Infinity GraphQL Endpoint. Customization Guide This site is usings rocketseat. You may refer

Nov 24, 2022

A Crypto App built to pracitse GraphQL Federation

Getting Started To start this project please first create a .env file in the root of your project with the following: REACT_APP_ASTRA_TOKEN={your_astr

Dec 28, 2022

Very easy graphQL example made by Bobby Chao

Very easy graphQL example made by Bobby Chao. The folder has been organized, the module has been split, and it can be directly used as a development scratch. It using graphQL + node.js + express, and MySQL as datasource.

Sep 18, 2022

Graphql & Apollo

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts After fetch the project, please install n

Dec 21, 2021

Ecommerce-backend-nestjs - Ecommerce app with Nestjs + Prisma ORM + GraphQL + SQLite

ECOMMERCE BACKEND NESTJS APP Nestjs + Prisma ORM + GraphQL + SQLite USER Create Account Login Product Create Product Get Products Get Product Search P

Apr 6, 2022
Owner
Rob Conery
I am the author of The Imposter's Handbook, founder of Big Machine, and a Senior Cloud Developer Engineer at Microsoft. I also host and produce a few podcasts.
Rob Conery
An adapter-based ORM for Node.js with support for mysql, mongo, postgres, mssql (SQL Server), and more

Waterline is a next-generation storage and retrieval engine, and the default ORM used in the Sails framework. It provides a uniform API for accessing

Balderdash 5.4k Jan 4, 2023
curl for GraphQL with autocomplete, subscriptions and GraphiQL. Also a dead-simple universal javascript GraphQL client.

graphqurl graphqurl is a curl like CLI for GraphQL. It's features include: CLI for making GraphQL queries. It also provisions queries with autocomplet

Hasura 3.2k Jan 3, 2023
GraphQL Fastify Server is an implementation of GraphQL.

GraphQL Fastify Server Installation Usage Using cache Middlewares Liveness & Readiness Contributing License Installation npm install --save graphql-fa

Rui Silva 33 Dec 19, 2022
Execute one command (or mount one Node.js middleware) and get an instant high-performance GraphQL API for your PostgreSQL database!

PostGraphile Instant lightning-fast GraphQL API backed primarily by your PostgreSQL database. Highly customisable and extensible thanks to incredibly

Graphile 11.7k Jan 4, 2023
Starter template for NestJS 😻 includes GraphQL with Prisma Client, Passport-JWT authentication, Swagger Api and Docker

Instructions Starter template for ?? NestJS and Prisma. Checkout NestJS Prisma Schematics to automatically add Prisma support to your Nest application

notiz.dev 1.6k Jan 4, 2023
around nestjs, with prisma and some graphql lib,write less code,create power api

介绍 这是一个 prisma + nestjs + graphql 的集成示例 对于开发者来说,特别是使用 graphql 的时候,只需要写非常少量的代码即可完成数据的各种操作,同时也支持接口透传。 开发&部署 本地开发 npm run start:dev swagger 地址:http://loc

芋头 26 Nov 24, 2022
A starter template for Nest.js with MongoDB, GraphQL, JWT Auth, and more!

A progressive Node.js framework for building efficient and scalable server-side applications. Description Nest framework TypeScript starter repository

Michael Guay 19 Dec 25, 2022
Conjure SQL from GraphQL queries 🧙🔮✨

Sqlmancer Conjure SQL from your GraphQL queries ?? ?? ✨ ⚠️ This project is currently on hiatus. I am hoping to resume working on Sqlmancer once I have

Daniel Rearden 132 Oct 30, 2022
Application made to show the basic concepts of GraphQL with Apollo Server

graphql-insta-example Application made to show the basic concepts of GraphQL with Apollo Server. Getting Started Run npm install Run npm run dev Go to

Ana Julia Bittencourt 26 Aug 26, 2022
Workshop to illustrate how to use GraphQL

?? Netflix Clone using Astra DB and GraphQL 50 minutes, Intermediate, Start Building A simple ReactJS Netflix homepage clone running on Astra DB that

DataStax Developers 606 Jan 4, 2023