generateLibrary method
Generates a complete library definition from GraphQL documents.
This method coordinates the entire generation process:
- Sets up the canonical visitor for enums and input objects
- Processes fragments from all documents
- Generates query definitions for each document
- Merges and validates all generated classes
- Returns a complete library definition
path - The file path for the generated library
gqlDocs - List of GraphQL documents to process
options - Generator options and configuration
schemaMap - Schema mapping configuration
fragmentsCommon - Common fragments shared across documents
Returns a LibraryDefinition containing all generated code
Throws DuplicatedClassesException if duplicate classes are found Throws GenerationProcessError if generation fails
Implementation
LibraryDefinition generateLibrary(
String path,
List<DocumentNode> gqlDocs,
GeneratorOptions options,
SchemaMap schemaMap,
List<FragmentDefinitionNode> fragmentsCommon,
) {
try {
// Validate inputs
if (path.isEmpty) {
throw GenerationProcessError('Path cannot be empty');
}
if (gqlDocs.isEmpty) {
throw GenerationProcessError('No GraphQL documents provided');
}
// Set up canonical visitor for enums and input objects
final canonicalVisitor = CanonicalVisitor(
context: Context(
schema: schemaService.schema,
typeDefinitionNodeVisitor: schemaService.typeVisitor,
options: options,
schemaMap: schemaMap,
path: [],
currentType: null,
currentFieldName: null,
currentClassName: null,
generatedClasses: [],
inputsClasses: [],
fragments: [],
usedEnums: {},
usedInputObjects: {},
),
);
schemaService.schema.accept(canonicalVisitor);
// Extract fragments from all documents
final documentFragments = gqlDocs
.map((doc) => doc.definitions.whereType<FragmentDefinitionNode>())
.expand((e) => e)
.toList();
// Create documents without fragments for processing
final documentsWithoutFragments = gqlDocs.map((doc) {
return DocumentNode(
definitions: doc.definitions
.where((e) => e is! FragmentDefinitionNode)
.toList(),
span: doc.span,
);
}).toList();
// Generate query definitions for each document
final queryDefinitions = documentsWithoutFragments
.map(
(doc) => generateDefinitions(
path: path,
document: doc,
options: options,
schemaMap: schemaMap,
fragmentsCommon: [
...documentFragments,
...fragmentsCommon,
],
canonicalVisitor: canonicalVisitor,
),
)
.expand((e) => e)
.toList();
// Validate and merge all generated classes
final allClassesNames = queryDefinitions
.map((def) => def.classes.map((c) => c))
.expand((e) => e)
.toList();
_validateAndMergeDuplicateClasses(allClassesNames);
// Extract basename and custom imports
final basename = FileService.extractBasename(path);
final customImports = schemaService.extractCustomImports(options);
return LibraryDefinition(
basename: basename,
queries: queryDefinitions,
customImports: customImports,
schemaMap: schemaMap,
);
} catch (e) {
// Preserve original exception types for backward compatibility
if (e is GenerationProcessError ||
e is DuplicatedClassesException ||
e is MissingRootTypeException ||
e is MissingFragmentException ||
e is MissingScalarConfigurationException ||
e is QueryGlobsSchemaException ||
e is QueryGlobsOutputException ||
e is MissingFilesException ||
e is MissingBuildConfigurationException) {
rethrow;
}
throw GenerationProcessError(
'Failed to generate library: $e',
context: 'Path: $path, Documents: ${gqlDocs.length}',
suggestion: 'Check your GraphQL documents and schema for errors',
);
}
}