Detalhes do pacote

@pothos/plugin-complexity

hayes29.4kISC4.1.2

A Pothos plugin for defining and limiting complexity of queries

pothos, graphql, schema, plugin

readme (leia-me)

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: {},
});

changelog (log de mudanças)

@giraphql/plugin-complexity

4.1.2

Patch Changes

  • 1622740: update dependencies

4.1.1

Patch Changes

  • cd7f309: Update dependencies

4.1.0

Minor Changes

  • 27af377: replace eslint and prettier with biome

4.0.2

Patch Changes

  • Updated dependencies [777f6de]
    • @pothos/core@4.0.2

4.0.1

Patch Changes

  • 9bd203e: Fix graphql peer dependency version to match documented minumum version
  • Updated dependencies [9bd203e]
    • @pothos/core@4.0.1

4.0.0

Major Changes

Patch Changes

  • c1e6dcb: update readmes
  • Updated dependencies [c1e6dcb]
  • Updated dependencies [29841a8]
    • @pothos/core@4.0.0

4.0.0-next.1

Patch Changes

  • update readmes
  • Updated dependencies
    • @pothos/core@4.0.0-next.1

4.0.0-next.0

Major Changes

Patch Changes

  • Updated dependencies [29841a8]
    • @pothos/core@4.0.0-next.0

3.13.1

Patch Changes

  • 1ecea46: revert accidental pinning of graphql peer dependency

3.13.0

Minor Changes

  • 2f23a1af: fix bug when limits are zero

3.12.1

Patch Changes

  • 4c6bc638: Add provinance to npm releases

3.12.0

Minor Changes

  • bf0385ae: Add new PothosError classes

3.11.7

Patch Changes

  • d4d41796: Update dev dependencies

3.11.6

Patch Changes

  • 6f00194c: Fix an issue with esm import transform

3.11.5

Patch Changes

  • b12f9122: Fix issue with esm build script

3.11.4

Patch Changes

  • 9fa27cf7: Transform dynamic type imports in d.ts files

3.11.3

Patch Changes

  • 3a82d645: Apply esm transform to esm d.ts definitions

3.11.2

Patch Changes

  • 218fc68b: Fix script for copying ems d.ts definitions

3.11.1

Patch Changes

  • 67531f1e: Create separate typescript definitions for esm files

3.11.0

Minor Changes

  • 11929311: Update type definitions to work with module: "nodeNext"

3.10.2

Patch Changes

  • aa18acb7: update dev dependencies

3.10.1

Patch Changes

  • a76616e0: Don't allow negative size in complexity multipliers

3.10.0

Minor Changes

  • d67764b5: Make options objecst on toSchema, queryType, and mutationType optional

3.9.1

Patch Changes

  • 3f5d2a92: Use fieldComplexity from both builder and toSchema options

3.9.0

Minor Changes

  • c82d5719: add builder option for calculating complexity based on field

3.8.0

Minor Changes

  • 76d50bb4: Fix import of cjs graphql file in esm pothos

3.7.0

Minor Changes

  • 85da3312: Add createComplexityRule method for creating a standard graphql validator"
  • 94138077: Add option to disable complexity checks

3.6.1

Patch Changes

  • 4bcc04b6: Allow to configure complexityError via options.

3.6.0

Minor Changes

  • 3a7ff291: Refactor internal imports to remove import cycles

Patch Changes

  • 3a7ff291: Update dev dependencies

3.5.0

Minor Changes

  • f58ad8fa: Add complexityFromQuery util for calculating complexity without running a request
  • f58ad8fa: Add complexityError option for customizing errors thrown when query exceeds complixity limits

3.4.0

Minor Changes

  • ecb2714c: Add types entry to export map in package.json and update dev dependencies

    This should fix compatibility with typescripts new "moduleResolution": "node12"

3.3.0

Minor Changes

  • 241a385f: Add peer dependency on @pothos/core

3.2.0

Minor Changes

  • 6279235f: Update build process to use swc and move type definitions to dts directory

Patch Changes

  • 21a2454e: update dev dependencies

3.1.2

Patch Changes

  • a7d95fca: Fix bug with __typename selected in a Union fragment

3.1.1

Patch Changes

  • 03aecf76: update .npmignore

3.1.0

Minor Changes

  • 4ad5f4ff: Normalize resolveType and isTypeOf behavior to match graphql spec behavior and allow both to be optional

Patch Changes

  • 43ca3031: Update dev dependencies

3.0.1

Patch Changes

  • 2d9b21cd: Use workspace:* for dev dependencies on pothos packages

3.0.0

Major Changes

  • 4caad5e4: Rename GiraphQL to Pothos

2.5.0

Minor Changes

  • 1d62c1ff: Handle __typename in complexity plugin

2.4.0

Minor Changes

  • 9307635a: Migrate build process to use turborepo

2.3.2

Patch Changes

  • 2b08f852: Fix syntax highlighting in docs and update npm README.md files"

2.3.1

Patch Changes

  • c6aa732: graphql@15 type compatibility fix

2.3.0

Minor Changes

  • 48e9fd8: Add missing exports field to package.json

2.2.0

Minor Changes

  • 42d210c: Use type only imports to resolve circular dependencies

2.1.0

Minor Changes

  • 6d6d54e: Add complexity plugin