generateLibrary function

LibraryDefinition generateLibrary(
  1. String path,
  2. List<DocumentNode> gqlDocs,
  3. GeneratorOptions options,
  4. SchemaMap schemaMap,
  5. List<FragmentDefinitionNode> fragmentsCommon,
  6. DocumentNode schema,
)

Generate queries definitions from a GraphQL schema and a list of queries, given Artemis options and schema mappings.

Implementation

LibraryDefinition generateLibrary(
  String path,
  List<DocumentNode> gqlDocs,
  GeneratorOptions options,
  SchemaMap schemaMap,
  List<FragmentDefinitionNode> fragmentsCommon,
  DocumentNode schema,
) {
  final typeDefinitionNodeVisitor = TypeDefinitionNodeVisitor();
  schema.accept(typeDefinitionNodeVisitor);

  final canonicalVisitor = CanonicalVisitor(
    context: Context(
      schema: schema,
      typeDefinitionNodeVisitor: typeDefinitionNodeVisitor,
      options: options,
      schemaMap: schemaMap,
      path: [],
      currentType: null,
      currentFieldName: null,
      currentClassName: null,
      generatedClasses: [],
      inputsClasses: [],
      fragments: [],
      usedEnums: {},
      usedInputObjects: {},
    ),
  );

  schema.accept(canonicalVisitor);

  final documentFragments = gqlDocs
      .map((doc) => doc.definitions.whereType<FragmentDefinitionNode>())
      .expand((e) => e)
      .toList();

  final documentsWithoutFragments = gqlDocs.map((doc) {
    return DocumentNode(
      definitions:
          doc.definitions.where((e) => e is! FragmentDefinitionNode).toList(),
      span: doc.span,
    );
  }).toList();

  final queryDefinitions = documentsWithoutFragments
      .map((doc) => generateDefinitions(
            schema: schema,
            typeDefinitionNodeVisitor: typeDefinitionNodeVisitor,
            path: path,
            document: doc,
            options: options,
            schemaMap: schemaMap,
            fragmentsCommon: [
              ...documentFragments,
              ...fragmentsCommon,
            ],
            canonicalVisitor: canonicalVisitor,
          ))
      .expand((e) => e)
      .toList();

  final allClassesNames = queryDefinitions
      .map((def) => def.classes.map((c) => c))
      .expand((e) => e)
      .toList();

  allClassesNames.mergeDuplicatesBy((a) => a.name, (a, b) {
    if (a.name == b.name && a != b) {
      throw DuplicatedClassesException(a, b);
    }

    return a;
  });

  final basename = p.basenameWithoutExtension(path);

  final customImports = _extractCustomImports(schema, options);
  return LibraryDefinition(
    basename: basename,
    queries: queryDefinitions,
    customImports: customImports,
  );
}