@baeta/extension-complexity 2.0.0-next.0 → 2.0.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,104 @@
1
+ # @baeta/extension-complexity
2
+
3
+ ## 2.0.0-next.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Fix broken types
8
+
9
+ - Updated dependencies []:
10
+ - @baeta/core@2.0.0-next.2
11
+
12
+ ## 2.0.0-next.1
13
+
14
+ ### Patch Changes
15
+
16
+ - Fix release version
17
+
18
+ - Updated dependencies []:
19
+ - @baeta/core@2.0.0-next.1
20
+
21
+ ## 2.0.0-next.0
22
+
23
+ ### Major Changes
24
+
25
+ - [#214](https://github.com/andreisergiu98/baeta/pull/214) [`31d1a50`](https://github.com/andreisergiu98/baeta/commit/31d1a509f96535b43ae85d19c770eb1a5f09dc94) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - Baeta v2 – major refactor
26
+ - **Side-effect-free type generation & resolver definitions.**
27
+ The types generator and resolver definitions were reworked to be side-effect free, improving type safety.
28
+ - **Stricter type safety.**
29
+ You must now **explicitly define resolvers for every field** during development—breakages that used to surface at runtime are now caught at compile time.
30
+ - **Removed `@baeta/compiler`.**
31
+ Since modern runtimes can execute TypeScript natively, the separate compiler package is no longer needed. Use your runtime’s native TS support or your existing build setup.
32
+ - **Subscriptions update.**
33
+ `@baeta/subscriptions-pubsub` now targets **`graphql-subscriptions` v3**.
34
+
35
+ ### Patch Changes
36
+
37
+ - Updated dependencies [[`31d1a50`](https://github.com/andreisergiu98/baeta/commit/31d1a509f96535b43ae85d19c770eb1a5f09dc94)]:
38
+ - @baeta/core@2.0.0-next.0
39
+
40
+ ## 1.0.11
41
+
42
+ ### Patch Changes
43
+
44
+ - Updated dependencies [[`7f3d5ff`](https://github.com/andreisergiu98/baeta/commit/7f3d5ff7cd0871e2f017c86dd29ac7bc02647154)]:
45
+ - @baeta/core@1.0.11
46
+
47
+ ## 1.0.9
48
+
49
+ ### Patch Changes
50
+
51
+ - [`583014f`](https://github.com/andreisergiu98/baeta/commit/583014f0bac810b25d9a8226bda2df4c9039f5e3) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - Update dependencies
52
+
53
+ - Updated dependencies [[`583014f`](https://github.com/andreisergiu98/baeta/commit/583014f0bac810b25d9a8226bda2df4c9039f5e3)]:
54
+ - @baeta/core@1.0.9
55
+
56
+ ## 0.0.5
57
+
58
+ ### Patch Changes
59
+
60
+ - [#189](https://github.com/andreisergiu98/baeta/pull/189) [`d500378`](https://github.com/andreisergiu98/baeta/commit/d500378198e0a9c48298c4242913bca8ad348228) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - add jsdocs
61
+
62
+ - Updated dependencies [[`d500378`](https://github.com/andreisergiu98/baeta/commit/d500378198e0a9c48298c4242913bca8ad348228), [`1334c2a`](https://github.com/andreisergiu98/baeta/commit/1334c2a866676c88f0f3d380b22133d81c4e98bc)]:
63
+ - @baeta/core@1.0.8
64
+
65
+ ## 0.0.4
66
+
67
+ ### Patch Changes
68
+
69
+ - [`b59db50`](https://github.com/andreisergiu98/baeta/commit/b59db501a83275ab2d964933080e688a3a5d8820) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - add readme
70
+
71
+ - Updated dependencies [[`b59db50`](https://github.com/andreisergiu98/baeta/commit/b59db501a83275ab2d964933080e688a3a5d8820)]:
72
+ - @baeta/core@0.1.5
73
+ - @baeta/errors@0.1.4
74
+
75
+ ## 0.0.3
76
+
77
+ ### Patch Changes
78
+
79
+ - [#180](https://github.com/andreisergiu98/baeta/pull/180) [`483c709`](https://github.com/andreisergiu98/baeta/commit/483c70932f815fd114732c00b74f9488d7924c72) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - Raise minimum required NodeJS version to 22.12.0. Drop CommonJS builds in favor of the require_esm feature from NodeJS 22.12.0 onwards.
80
+
81
+ - [`de6e89c`](https://github.com/andreisergiu98/baeta/commit/de6e89c1b592e280967c73a4137d24ee56ef1857) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - raise es target to 2024
82
+
83
+ - Updated dependencies [[`483c709`](https://github.com/andreisergiu98/baeta/commit/483c70932f815fd114732c00b74f9488d7924c72), [`de6e89c`](https://github.com/andreisergiu98/baeta/commit/de6e89c1b592e280967c73a4137d24ee56ef1857)]:
84
+ - @baeta/core@0.1.4
85
+ - @baeta/errors@0.1.3
86
+
87
+ ## 0.0.2
88
+
89
+ ### Patch Changes
90
+
91
+ - [`c8dea0c`](https://github.com/andreisergiu98/baeta/commit/c8dea0c6a7824039cf4de13a8d2c0b1682ea3d02) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - maintain order of prepended middlewares
92
+
93
+ - Updated dependencies [[`c8dea0c`](https://github.com/andreisergiu98/baeta/commit/c8dea0c6a7824039cf4de13a8d2c0b1682ea3d02)]:
94
+ - @baeta/core@0.1.3
95
+
96
+ ## 0.0.1
97
+
98
+ ### Patch Changes
99
+
100
+ - [#176](https://github.com/andreisergiu98/baeta/pull/176) [`d77cd8a`](https://github.com/andreisergiu98/baeta/commit/d77cd8a1810fdf72cfbbb08d05c207bbc893c822) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - feat: add complexity extension
101
+
102
+ - Updated dependencies [[`59bbb9c`](https://github.com/andreisergiu98/baeta/commit/59bbb9c4baaf716f27dc251fe7aeb0231e6c5321), [`d77cd8a`](https://github.com/andreisergiu98/baeta/commit/d77cd8a1810fdf72cfbbb08d05c207bbc893c822), [`cf9f094`](https://github.com/andreisergiu98/baeta/commit/cf9f09468f84d99b069eb0f55e1fc207e2a41dd8)]:
103
+ - @baeta/core@0.1.2
104
+ - @baeta/errors@0.1.2
@@ -0,0 +1,200 @@
1
+ import { FieldBuilder, TypeBuilder, SubscriptionBuilder, Extension, ModuleCompiler } from '@baeta/core/sdk';
2
+ import { GraphQLError, GraphQLErrorOptions } from 'graphql';
3
+
4
+ /**
5
+ * Configuration for field complexity calculation.
6
+ */
7
+ type FieldSettings = {
8
+ complexity?: number;
9
+ multiplier?: number;
10
+ };
11
+ /**
12
+ * Arguments passed to field settings functions.
13
+ */
14
+ type GetFieldSettingsArgs<Context, Args> = {
15
+ /** Arguments passed to the GraphQL field */
16
+ args: Args;
17
+ /** Request context */
18
+ ctx: Context;
19
+ };
20
+ /**
21
+ * Function to determine complexity settings for a field.
22
+ * Returns either field settings or false to disable complexity calculation.
23
+ *
24
+ * @param params - Object containing field arguments and context
25
+ * @returns Field settings object or false
26
+ */
27
+ type GetFieldSettings<Context, Args> = (params: GetFieldSettingsArgs<Context, Args>) => FieldSettings | false;
28
+
29
+ /** biome-ignore-all lint/correctness/noUnusedVariables: arguments used for inference */
30
+
31
+ declare global {
32
+ export namespace BaetaExtensions {
33
+ interface FieldExtensions<Result, Source, Context, Args, Info, Builder extends FieldBuilder<Result, Source, Context, Args, Info>> {
34
+ /**
35
+ * Configures complexity calculation for a type field.
36
+ *
37
+ * @param fn - Function to determine complexity settings
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * Query.users.$complexity((_, args) => ({
42
+ * complexity: 1,
43
+ * multiplier: args.limit || 10
44
+ * })).resolve(...);
45
+ *
46
+ * // Disable complexity calculation
47
+ * Query.simple.$complexity(() => false);
48
+ * ```
49
+ */
50
+ $complexity: (fn: GetFieldSettings<Context, Args>) => ReturnType<Builder['toMethods']>;
51
+ }
52
+ interface TypeExtensions<Source, Context, Info, Builder extends TypeBuilder<Source, Context, Info>> {
53
+ /**
54
+ * Configures complexity calculation for a type field.
55
+ *
56
+ * @param fn - Function to determine complexity settings
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * Query.users.$complexity((_, args) => ({
61
+ * complexity: 1,
62
+ * multiplier: args.limit || 10
63
+ * })).resolve(...);
64
+ *
65
+ * // Disable complexity calculation
66
+ * Query.simple.$complexity(() => false);
67
+ * ```
68
+ */
69
+ $complexity: (fn: GetFieldSettings<Context, unknown>) => ReturnType<Builder['toMethods']>;
70
+ }
71
+ interface SubscriptionExtensions<Result, Source, Context, Args, Info, Builder extends SubscriptionBuilder<Result, Source, Context, Args, Info>> {
72
+ /**
73
+ * Configures complexity calculation for a type field.
74
+ *
75
+ * @param fn - Function to determine complexity settings
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * Query.users.$complexity((_, args) => ({
80
+ * complexity: 1,
81
+ * multiplier: args.limit || 10
82
+ * })).resolve(...);
83
+ *
84
+ * // Disable complexity calculation
85
+ * Query.simple.$complexity(() => false);
86
+ * ```
87
+ */
88
+ $complexity: (fn: GetFieldSettings<Context, Args>) => ReturnType<Builder['toMethods']>;
89
+ }
90
+ }
91
+ }
92
+
93
+ /** Complexity error code */
94
+ declare const ComplexityErrorCode = "COMPLEXITY_ERROR";
95
+ /**
96
+ * Thrown when a query exceeds the complexity limits.
97
+ */
98
+ declare class ComplexityError extends GraphQLError {
99
+ constructor(message?: string, options?: GraphQLErrorOptions);
100
+ }
101
+ /**
102
+ * Types of complexity validation errors that can occur during query analysis.
103
+ */
104
+ declare const ComplexityErrorKind: {
105
+ /** Query exceeds maximum allowed depth */
106
+ Depth: string;
107
+ /** Query exceeds maximum allowed breadth (fields per level) */
108
+ Breadth: string;
109
+ /** Query exceeds total complexity score limit */
110
+ Complexity: string;
111
+ };
112
+ type ComplexityErrorKind = (typeof ComplexityErrorKind)[keyof typeof ComplexityErrorKind];
113
+ /**
114
+ * Function type for creating custom complexity error messages.
115
+ *
116
+ * @param kind - The type of complexity limit that was exceeded
117
+ * @param limits - The maximum allowed value
118
+ * @param results - The actual value that exceeded the limit
119
+ * @returns A GraphQL error with a custom message
120
+ */
121
+ type GetComplexityError = (kind: ComplexityErrorKind, limits: number, results: number) => GraphQLError;
122
+
123
+ /**
124
+ * Configuration for query complexity limits.
125
+ */
126
+ interface ComplexityLimit {
127
+ /** Maximum allowed query depth */
128
+ depth?: number;
129
+ /** Maximum allowed fields per level */
130
+ breadth?: number;
131
+ /** Maximum allowed total complexity score */
132
+ complexity?: number;
133
+ }
134
+ /**
135
+ * Function to determine complexity limits, can be static or context-based.
136
+ */
137
+ type GetComplexityLimit<Context> = ComplexityLimit | ((ctx: Context) => ComplexityLimit | PromiseLike<ComplexityLimit>);
138
+
139
+ /**
140
+ * Configuration options for the complexity extension.
141
+ */
142
+ interface ComplexityExtensionOptions<Context> {
143
+ /** Static limits or function to determine limits based on context */
144
+ limit?: GetComplexityLimit<Context>;
145
+ /**
146
+ * Base complexity score for fields
147
+ * @defaultValue 1
148
+ */
149
+ defaultComplexity?: number;
150
+ /**
151
+ * Multiplier applied to list fields
152
+ * @defaultValue 10
153
+ */
154
+ defaultListMultiplier?: number;
155
+ /** Custom error message generator */
156
+ complexityError?: GetComplexityError;
157
+ }
158
+
159
+ interface ComplexityState {
160
+ fieldSettings: GetFieldSettings<unknown, unknown>;
161
+ }
162
+ declare global {
163
+ export namespace BaetaExtensions {
164
+ interface Extensions {
165
+ complexityExtension: ComplexityExtension<unknown>;
166
+ }
167
+ }
168
+ }
169
+ declare class ComplexityExtension<Ctx> extends Extension<ComplexityState> {
170
+ readonly stateKey: symbol;
171
+ private readonly options;
172
+ constructor(options?: ComplexityExtensionOptions<Ctx>);
173
+ getTypeExtensions: <Source, Context, Info>(builder: TypeBuilder<Source, Context, Info>) => BaetaExtensions.TypeExtensions<Source, Context, Info, TypeBuilder<Source, Context, Info>>;
174
+ getFieldExtensions: <Result, Source, Context, Args, Info>(builder: FieldBuilder<Result, Source, Context, Args, Info>) => BaetaExtensions.FieldExtensions<Result, Source, Context, Args, Info, FieldBuilder<Result, Source, Context, Args, Info>>;
175
+ getSubscriptionExtensions: <Result, Source, Context, Args, Info>(builder: SubscriptionBuilder<Result, Source, Context, Args, Info>) => BaetaExtensions.SubscriptionExtensions<Result, Source, Context, Args, Info, SubscriptionBuilder<Result, Source, Context, Args, Info>>;
176
+ mutate(compilers: ModuleCompiler[]): void;
177
+ }
178
+
179
+ /**
180
+ * Creates a complexity analysis extension for GraphQL queries.
181
+ *
182
+ * @param options - Configuration options for complexity analysis
183
+ * @returns Extension factory function
184
+ *
185
+ * @example
186
+ * ```typescript
187
+ * const complexity = complexityExtension<Context>({
188
+ * defaultComplexity: 1,
189
+ * defaultListMultiplier: 10,
190
+ * limit: {
191
+ * depth: 5,
192
+ * breadth: 10,
193
+ * complexity: 100
194
+ * }
195
+ * });
196
+ * ```
197
+ */
198
+ declare function complexityExtension<Ctx>(options?: ComplexityExtensionOptions<Ctx>): ComplexityExtension<Ctx>;
199
+
200
+ export { ComplexityError, ComplexityErrorCode, ComplexityErrorKind, type ComplexityExtensionOptions, type ComplexityLimit, type FieldSettings, type GetComplexityError, type GetComplexityLimit, type GetFieldSettings, type GetFieldSettingsArgs, complexityExtension };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baeta/extension-complexity",
3
- "version": "2.0.0-next.0",
3
+ "version": "2.0.0-next.2",
4
4
  "keywords": [
5
5
  "baeta",
6
6
  "graphql",
@@ -27,8 +27,8 @@
27
27
  "type": "module",
28
28
  "exports": {
29
29
  ".": {
30
- "types": "./index.ts",
31
- "default": "./index.ts"
30
+ "types": "./dist/index.d.ts",
31
+ "default": "./dist/index.js"
32
32
  }
33
33
  },
34
34
  "types": "dist/index.d.ts",
@@ -44,15 +44,15 @@
44
44
  "types": "tsc --noEmit"
45
45
  },
46
46
  "devDependencies": {
47
- "@baeta/builder": "workspace:^",
48
- "@baeta/core": "workspace:^",
49
- "@baeta/testing": "workspace:^",
50
- "@baeta/tsconfig": "workspace:^",
47
+ "@baeta/builder": "^0.0.0",
48
+ "@baeta/core": "^2.0.0-next.2",
49
+ "@baeta/testing": "^0.0.0",
50
+ "@baeta/tsconfig": "^0.0.0",
51
51
  "graphql": "^16.11.0",
52
52
  "typescript": "^5.9.3"
53
53
  },
54
54
  "peerDependencies": {
55
- "@baeta/core": "workspace:^",
55
+ "@baeta/core": "^2.0.0-next.2",
56
56
  "graphql": "^16.6.0"
57
57
  },
58
58
  "engines": {
@@ -89,4 +89,4 @@
89
89
  "alphabetical-ignoring-documents"
90
90
  ]
91
91
  }
92
- }
92
+ }