-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
96 lines (96 loc) · 3.68 KB
/
Copy pathindex.ts
File metadata and controls
96 lines (96 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*!
* @imqueue/type-graphql-dependency - Declarative GraphQL dependency loading
*
* I'm Queue Software Project
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* If you want to use this code in a closed source (commercial) project, you can
* purchase a proprietary commercial license. Please contact us at
* <support@imqueue.com> to get commercial licensing options.
*/
/**
* `@imqueue/graphql-dependency` for `type-graphql` — declare cross-service
* dependencies on the decorated classes you already have, instead of on raw
* `GraphQLObjectType` values.
*
* The engine underneath is `@imqueue/graphql-dependency`, and the concepts are
* its concepts: a bulk **loader** per type, **requirements** describing which
* types own which, and an optional **initializer** that pre-fills fields the
* requirement filters need. What this package changes is where they are written.
* {@link DependencyFor} is a class decorator, so a type's relations sit on the
* class that defines it, and {@link Dependency} resolves a class back to its
* dependency description at request time.
*
* @remarks
* `type-graphql` builds its schema from decorated classes, which means the
* `GraphQLObjectType` a class becomes does not exist while the decorators are
* running. Every declaration is therefore deferred: {@link DependencyFor}
* registers a hook rather than wiring anything, and the hooks run once the schema
* is built.
*
* **Running them is the application's job.** Nothing here calls them. After
* building the schema, pass it to every hook in {@link schemaHooks} — miss that
* step and no dependency is ever registered, no error is raised, and the
* dependency fields simply stay empty:
*
* ```typescript
* const schema = await buildSchema({ resolvers: [...] });
*
* schemaHooks.forEach(hook => hook(schema));
* ```
*
* Because the wiring is deferred, the mistakes it catches surface then rather
* than at start-up: a class that is not a GraphQL type, a relation naming a field
* that does not exist, a filter referring to an unknown field. All of them throw
* `TypeError` from inside the hook, so run the hooks during boot rather than
* lazily on first request.
*
* @example
* ```typescript
* import {
* Dependency,
* DependencyFor,
* schemaHooks,
* } from '@imqueue/type-graphql-dependency';
* import { buildSchema, ObjectType } from 'type-graphql';
*
* @DependencyFor<Partial<Consumer>>({
* require: [
* [() => ApiKey, [{ as: 'apiKeys', filter: { consumerId: 'id' } }]],
* ],
* async load(context, filter, fields) {
* const { data } = await context.consumer.listConsumer(filter, fields);
*
* return data;
* },
* })
* @ObjectType()
* export class Consumer {
* // ... field definitions ...
* }
*
* // once, at boot
* const schema = await buildSchema({ resolvers: [ConsumerResolver] });
*
* schemaHooks.forEach(hook => hook(schema));
*
* // later, in a resolver
* await Dependency(Consumer).load(data, context, fields);
* ```
*
* @packageDocumentation
*/
export * from './src/index.js';