@baeta/extension-complexity 2.0.0-next.1 → 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 CHANGED
@@ -1,5 +1,14 @@
1
1
  # @baeta/extension-complexity
2
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
+
3
12
  ## 2.0.0-next.1
4
13
 
5
14
  ### Patch Changes
@@ -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.1",
3
+ "version": "2.0.0-next.2",
4
4
  "keywords": [
5
5
  "baeta",
6
6
  "graphql",
@@ -45,14 +45,14 @@
45
45
  },
46
46
  "devDependencies": {
47
47
  "@baeta/builder": "^0.0.0",
48
- "@baeta/core": "^2.0.0-next.1",
48
+ "@baeta/core": "^2.0.0-next.2",
49
49
  "@baeta/testing": "^0.0.0",
50
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": "^2.0.0-next.1",
55
+ "@baeta/core": "^2.0.0-next.2",
56
56
  "graphql": "^16.6.0"
57
57
  },
58
58
  "engines": {