generateDefinitions function

Iterable<QueryDefinition> generateDefinitions({
  1. required DocumentNode schema,
  2. required TypeDefinitionNodeVisitor typeDefinitionNodeVisitor,
  3. required String path,
  4. required DocumentNode document,
  5. required GeneratorOptions options,
  6. required SchemaMap schemaMap,
  7. required List<FragmentDefinitionNode> fragmentsCommon,
  8. required CanonicalVisitor canonicalVisitor,
})

Generate a query definition from a GraphQL schema and a query, given Artemis options and schema mappings.

Implementation

Iterable<QueryDefinition> generateDefinitions({
  required DocumentNode schema,
  required TypeDefinitionNodeVisitor typeDefinitionNodeVisitor,
  required String path,
  required DocumentNode document,
  required GeneratorOptions options,
  required SchemaMap schemaMap,
  required List<FragmentDefinitionNode> fragmentsCommon,
  required CanonicalVisitor canonicalVisitor,
}) {
  // final documentFragments =
  //     document.definitions.whereType<FragmentDefinitionNode>();

  // if (documentFragments.isNotEmpty && fragmentsCommon.isNotEmpty) {
  //   throw FragmentIgnoreException();
  // }

  final operations =
      document.definitions.whereType<OperationDefinitionNode>().toList();

  return operations.map((operation) {
    // final fragments = <FragmentDefinitionNode>[
    //   ...documentFragments,
    // ];
    final definitions = document.definitions
        // filtering unused operations
        .where((e) {
      return e is! OperationDefinitionNode || e == operation;
    }).toList();

    if (fragmentsCommon.isNotEmpty) {
      final fragmentsOperation =
          _extractFragments(operation.selectionSet, fragmentsCommon);
      definitions.addAll(fragmentsOperation);
      // fragments.addAll(fragmentsOperation);
    }

    final basename = p.basenameWithoutExtension(path).split('.').first;
    final operationName = operation.name?.value ?? basename;

    final schemaVisitor = SchemaDefinitionVisitor();
    final objectVisitor = ObjectTypeDefinitionVisitor();

    schema.accept(schemaVisitor);
    schema.accept(objectVisitor);

    String suffix;
    switch (operation.type) {
      case OperationType.subscription:
        suffix = 'Subscription';
        break;
      case OperationType.mutation:
        suffix = 'Mutation';
        break;
      case OperationType.query:
      default:
        suffix = 'Query';
        break;
    }

    final rootTypeName =
        (schemaVisitor.schemaDefinitionNode?.operationTypes ?? [])
                .firstWhereOrNull((e) => e.operation == operation.type)
                ?.type
                .name
                .value ??
            suffix;

    final parentType = objectVisitor.getByName(rootTypeName);

    if (parentType == null) {
      throw MissingRootTypeException(rootTypeName);
    }

    final name = QueryName.fromPath(
      path: createPathName([
        ClassName(name: operationName),
        ClassName(name: parentType.name.value)
      ], schemaMap.namingScheme),
    );

    final context = Context(
      schema: schema,
      typeDefinitionNodeVisitor: typeDefinitionNodeVisitor,
      options: options,
      schemaMap: schemaMap,
      path: [
        TypeName(name: operationName),
        TypeName(name: parentType.name.value)
      ],
      currentType: parentType,
      currentFieldName: null,
      currentClassName: null,
      generatedClasses: [],
      inputsClasses: [],
      fragments: fragmentsCommon,
      usedEnums: {},
      usedInputObjects: {},
    );

    final visitor = GeneratorVisitor(context: context);
    final documentDefinitions = DocumentNode(definitions: definitions);
    documentDefinitions.accept(visitor);

    return QueryDefinition(
      name: name,
      operationName: operationName,
      document: documentDefinitions,
      classes: [
        ...context.usedEnums
            .map((e) => canonicalVisitor.enums[e.name]?.call())
            .whereType<Definition>(),
        ...visitor.context.generatedClasses,
        ...context.usedInputObjects
            .map((e) => canonicalVisitor.inputObjects[e.name]?.call())
            .whereType<Definition>(),
      ],
      inputs: visitor.context.inputsClasses,
      generateHelpers: options.generateHelpers,
      generateQueries: options.generateQueries,
      suffix: suffix,
    );
  });
}