@enonic-types/lib-graphql 3.0.0

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.
Files changed (3) hide show
  1. package/README.md +57 -0
  2. package/index.d.ts +397 -0
  3. package/package.json +29 -0
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # @enonic-types/lib-graphql
2
+
3
+ TypeScript type definitions for the [Enonic XP GraphQL library](https://developer.enonic.com/docs/graphql-library) (`com.enonic.lib:lib-graphql`).
4
+
5
+ Covers all three server-side modules:
6
+
7
+ - `/lib/graphql` — schema builder, scalars, execution
8
+ - `/lib/graphql-connection` — Relay-style connection helper
9
+ - `/lib/graphql-rx` — reactive subscription plumbing
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ npm install --save-dev @enonic-types/lib-graphql
15
+ ```
16
+
17
+ Versions track the library itself — install the same version as the `com.enonic.lib:lib-graphql` dependency in your `build.gradle`.
18
+
19
+ ## Setup
20
+
21
+ The declarations are ambient (`declare module "/lib/graphql"` etc.), so no `paths` mapping is needed — the package just has to be part of your TypeScript program. Add it to `types` in `tsconfig.json`:
22
+
23
+ ```json
24
+ {
25
+ "compilerOptions": {
26
+ "types": ["@enonic-types/lib-graphql"]
27
+ }
28
+ }
29
+ ```
30
+
31
+ Note: setting `types` disables automatic inclusion of other `@types`/`@enonic-types` packages, so list everything your project uses (e.g. `["@enonic-types/global", "@enonic-types/lib-graphql"]`).
32
+
33
+ Alternatively, reference it from any file in your program:
34
+
35
+ ```ts
36
+ /// <reference types="@enonic-types/lib-graphql" />
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ```ts
42
+ import {newSchemaGenerator, GraphQLString, GraphQLInt, nonNull, execute} from '/lib/graphql';
43
+
44
+ const schemaGenerator = newSchemaGenerator();
45
+
46
+ const personType = schemaGenerator.createObjectType({
47
+ name: 'Person',
48
+ fields: {
49
+ name: {type: nonNull(GraphQLString)},
50
+ age: {type: GraphQLInt}
51
+ }
52
+ });
53
+ ```
54
+
55
+ Heads-up: a named import of the extended `Date` scalar (`import {Date} from '/lib/graphql'`) shadows the global `Date` object in that file — prefer a namespace import (`graphQlLib.Date`) where that matters.
56
+
57
+ Full API reference: https://developer.enonic.com/docs/graphql-library
package/index.d.ts ADDED
@@ -0,0 +1,397 @@
1
+ // Type definitions for the Enonic XP GraphQL library (com.enonic.lib:lib-graphql).
2
+ // Covers three importable modules:
3
+ // /lib/graphql — schema builder, scalars, execution
4
+ // /lib/graphql-connection — Relay-style connection helper
5
+ // /lib/graphql-rx — reactive subscription plumbing
6
+ //
7
+ // Type names mirror the library documentation
8
+ // (https://developer.enonic.com/docs/graphql-library): OutputField,
9
+ // InterfaceField, InputField, ResolverEnvironment, ExecutionResult,
10
+ // ConnectionSource, PublishProcessor, SubscriptionSubscriber.
11
+
12
+ declare module "/lib/graphql" {
13
+ // ---------------------------------------------------------------------
14
+ // Opaque type handles
15
+ // ---------------------------------------------------------------------
16
+ // Consumers pass these around but do not introspect them. The `_kind`
17
+ // brand is nominal — TypeScript never observes it at runtime.
18
+
19
+ export interface GraphQLType {
20
+ readonly _kind?: string;
21
+ }
22
+
23
+ export interface GraphQLScalarType extends GraphQLType {
24
+ readonly _kind?: "Scalar";
25
+ }
26
+
27
+ export interface GraphQLObjectType extends GraphQLType {
28
+ readonly _kind?: "Object";
29
+ /** Returns the type's declared `name`. Used by `createConnectionType` to derive edge/connection names. */
30
+ getName(): string;
31
+ }
32
+
33
+ export interface GraphQLInputObjectType extends GraphQLType {
34
+ readonly _kind?: "InputObject";
35
+ }
36
+
37
+ export interface GraphQLInterfaceType extends GraphQLType {
38
+ readonly _kind?: "Interface";
39
+ }
40
+
41
+ export interface GraphQLUnionType extends GraphQLType {
42
+ readonly _kind?: "Union";
43
+ }
44
+
45
+ export interface GraphQLEnumType extends GraphQLType {
46
+ readonly _kind?: "Enum";
47
+ }
48
+
49
+ export interface GraphQLTypeReference extends GraphQLType {
50
+ readonly _kind?: "Reference";
51
+ }
52
+
53
+ /** Opaque schema handle produced by `createSchema` and consumed by `execute`. */
54
+ export interface GraphQLSchema {
55
+ readonly _kind?: "Schema";
56
+ }
57
+
58
+ // ---------------------------------------------------------------------
59
+ // Scalars
60
+ // ---------------------------------------------------------------------
61
+
62
+ export const GraphQLInt: GraphQLScalarType;
63
+ export const GraphQLFloat: GraphQLScalarType;
64
+ export const GraphQLString: GraphQLScalarType;
65
+ export const GraphQLBoolean: GraphQLScalarType;
66
+ export const GraphQLID: GraphQLScalarType;
67
+
68
+ /**
69
+ * Extended `Date` scalar. Note: a named import (`import {Date} from '/lib/graphql'`)
70
+ * shadows the global `Date` object in that file.
71
+ */
72
+ export const Date: GraphQLScalarType;
73
+ export const DateTime: GraphQLScalarType;
74
+ export const Time: GraphQLScalarType;
75
+ export const Json: GraphQLScalarType;
76
+
77
+ export const LocalDateTime: GraphQLScalarType;
78
+ export const LocalTime: GraphQLScalarType;
79
+
80
+ // ---------------------------------------------------------------------
81
+ // Field / resolver types
82
+ // ---------------------------------------------------------------------
83
+
84
+ /**
85
+ * Environment passed to every output-field `resolve` function.
86
+ * Contains exactly three keys: `source`, `args` and `context`.
87
+ * Generic parameters let callers narrow each where they know the shape.
88
+ */
89
+ export interface ResolverEnvironment<Source = unknown, Args = Record<string, unknown>, Context = unknown> {
90
+ /** Value returned by the parent field. */
91
+ source: Source;
92
+ /** Arguments supplied for the current field. */
93
+ args: Args;
94
+ /** Context passed to `execute()`. */
95
+ context: Context;
96
+ }
97
+
98
+ /** Alias kept for graphql-java familiarity. */
99
+ export type DataFetchingEnvironment<
100
+ Source = unknown,
101
+ Args = Record<string, unknown>,
102
+ Context = unknown
103
+ > = ResolverEnvironment<Source, Args, Context>;
104
+
105
+ /**
106
+ * A fixed (non-function) resolver value. Kept separate from the function form
107
+ * so resolver signatures stay checked instead of collapsing to `unknown`.
108
+ */
109
+ export type ResolvedValue = string | number | boolean | null | unknown[] | Record<string, unknown>;
110
+
111
+ /**
112
+ * Defines a field on an object type. The `resolve` member is optional;
113
+ * when absent, the runtime reads a property with the field's name from
114
+ * `env.source`. It may also be a fixed value instead of a function.
115
+ *
116
+ * A subscription field's resolver returns a publisher
117
+ * (see `PublishProcessor`/`Flowable` in `/lib/graphql-rx`).
118
+ *
119
+ * `env.args` is typed as `any` in the default resolver signature so callers can
120
+ * narrow it to a concrete interface in their own resolver's `env` annotation
121
+ * without an explicit cast. The runtime never validates arg shape — the schema
122
+ * does — so this matches actual behavior.
123
+ */
124
+ export interface OutputField<Source = unknown, Context = unknown> {
125
+ type: GraphQLType;
126
+ /** Map of argument names to GraphQL input types. */
127
+ args?: Record<string, GraphQLType>;
128
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
129
+ resolve?: ((env: ResolverEnvironment<Source, any, Context>) => unknown) | ResolvedValue;
130
+ }
131
+
132
+ /** Alias kept for graphql-java familiarity. */
133
+ export type GraphQLFieldConfig<Source = unknown, Context = unknown> = OutputField<Source, Context>;
134
+
135
+ /** Defines a field on an interface — no `resolve`. */
136
+ export interface InterfaceField {
137
+ type: GraphQLType;
138
+ /** Map of argument names to GraphQL input types. */
139
+ args?: Record<string, GraphQLType>;
140
+ }
141
+
142
+ /** Defines a field on an input object. */
143
+ export interface InputField {
144
+ type: GraphQLType;
145
+ }
146
+
147
+ export type GraphQLFieldMap<Source = unknown, Context = unknown> = Record<
148
+ string,
149
+ OutputField<Source, Context>
150
+ >;
151
+
152
+ export type GraphQLInterfaceFieldMap = Record<string, InterfaceField>;
153
+
154
+ export type GraphQLInputFieldMap = Record<string, InputField>;
155
+
156
+ // ---------------------------------------------------------------------
157
+ // Schema builder parameters
158
+ // ---------------------------------------------------------------------
159
+
160
+ export interface CreateSchemaParams {
161
+ query: GraphQLObjectType;
162
+ mutation?: GraphQLObjectType;
163
+ subscription?: GraphQLObjectType;
164
+ /** Additional types needed for reference resolution. */
165
+ dictionary?: GraphQLObjectType[];
166
+ }
167
+
168
+ export interface CreateObjectTypeParams<Source = unknown, Context = unknown> {
169
+ name: string;
170
+ description?: string;
171
+ fields: GraphQLFieldMap<Source, Context>;
172
+ interfaces?: Array<GraphQLInterfaceType | GraphQLTypeReference>;
173
+ }
174
+
175
+ export interface CreateInputObjectTypeParams {
176
+ name: string;
177
+ description?: string;
178
+ fields: GraphQLInputFieldMap;
179
+ }
180
+
181
+ export interface CreateInterfaceTypeParams<Source = unknown> {
182
+ name: string;
183
+ description?: string;
184
+ fields: GraphQLInterfaceFieldMap;
185
+ /**
186
+ * Called at execution time to pick the concrete `GraphQLObjectType` for a runtime value.
187
+ * Must return an already-defined object type (not a reference).
188
+ */
189
+ typeResolver: (source: Source) => GraphQLObjectType;
190
+ }
191
+
192
+ export interface CreateUnionTypeParams<Source = unknown> {
193
+ name: string;
194
+ /** Possible types of the union. Must be non-empty. */
195
+ types: Array<GraphQLObjectType | GraphQLTypeReference>;
196
+ typeResolver: (source: Source) => GraphQLObjectType;
197
+ description?: string;
198
+ }
199
+
200
+ /**
201
+ * Enum values can be provided as a `string[]` (name === value) or as a
202
+ * `Record<string, unknown>` mapping enum-name -> backing value.
203
+ */
204
+ export interface CreateEnumTypeParams {
205
+ name: string;
206
+ values: string[] | Record<string, unknown>;
207
+ description?: string;
208
+ }
209
+
210
+ // ---------------------------------------------------------------------
211
+ // Schema generator
212
+ // ---------------------------------------------------------------------
213
+
214
+ export interface SchemaGenerator {
215
+ createSchema(params: CreateSchemaParams): GraphQLSchema;
216
+ createObjectType<Source = unknown, Context = unknown>(
217
+ params: CreateObjectTypeParams<Source, Context>
218
+ ): GraphQLObjectType;
219
+ createInputObjectType(params: CreateInputObjectTypeParams): GraphQLInputObjectType;
220
+ createInterfaceType<Source = unknown>(params: CreateInterfaceTypeParams<Source>): GraphQLInterfaceType;
221
+ createUnionType<Source = unknown>(params: CreateUnionTypeParams<Source>): GraphQLUnionType;
222
+ createEnumType(params: CreateEnumTypeParams): GraphQLEnumType;
223
+ /**
224
+ * Like `createObjectType`, but cached per generator: subsequent calls on the
225
+ * same generator return the type created by the first call.
226
+ */
227
+ createPageInfoObjectType<Source = unknown, Context = unknown>(
228
+ params: CreateObjectTypeParams<Source, Context>
229
+ ): GraphQLObjectType;
230
+ }
231
+
232
+ export function newSchemaGenerator(): SchemaGenerator;
233
+
234
+ // ---------------------------------------------------------------------
235
+ // Type wrappers
236
+ // ---------------------------------------------------------------------
237
+
238
+ /** Wraps a type to indicate a list of that type (`[T]`). */
239
+ export function list(type: GraphQLType): GraphQLType;
240
+
241
+ /** Wraps a type to indicate a non-null occurrence (`T!`). */
242
+ export function nonNull(type: GraphQLType): GraphQLType;
243
+
244
+ /** Placeholder for a type identified by name — resolved when the schema is assembled. */
245
+ export function reference(typeKey: string): GraphQLTypeReference;
246
+
247
+ // ---------------------------------------------------------------------
248
+ // Execution
249
+ // ---------------------------------------------------------------------
250
+
251
+ export interface ErrorLocation {
252
+ line: number;
253
+ column: number;
254
+ }
255
+
256
+ /** A validation or data-fetching error in an `ExecutionResult`. */
257
+ export interface ExecutionError {
258
+ errorType: string;
259
+ message: string;
260
+ locations?: ErrorLocation[];
261
+ /** Present on validation errors. */
262
+ validationErrorType?: string;
263
+ /** Present on data-fetching errors caused by a thrown exception. */
264
+ exception?: {
265
+ name: string;
266
+ message?: string;
267
+ };
268
+ }
269
+
270
+ /**
271
+ * The mapped result returned by `execute()` and delivered to subscription callbacks.
272
+ *
273
+ * For a subscription operation, `data` is a publisher rather than an object —
274
+ * type it explicitly with `execute<Flowable>(...)` using `Flowable` from
275
+ * `/lib/graphql-rx`, then call `data.subscribe(...)`.
276
+ */
277
+ export interface ExecutionResult<Data = unknown> {
278
+ data?: Data;
279
+ errors?: ExecutionError[];
280
+ }
281
+
282
+ /**
283
+ * Runs a query against a schema. Arguments are positional; `variables` and
284
+ * `context` are optional.
285
+ */
286
+ export function execute<Data = unknown>(
287
+ schema: GraphQLSchema,
288
+ query: string,
289
+ variables?: Record<string, unknown>,
290
+ context?: unknown
291
+ ): ExecutionResult<Data>;
292
+ }
293
+
294
+ declare module "/lib/graphql-connection" {
295
+ import type { GraphQLObjectType, SchemaGenerator } from "/lib/graphql";
296
+
297
+ /**
298
+ * The value a resolver must return for a field whose type was created by
299
+ * `createConnectionType()`.
300
+ */
301
+ export interface ConnectionSource<Node = unknown> {
302
+ /** Total number of available items. */
303
+ total: number;
304
+ /** Zero-based index of the first item in `hits`. */
305
+ start: number;
306
+ /** Items in the current page. */
307
+ hits: Node[];
308
+ }
309
+
310
+ /**
311
+ * Builds a Relay-style connection object type wrapping the given node type.
312
+ * Arguments are positional. The connection is generated with
313
+ * `<NodeType>Connection` / `<NodeType>Edge` names derived from `type.getName()`,
314
+ * and exposes the fields `totalCount`, `edges` and `pageInfo`.
315
+ */
316
+ export function createConnectionType(
317
+ schemaGenerator: SchemaGenerator,
318
+ type: GraphQLObjectType
319
+ ): GraphQLObjectType;
320
+
321
+ /**
322
+ * Base64-encodes a cursor value. The value is coerced with `String()`;
323
+ * typically a start offset.
324
+ */
325
+ export function encodeCursor(value: unknown): string;
326
+
327
+ /** Reverse of `encodeCursor`. Always returns a string — `parseInt` before arithmetic. */
328
+ export function decodeCursor(value: string): string;
329
+ }
330
+
331
+ declare module "/lib/graphql-rx" {
332
+ import type { ExecutionResult } from "/lib/graphql";
333
+
334
+ /**
335
+ * Structural stand-in for `java.lang.Throwable`. Construct one with
336
+ * `Java.type()` — a string or a JavaScript `Error` is not accepted at runtime.
337
+ */
338
+ export interface Throwable {
339
+ getMessage(): string | null;
340
+ }
341
+
342
+ /**
343
+ * A subscriber returned by `createSubscriber()`. It has no constructor.
344
+ */
345
+ export interface SubscriptionSubscriber {
346
+ readonly _kind?: "Subscriber";
347
+ /**
348
+ * Cancels its active subscription. Calling it before subscription or
349
+ * more than once has no effect.
350
+ */
351
+ cancelSubscription(): void;
352
+ }
353
+
354
+ /** Alias kept for reactive-streams familiarity. */
355
+ export type Subscriber = SubscriptionSubscriber;
356
+
357
+ /**
358
+ * A publisher of values. Filters can be chained; each `filter` call returns a
359
+ * new publisher and leaves the source untouched.
360
+ */
361
+ export interface Flowable<T = unknown> {
362
+ /** Forwards only the values the predicate accepts. */
363
+ filter(predicate: (value: T) => boolean): Flowable<T>;
364
+ /** Subscribes a subscriber created with `createSubscriber()`. */
365
+ subscribe(subscriber: SubscriptionSubscriber): void;
366
+ }
367
+
368
+ /**
369
+ * A reactive event source returned by `createPublishProcessor()`. Push values
370
+ * via `onNext`, close via `onComplete`, or fail via `onError`. Return one
371
+ * (optionally filtered) from a subscription field resolver.
372
+ */
373
+ export interface PublishProcessor<T = unknown> extends Flowable<T> {
374
+ readonly _kind?: "PublishProcessor";
375
+ /** Publishes a value to active subscribers. Becomes the `source` of the subscription field's resolver. */
376
+ onNext(value: T): void;
377
+ /** Terminates the stream with an error. Takes a Java `Throwable` constructed with `Java.type()`. */
378
+ onError(error: Throwable): void;
379
+ /** Completes the stream. Subscribers receive no further events. */
380
+ onComplete(): void;
381
+ }
382
+
383
+ export interface CreateSubscriberParams<Data = unknown> {
384
+ /** Called with each mapped `ExecutionResult` produced by a subscription. */
385
+ onNext?: (result: ExecutionResult<Data>) => void;
386
+ }
387
+
388
+ /** Creates a publish processor that can be returned by a subscription field resolver. */
389
+ export function createPublishProcessor<T = unknown>(): PublishProcessor<T>;
390
+
391
+ /** Creates a subscriber for the publisher returned as `data` by a subscription execution. */
392
+ export function createSubscriber<Data = unknown>(
393
+ params: CreateSubscriberParams<Data>
394
+ ): SubscriptionSubscriber;
395
+ }
396
+
397
+ export {};
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@enonic-types/lib-graphql",
3
+ "version": "3.0.0",
4
+ "description": "Type definitions for the Enonic XP GraphQL library",
5
+ "types": "index.d.ts",
6
+ "files": [
7
+ "index.d.ts"
8
+ ],
9
+ "keywords": [
10
+ "enonic",
11
+ "enonic-xp",
12
+ "lib-graphql",
13
+ "graphql",
14
+ "types",
15
+ "typescript"
16
+ ],
17
+ "license": "Apache-2.0",
18
+ "homepage": "https://developer.enonic.com/docs/graphql-library",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/enonic/lib-graphql.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/enonic/lib-graphql/issues"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ }
29
+ }