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

express-graphql-persisted-queries

v0.1.2

Published

A Middleware for Persisted Queries with express-graphql

Downloads

33

Readme

A Middleware for Persisted Queries with express-graphql

Code Coverage Status CI Build Status npm version

express-graphql-persisted-queries is an HTTP server middleware for persisted queries designed to work with express-graphql.

express-graphql-persisted-queries gives you a lot of flexibility by allowing you to specify a custom query ID key, a custom way to map a query ID to an actual query, and whether you want to allow only persisted queries for enhanced security.

express-graphql-persisted-queries also allows you to specify the persisted query ID both in the search params of a GET request and in the body of a POST request. This means that you can send HTTP GET requests for your GraphQL queries and laverage HTTP caching. This also means that you can preload queries with <link rel="preload">, starting to load data in parallel with code on the first page load.

Just like express-graphql, this middleware works with any HTTP web framework that supports connect styled middleware, including Connect, Express, and Restify.

Installation

Using yarn:

yarn add express-graphql-persisted-queries

Using npm:

npm install express-graphql-persisted-queries

Usage

express-graphql-persisted-queries exports its middleware as the persistedQueries named export. So, you should import it as follows:

// Using ES modules
import { persistedQueries } from 'express-graphql-persisted-queries';

// Using CommonJS
const { persistedQueries } = require('express-graphql-persisted-queries');

Here's an example of the most basic usage, which assumes that we are sending the query ID under the queryId key in the search params or request body, and that we store the mapping from query ID to query in a JSON file.

import queryMap from './queryMap.json';

app.use('/graphql', persistedQueries({ queryMap }), graphqlHTTP({ schema }));

Notice that the persistedQueries middleware should be used before graphqlHTTP.

Here's a more advanced example in which we specify a custom query ID key, use the database to map the query ID to a query, and allow only persisted queries in production:

app.use(
  '/graphql',
  persistedQueries({
    queryIdKey: 'id',
    queryMap: (queryId) => getQueryTextFromDatabase(queryId),
    strict: process.env.NODE_ENV === 'production',
  }),
  graphqlHTTP({ schema }),
);

API

persistedQueries(options: Options): Middleware

Parameters

  • options: Options are the middleware options:

    • queryIdKey?: string (default: 'queryId') is the key in the search params or request body that specifies the ID of the persisted query.
    • queryMap: QueryMap is either an object mapping query IDs to query text or a function that receives the query ID as input and returns the query text, null, or a promise that resolves with query text or null. The QueryMap type is defined as follows:
      type QueryMap = Record<string, Maybe<string>> | QueryMapFn;
      type QueryMapFn = (queryId: string) => PromiseOrValue<Maybe<string>>;
    • strict?: boolean (default: false) specifies whether only persisted queries are allowed. When strict is true, any request that contains the query text or that does not contain a valid query ID is considered invalid and results in a 400 Bad Request error response.

Return value

  • Middleware an HTTP server middleware.

Description

persistedQueries is used to create a middleware that adds support for persisted queries to your GraphQL HTTP server. This middleware should be specified before graphqlHTTP from express-graphql.

Example

app.use(
  '/graphql',
  persistedQueries({
    queryMap,
    strict: process.env.NODE_ENV === 'production',
  }),
  graphqlHTTP({ schema }),
);

Contributing

Your contributions are very welcome! To get started, read our contributing guidelines.

Also check out existing issues labeled as help wanted or good first issue.

License

MIT