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!
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. Usesudo apt install unzip
to installunzip
package. Kernel version 5.6 or higher is strongly recommended, but the minimum is 5.1. Useuname -r
to check Kernel version. - Once
unzip
package is installed, see WSL directions
- The
- 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
- 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';
(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
-
Start redis server
- For starting the redis server, check out the official documentation and follow the directions for your OS!
Once installed, your server should reflect the below:
- Note: The default port is
6379
const redisClient = redis.createClient({
host: "localhost",
port: 6379,
});
- 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';
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}
}),
});
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,
})
);
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!
Feel free to dive deeper into BuQL itself......or reach out to the team: