npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

filamentql

v1.0.8

Published

FilamentQL is a lightweight caching library for GraphQL queries that utilizes a parsing algorithm to detect differences between incoming queries and existing data stored within the cache. The library offers tools for both client and server side caching as

Downloads

38

Readme

FilamentQL

FilamentQL is a lightweight caching library for GraphQL queries that utilizes a parsing algorithm to detect differences between incoming queries and existing data stored within the cache. The library offers tools for both client and server side caching as well as tools for offline mode.

An interactive demo and detail information can be found at filamentql.io

Server-Side Caching

On the server-side, FilamentQL provides a GraphQL endpoint for your Express server with user-defined type definitions and resolvers, and creates a caching layer via a local Redis instance. When a client makes a request, FilamentQL checks the Redis cache for any previously stored data that matches the request. Through a parsing algorithm, FilamentQL then takes the incoming query and identifies any dissimilarities, and makes a subsequent query for just those dissimilarities to the database. Whenever possible, FilamentQL merges data coming back from the database with data from Redis cache and sends it back to the client:

Client-Side Caching

On the client-side, FilamentQL behaves similarly. For its local cache implementation, FilamentQL utilizes session storage, a built-in property on the browser's window object, which allows data to persist throughout page refreshes.

Offline Mode

FilamentQL also supports an offline mode. If the user gets disconnected from the server, all mutations made when the internet is down will be stored in a queue. At a set interval, FilamentQL checks if the network is back online. Whenever the user is back online, FilamentQL dequeues and sends each mutation to the server. Subsequently, the data that comes back from server will update the state and re-render the frontend components. What the user experiences and sees on their end is a seamless re-syncing of information when they come back online.

Installation

1. Redis

FilamentQL utilizes Redis for its server-side caching. If Redis is not already installed on your machine:

  • Install on Mac using Homebrew:
    • In the terminal, enter brew install redis
    • When installation completes, start a redis server by entering redis-server
    • By default redis server runs on localhost:6379
    • To check if your redis server is working: send a ping to the redis server by entering the command redis-cli ping, you will get a PONG in response if your redis server is working properly.
  • Install on Linux or non-Homebrew:
    • Download appropriate version of Redis from redis.io/download
    • Follow installation instructions
    • When installation completes, start a redis server by entering redis-server
    • By default redis server runs on localhost:6379
    • To check if your redis server is working: send a ping to the redis server by entering the command redis-cli ping, you will get a PONG in response if your redis server is working properly.

2. FilamentQL

Check out our npm package

npm install filamentql

Instructions

FilamentQL comes with filamentql/client and filamentql/server in order to make all the magic happen.

On client side, filamentql exposes 2 hooks:

  • useFilamentQuery
    • helps query data from GraphQL server
  • useFilamentMutation
    • helps make mutation even though the network is offline

Both abstract away how to fetch queries and mutations and automatically update state when data is returned from server.

Client Example

import React, { useState } from 'react';
import { useFilamentQuery, useFilamentMutation } from 'filamentql/client';

const query = `
  query {
    todos {
      id
      text
      isCompleted
    }
  }
`;

const mutation = (text) => `
  mutation {
    addTodo(input: { text: "${text}" }) {
      id
      text
      isCompleted
    }
  }
`;

const App = () => {
  const [value, setValue] = useState('');
  const { state: todosQuery } = useFilamentQuery(query);
  const [callAddTodoMutation, addTodoResponse] = useFilamentMutation(
    mutation,
    () => {
      // this callback is invoked when data is returned from server
      // this is a good place to update relevant state now with new data
      console.log(addTodoResponse.addTodo);
    }
  );

  const handleSubmit = (event) => {
    event.preventDefault();
    callAddTodoMutation(value);
    setValue('')
  };

  const handleChange = (event) => setValue(event.target.value)

  return (
    <div className="App">
      <form onSubmit={handleSubmit}>
        <input type="text" value={value} onChange={handleChange} />
        <button type="submit">New todo</button>
      </form>
      <div>
        {todosQuery &&
          todosQuery.todos.map((todo) => <Todo key={todo.id} todo={todo} />)}
      </div>
    </div>
  );
};

export default App;

Server Example

FilamentQL achieves the caching ability via Express middleware /filament. This middleware will determine if it needs to talk to /graphql or just returns the data from cache.

Since useFilamentQuery under the hood will send all queries to /filament, the middleware /filament needs to be setup in order to facilitate caching process.

And make sure to mount your GraphQL server at route /graphql.

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const redis = require('redis');

// Redis Setup
const client = redis.createClient();
client
  .on('error', (err) => console.log('Error: ' + err))
  .on('connect', () => console.log('Redis client connected'));

// FilamentQL Setup
const filamentMiddlewareWrapper = require('filamentql/server');
const filamentMiddleware = filamentMiddlewareWrapper(client);

// Express setup
const app = express();
const PORT = 4000;
const schema = require('./schema');

app.use(express.json());
app.use('/filament', filamentMiddleware);
app.use(
  '/graphql',
  graphqlHTTP((req) => ({
    schema,
    graphiql: true,
    context: {
      req,
    },
  }))
);

app.listen(PORT, () => console.log(`GraphQL server is on port: ${PORT}`));

Contributors

FilamentQL is an open-source NPM package created in collaboration with OS Labs and developed by

Notes

  • Currently, FilamentQL v1.0 can only cache and parse queries without arguments, variables, or directives and do not support caching for mutation.