GiraphQL

Searchâ€¦

Complexity

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

Usage

Install

1

yarn add @giraphql/plugin-complexity

Copied!

Setup

1

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

2

â€‹

3

const builder = new SchemaBuilder({

4

plugins: [ComplexityPlugin],

5

});

Copied!

Configure defaults and limits

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

1

const builder = new SchemaBuilder({

2

plugins: [ComplexityPlugin],

3

defaultComplexity: 1,

4

defaultListMultiplier: 10,

5

complexity: {

6

limit: {

7

complexity: 500,

8

depth: 10,

9

breadth: 50,

10

},

11

// or

12

limit: (ctx) => ({

13

complexity: 500,

14

depth: 10,

15

breadth: 50,

16

}),

17

},

18

});

19

// or

20

const schema = builder.toSchema({

21

complexity: {

22

limit: {

23

complexity: 500,

24

depth: 10,

25

breadth: 50,

26

},

27

},

28

});

Copied!

Options

- 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

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`

:1

query {

2

posts {

3

# complexity = 131 (posts + 10 * (2 + 11))

4

author {

5

# complexity = 2 (author + 1 * name)

6

name # complexity = 1, depth: 3

7

}

8

comments {

9

# complexity = 11 (comments + 10 * comment)

10

comment # complexity = 1, depth: 3

11

}

12

}

13

}

Copied!

Defining complexity of a field:

You can set a custom complexity value on any field:

1

builder.queryFields((t) => ({

2

posts: t.field({

3

type: [Post],

4

complexity: 20,

5

}),

6

}));

Copied!

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

1

builder.queryFields((t) => ({

2

posts: t.field({

3

type: [Post],

4

complexity: { field: 5, multiplier: 20 },

5

}),

6

}));

Copied!

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

1

builder.queryFields((t) => ({

2

posts: t.field({

3

type: [Post],

4

args: {

5

limit: t.arg.int(),

6

},

7

// base multiplier on how many posts are being requested

8

complexity: (args, ctx) => ({ field: 5, multiplier: args.limit ?? 5 }),

9

}),

10

}));

Copied!

Last modified 1mo ago

Copy link

Edit on GitHub