Skip to content

oslabs-beta/BuQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 

Repository files navigation

BuQL

buql logo


npm GitHub license

Skip to It

The Power of BuQL

Bun is an groundbreaking new runtime. GraphQL is an incredibly efficient query language. Ioredis is an optimized client of Redis, the state-of-the-art caching solution. What could they do when combined?

Welcome to BuQL, the harmonizing of Bun and GraphQL, with ioredis included for the most optimal query response times. Any developer with a Bun-based codebase can now utilize BuQL as an Express middleware to intercept queries, returning the responses from a cache when possible and caching them otherwise. BuQL is able to bring all of this to the table in an easy-to-use npm package equipped with security features to mitigate risk.

Optimized response times via enhanced runtime speeds. Lightweight and flexible. Straightforward in use, elegant in performance. Keep extremities in at all times, it's time to BuQL up!

BuQLing Up

1. Getting started with Bun

Windows is typically not recommended, but here's how to on other OS's:

  • MacOS and WSL
$ curl -fsSL https://bun.sh/install | bash 

(or, to install a specific version)

$ curl -fsSL https://bun.sh/install | bash -s "bun-v1.0.0"
  • Linux

    • The unzip package is required to install Bun. Use sudo apt install unzip to install unzip package. Kernel version 5.6 or higher is strongly recommended, but the minimum is 5.1. Use uname -r to check Kernel version.
    • Once unzip package is installed, see WSL directions

  • Using npm (for the last time!)
$ npm install -g bun 

  • Using Homebrew (macOS and Linux)
$ brew install oven-sh/bun/bun 

  • Using docker
$ docker pull oven/bun
$ docker run --rm --init --ulimit memlock=-1:-1 oven/bun

  • Using Proto
$ proto install bun

2. BuQLing Up

  • Install BuQL
$ bun install @buql/buql
  • Import it into the file you'll be working in, usually referred to as 'index.js'
import buql from '@buql/buql';

3. Installing and Connecting to an ioredis server

(We recommend using ioredis, as that is where we discovered the best performace. However, due to the syntactic similarities between Redis and its client ioredis, either one should work.)
  • Install ioredis
$ bun install ioredis

Once installed, your server should reflect the below:

  • Note: The default port is 6379
const redisClient = redis.createClient({
  host: "localhost",
  port: 6379,
});

4. Utilizing GraphQL

  • Install GraphQL
$ bun install graphql
$ bun install express-graphql
  • Import the http function from GraphQL in the same folder as BuQL and ioredis:
import { graphqlHTTP } from 'express-graphql';

5. Set up GraphQL schemas

If you're using BuQL, it's likely you've done this already. But just in case, here's some example code to give you an idea. These will likely be in their own schema folder that you will need to import into the same one as BuQL:

  • Import relevant pieces of GraphQL:
import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLNonNull,
  GraphQLList,
  GraphQLID,
} from 'graphql';
  • Define Schemas
const UserType = new GraphQLObjectType({
  name: 'User',
  fields: () => ({
    id: { type: GraphQLString },
    username: { type: GraphQLString },
    age: { type: GraphQLInt}
  }),
});

6. Get to Work!

With everything in place, you’re all BuQLed in! Now you can set up your routes with BuQL.

For instance, in our demo’s frontend we wrote:

const buqlResponse = await fetch('http://localhost:8080/buql', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({query: selectedQuery.query}),
});
const responseObj = await buqlResponse.json();
let { source, cacheHits, nonCache, response } = responseObj;

From there, it travels to the backend, which has this code,

app.use('/buql', buql.security, buql.cache, (req, res) => {
  return res.status(200).send(res.locals.response);
});

app.use('/clearCache', buql.clearCache, (req, res) => {
  return res.status(200).send('cache cleared');
});

// Standalone graphql route
app.use(
  '/graphql',
  graphqlHTTP({
    schema,
    graphiql: true,
  })
);

Becoming a BuQLer

From its conception, BuQL was developed to be an open-source product with a never ending journey to perfection! We gladly welcome any and all contributions, whether through iterations, additions, or general feedback! Here are some features we would love to see:

  • Client-side caching
  • The ability to handle nested queries
  • A more agnostic, unopinionated approach, allowing for use beyond just the Express framework.

At the end of the day, we welcome any and all ideas. Get creative!

Further Info

Feel free to dive deeper into BuQL itself...

...or reach out to the team:

Dylan Briar

Jacob Diamond

Julien Kerekes

Joseph McGarry

linked linked linked linked
GitHub GitHub GitHub GitHub

Send something to the whole team here!
gmail

Let us know you liked the project by clicking the star in the top right!

Thanks and BuQL Up!

About

A lightweight, secure caching solution for GraphQL, Redis, and Bun.

Resources

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •