0
# Service Classes
1
2
Core service classes for exploring metadata, handling execution context, and managing GraphQL functionality. These services provide the underlying infrastructure for NestJS GraphQL's code-first approach and runtime operations.
3
4
## Capabilities
5
6
### Base Explorer Service
7
8
Foundation service for exploring and extracting metadata from NestJS modules.
9
10
```typescript { .api }
11
/**
12
* Base class for metadata exploration services
13
*/
14
export abstract class BaseExplorerService {
15
/**
16
* Explores modules and extracts relevant metadata
17
* @returns Array of metadata objects
18
*/
19
abstract explore(): any[];
20
}
21
```
22
23
### Resolver Explorer Service
24
25
Service for discovering and processing GraphQL resolvers within NestJS modules.
26
27
```typescript { .api }
28
/**
29
* Service for exploring and extracting resolver metadata from modules
30
*/
31
export class ResolversExplorerService extends BaseExplorerService {
32
/**
33
* Explores modules to find GraphQL resolvers
34
* @param modules - Array of NestJS modules to explore
35
* @returns Array of resolver metadata
36
*/
37
exploreResolvers(modules: Module[]): ResolverMetadata[];
38
39
/**
40
* Extracts field resolvers from resolver classes
41
* @param resolverClass - The resolver class to analyze
42
* @returns Array of field resolver metadata
43
*/
44
extractFieldResolvers(resolverClass: Type<any>): FieldResolverMetadata[];
45
}
46
```
47
48
**Usage Examples:**
49
50
```typescript
51
import { ResolversExplorerService } from "@nestjs/graphql";
52
53
const resolversExplorer = new ResolversExplorerService();
54
const resolverMetadata = resolversExplorer.exploreResolvers(appModules);
55
```
56
57
### Scalar Explorer Service
58
59
Service for discovering and processing custom GraphQL scalars.
60
61
```typescript { .api }
62
/**
63
* Service for exploring and extracting scalar metadata from modules
64
*/
65
export class ScalarsExplorerService extends BaseExplorerService {
66
/**
67
* Explores modules to find custom GraphQL scalars
68
* @param modules - Array of NestJS modules to explore
69
* @returns Array of scalar metadata
70
*/
71
exploreScalars(modules: Module[]): ScalarMetadata[];
72
73
/**
74
* Extracts scalar definition from scalar class
75
* @param scalarClass - The scalar class to analyze
76
* @returns Scalar metadata
77
*/
78
extractScalarMetadata(scalarClass: Type<any>): ScalarMetadata;
79
}
80
```
81
82
### GraphQL AST Explorer
83
84
Service for exploring GraphQL AST (Abstract Syntax Tree) and generating TypeScript definitions.
85
86
```typescript { .api }
87
/**
88
* Service for exploring GraphQL AST and generating type definitions
89
*/
90
export class GraphQLAstExplorer {
91
/**
92
* Explores GraphQL document node and generates TypeScript definitions
93
* @param documentNode - GraphQL document AST node
94
* @param outputPath - Path where to write generated definitions
95
* @param mode - Output format mode
96
*/
97
explore(
98
documentNode: DocumentNode,
99
outputPath: string,
100
mode: GenerateOptions['outputAs']
101
): Promise<void>;
102
103
/**
104
* Generates TypeScript interfaces from GraphQL types
105
* @param typeNode - GraphQL type definition node
106
* @returns TypeScript interface definition
107
*/
108
generateTypeDefinition(typeNode: TypeDefinitionNode): string;
109
}
110
```
111
112
**Usage Examples:**
113
114
```typescript
115
import { GraphQLAstExplorer } from "@nestjs/graphql";
116
import { parse } from "graphql";
117
118
const astExplorer = new GraphQLAstExplorer();
119
const schema = parse(`
120
type User {
121
id: ID!
122
name: String!
123
}
124
`);
125
126
await astExplorer.explore(schema, './generated-types.ts', 'interface');
127
```
128
129
### GraphQL Types Loader
130
131
Service for loading and merging GraphQL type definitions from files.
132
133
```typescript { .api }
134
/**
135
* Service for loading GraphQL schema definitions from files
136
*/
137
export class GraphQLTypesLoader {
138
/**
139
* Merges GraphQL type definitions from multiple file paths
140
* @param paths - Array of file paths containing GraphQL schemas
141
* @returns Merged GraphQL schema string
142
*/
143
mergeTypesByPaths(paths: string[]): string;
144
145
/**
146
* Loads GraphQL schema from a single file
147
* @param path - File path to GraphQL schema
148
* @returns GraphQL schema string
149
*/
150
loadSchema(path: string): string;
151
}
152
```
153
154
**Usage Examples:**
155
156
```typescript
157
import { GraphQLTypesLoader } from "@nestjs/graphql";
158
159
const typesLoader = new GraphQLTypesLoader();
160
const mergedSchema = typesLoader.mergeTypesByPaths([
161
'./schemas/user.graphql',
162
'./schemas/post.graphql'
163
]);
164
```
165
166
### Type Metadata Storage
167
168
Service for storing and managing type metadata used in schema generation.
169
170
```typescript { .api }
171
/**
172
* Storage for GraphQL type metadata
173
*/
174
export class TypeMetadataStorage {
175
/**
176
* Adds field metadata to storage
177
* @param metadata - Field metadata to store
178
*/
179
addFieldMetadata(metadata: FieldMetadata): void;
180
181
/**
182
* Adds class metadata to storage
183
* @param metadata - Class metadata to store
184
*/
185
addClassMetadata(metadata: ClassMetadata): void;
186
187
/**
188
* Compiles and returns complete metadata for a target class
189
* @param target - Target class constructor
190
* @returns Compiled class metadata
191
*/
192
compileClassMetadata(target: Function): ClassMetadata;
193
194
/**
195
* Gets all field metadata for a specific target
196
* @param target - Target class constructor
197
* @returns Array of field metadata
198
*/
199
getFieldMetadata(target: Function): FieldMetadata[];
200
201
/**
202
* Clears all stored metadata
203
*/
204
clear(): void;
205
}
206
```
207
208
### Metadata Interfaces
209
210
Supporting interfaces for metadata storage and exploration.
211
212
```typescript { .api }
213
/**
214
* Metadata for GraphQL resolver classes
215
*/
216
interface ResolverMetadata {
217
target: Function;
218
typeFn?: ReturnTypeFunc;
219
isAbstract?: boolean;
220
parent?: Function;
221
}
222
223
/**
224
* Metadata for GraphQL field resolvers
225
*/
226
interface FieldResolverMetadata {
227
target: Function;
228
methodName: string;
229
fieldName?: string;
230
typeFn?: ReturnTypeFunc;
231
options?: ResolveFieldOptions;
232
}
233
234
/**
235
* Metadata for custom GraphQL scalars
236
*/
237
interface ScalarMetadata {
238
target: Function;
239
name: string;
240
typeFn?: ReturnTypeFunc;
241
}
242
243
/**
244
* Metadata for GraphQL fields
245
*/
246
interface FieldMetadata {
247
target: Function;
248
propertyKey: string;
249
typeFn?: ReturnTypeFunc;
250
options?: FieldOptions;
251
}
252
253
/**
254
* Metadata for GraphQL classes (ObjectType, InputType, etc.)
255
*/
256
interface ClassMetadata {
257
target: Function;
258
name?: string;
259
description?: string;
260
isAbstract?: boolean;
261
interfaces?: Function[];
262
fields: FieldMetadata[];
263
}
264
265
/**
266
* Options for generating TypeScript definitions
267
*/
268
interface GenerateOptions {
269
outputAs?: 'class' | 'interface';
270
outputPath?: string;
271
watch?: boolean;
272
debug?: boolean;
273
}
274
275
/**
276
* NestJS module reference
277
*/
278
interface Module {
279
metatype: Type<any>;
280
providers: any[];
281
controllers: any[];
282
exports: any[];
283
}
284
```
285
286
**Usage Examples:**
287
288
```typescript
289
import { TypeMetadataStorage } from "@nestjs/graphql";
290
291
const storage = new TypeMetadataStorage();
292
293
// Store field metadata
294
storage.addFieldMetadata({
295
target: UserClass,
296
propertyKey: 'name',
297
typeFn: () => String,
298
options: { nullable: false }
299
});
300
301
// Retrieve compiled metadata
302
const classMetadata = storage.compileClassMetadata(UserClass);
303
```