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

@pothos/plugin-complexity

v3.13.0

Published

A Pothos plugin for defining and limiting complexity of queries

Downloads

15,000

Readme

Complexity Plugin

This plugin allows you to define complexity of fields and limit the maximum complexity, depth, and breadth of queries.

Usage

Install

yarn add @pothos/plugin-complexity

Setup

import ComplexityPlugin from '@pothos/plugin-complexity';

const builder = new SchemaBuilder({
  plugins: [ComplexityPlugin],
});

Configure defaults and limits

To limit query complexity you can specify a maximum complexity either in the builder setup, or when building the schema:

const builder = new SchemaBuilder({
  plugins: [ComplexityPlugin],
  defaultComplexity: 1,
  defaultListMultiplier: 10,
  complexity: {
    limit: {
      complexity: 500,
      depth: 10,
      breadth: 50,
    },
    // or
    limit: (ctx) => ({
      complexity: 500,
      depth: 10,
      breadth: 50,
    }),
  },
});
// or
const schema = builder.toSchema({
  complexity: {
    limit: {
      complexity: 500,
      depth: 10,
      breadth: 50,
    },
  },
});

Options

  • fieldComplexity: (optional, (args, ctx, field) => { complexity: number, multiplier: number} | number): default complexity calculation for fields. defaultComplexity and defaultListMultiplier will not be used if this is set.
  • defaultComplexity: (optional number) defines the default complexity for every field in the schema
  • defaultListMultiplier: (optional number) defines a default complexity multiplier for a list fields sub selections
  • limit: Defines limits for queries, passed the context object if limit is a function
    • complexity: defines the maximum complexity allowed for queries
    • depth: defines the maximum depth of selections in a query
    • breadth: defines the maximum total selections in a query
  • complexityError: (optional function) defines the error to throw when the query complexity exceeds the limit. The function is passed the errorKind (depth, breadth, or complexity), the result (with the depth, breadth, complext, and max values), and a GraphQL info object. It should return (or throw) an error, or a an error message as a string

How complexity is calculated

Complexity is calculated before resolving root any root level fields (query, mutation, subscription), and is based purely on the shape of the query before execution begins.

The complexity of a query is the sum of the complexity of each selected field. If a field has sub-selections, the complexity of its sub-selections are multiplied by a fields multiplier, and then added to the fields own complexity. The default multiplier for fields is 1, and 10 for list fields. This multiplier is meant to the n+1 complexity of list fields.

Example

The following query has a complexity of 131 (assuming we are using the default options), a depth of 3, and a breadth of 5:

query {
  posts {
    # complexity = 131 (posts + 10 * (2 + 11))
    author {
      # complexity = 2 (author + 1 * name)
      name # complexity = 1, depth: 3
    }
    comments {
      # complexity = 11 (comments + 10 * comment)
      comment # complexity = 1, depth: 3
    }
  }
}

Defining complexity of a field:

You can set a custom complexity value on any field:

builder.queryFields((t) => ({
  posts: t.field({
    type: [Post],
    complexity: 20,
  }),
}));

The complexity option can also set the multiplier for a field:

builder.queryFields((t) => ({
  posts: t.field({
    type: [Post],
    complexity: { field: 5, multiplier: 20 },
  }),
}));

A fields complexity can also be based on the fields arguments, or the context value:

builder.queryFields((t) => ({
  posts: t.field({
    type: [Post],
    args: {
      limit: t.arg.int(),
    },
    // base multiplier on how many posts are being requested
    complexity: (args, ctx) => ({ field: 5, multiplier: args.limit ?? 5 }),
  }),
}));

Utilities

complexityFromQuery(query, options)

Returns the query complexity for a given GraphQL query.

const complexity = complexityFromQuery(query, {
  schema: schema,
  // Complexity can be calculated based on the context and arguments,
  // so you may need to provide valid values for the context and arguments.
  // Both are optional, and will default to empty objects.
  context: {},
  variables: {},
});