Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

feat: Improve support for nested fragments #309

Open
wants to merge 3 commits into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 7.0.0-beta.12

- support inline fragments in fragment definitions

## 7.0.0-beta.11

- document generation fix for https://github.com/comigor/artemis/issues/307
Expand Down
2 changes: 1 addition & 1 deletion lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ Make sure your query is correct and your schema is updated.''');
nextFieldName: nextFieldName,
nextClassName: ClassName(name: nextType.name.value),
alias: fieldAlias,
ofUnion: Nullable<TypeDefinitionNode?>(null),
ofUnion: Nullable<Name?>(null),
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/generator/data/class_definition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ClassDefinition extends Definition with DataPrinter {
final Iterable<ClassProperty> properties;

/// The type this class extends from, or [null].
final Name? extension;
final ClassName? extension;

/// The types this class implements.
final Iterable<String> implementations;
Expand Down
4 changes: 2 additions & 2 deletions lib/generator/data/fragment_class_definition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class FragmentClassDefinition extends Definition with DataPrinter {
/// Instantiate a fragment class definition.
FragmentClassDefinition({
required this.name,
required this.properties,
}) : assert(hasValue(name) && hasValue(properties)),
this.properties = const [],
}) : assert(hasValue(name.name)),
super(name: name);

@override
Expand Down
12 changes: 6 additions & 6 deletions lib/generator/ephemeral_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Context {
final Name? currentFieldName;

/// If part of an union type, which [TypeDefinitionNode] it represents.
final TypeDefinitionNode? ofUnion;
final Name? ofUnion;

/// A string to replace the current class name.
final Name? alias;
Expand Down Expand Up @@ -114,7 +114,7 @@ class Context {
required TypeDefinitionNode nextType,
required Name? nextFieldName,
required Name? nextClassName,
Nullable<TypeDefinitionNode?>? ofUnion,
Nullable<Name?>? ofUnion,
Name? alias,
List<Definition>? generatedClasses,
List<QueryInput>? inputsClasses,
Expand Down Expand Up @@ -143,7 +143,7 @@ class Context {
Name? nextFieldName,
Name? nextClassName,
Name? alias,
Nullable<TypeDefinitionNode?>? ofUnion,
Nullable<Name?>? ofUnion,
List<Definition>? generatedClasses,
List<QueryInput>? inputsClasses,
List<FragmentDefinitionNode>? fragments,
Expand Down Expand Up @@ -203,7 +203,7 @@ class Context {
Name? nextFieldName,
Name? nextClassName,
Name? alias,
Nullable<TypeDefinitionNode?>? ofUnion,
Nullable<Name?>? ofUnion,
List<Definition>? generatedClasses,
List<QueryInput>? inputsClasses,
List<FragmentDefinitionNode>? fragments,
Expand Down Expand Up @@ -261,7 +261,7 @@ class Context {
/// Returns a copy of this context, with the same type, but on the first path.
Context sameTypeWithNoPath({
Name? alias,
Nullable<TypeDefinitionNode?>? ofUnion,
Nullable<Name?>? ofUnion,
List<Definition>? generatedClasses,
List<QueryInput>? inputsClasses,
List<FragmentDefinitionNode>? fragments,
Expand Down Expand Up @@ -289,7 +289,7 @@ class Context {
required TypeDefinitionNode nextType,
required Name nextFieldName,
required Name nextClassName,
Nullable<TypeDefinitionNode?>? ofUnion,
Nullable<Name?>? ofUnion,
Name? alias,
List<Definition>? generatedClasses,
List<QueryInput>? inputsClasses,
Expand Down
6 changes: 3 additions & 3 deletions lib/visitor/canonical_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CanonicalVisitor extends RecursiveVisitor {

final nextContext = context.sameTypeWithNoPath(
alias: enumName,
ofUnion: Nullable<TypeDefinitionNode?>(null),
ofUnion: Nullable<Name?>(null),
);

logFn(context, nextContext.align, '-> Enum');
Expand All @@ -53,7 +53,7 @@ class CanonicalVisitor extends RecursiveVisitor {
final name = ClassName(name: node.name.value);
final nextContext = context.sameTypeWithNoPath(
alias: name,
ofUnion: Nullable<TypeDefinitionNode?>(null),
ofUnion: Nullable<Name?>(null),
);

logFn(context, nextContext.align, '-> Input class');
Expand All @@ -69,7 +69,7 @@ class CanonicalVisitor extends RecursiveVisitor {
nextType: node,
nextClassName: ClassName(name: nextType.name.value),
nextFieldName: ClassName(name: i.name.value),
ofUnion: Nullable<TypeDefinitionNode?>(null),
ofUnion: Nullable<Name?>(null),
),
markAsUsed: false,
);
Expand Down
32 changes: 16 additions & 16 deletions lib/visitor/generator_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,20 @@ class GeneratorVisitor extends RecursiveVisitor {
possibleTypes.addAll(Map.fromIterables(keys, values));
}

final partOfUnion = nextContext.ofUnion != null;
if (partOfUnion) {}

final name = ClassName.fromPath(path: nextContext.fullPathName());
logFn(context, nextContext.align,
'└ ${nextContext.path}[${nextContext.currentType!.name.value}][${nextContext.currentClassName} ${nextContext.currentFieldName}] (${nextContext.alias ?? ''})');
logFn(context, nextContext.align,
'<- Generated class ${name.namePrintable}.');

final ofUnion = context.ofUnion;
if (ofUnion is FragmentName) {
_mixins.add(ofUnion);
}
nextContext.generatedClasses.add(ClassDefinition(
name: name,
properties: _classProperties,
mixins: _mixins,
extension: partOfUnion
? ClassName.fromPath(path: nextContext.rollbackPath().fullPathName())
: null,
extension: ofUnion is ClassName ? ofUnion : null,
factoryPossibilities: possibleTypes,
));
}
Expand All @@ -94,15 +92,16 @@ class GeneratorVisitor extends RecursiveVisitor {
void visitInlineFragmentNode(InlineFragmentNode node) {
logFn(context, context.align + 1,
'${context.path}: ... on ${node.typeCondition!.on.name.value}');
final ofUnion = context.ofUnion ??
ClassName.fromPath(path: context.rollbackPath().fullPathName());
final nextType = gql.getTypeByName(context.schema, node.typeCondition!.on);

if (nextType.name.value == context.currentType!.name.value) {
final visitor = GeneratorVisitor(
context: context.nextTypeWithSamePath(
nextType: nextType,
nextClassName: null,
nextFieldName: null,
ofUnion: Nullable<TypeDefinitionNode?>(context.currentType),
ofUnion: Nullable<Name?>(ofUnion),
inputsClasses: [],
fragments: [],
),
Expand All @@ -114,7 +113,7 @@ class GeneratorVisitor extends RecursiveVisitor {
nextType: nextType,
nextClassName: ClassName(name: nextType.name.value),
nextFieldName: ClassPropertyName(name: nextType.name.value),
ofUnion: Nullable<TypeDefinitionNode?>(context.currentType),
ofUnion: Nullable<Name?>(ofUnion),
inputsClasses: [],
fragments: [],
),
Expand Down Expand Up @@ -149,7 +148,7 @@ class GeneratorVisitor extends RecursiveVisitor {
nextType: leafType,
nextClassName: ClassName(name: leafType.name.value),
nextFieldName: ClassName(name: node.variable.name.value),
ofUnion: Nullable<TypeDefinitionNode?>(null),
ofUnion: Nullable<Name?>(null),
)
.fullPathName();

Expand Down Expand Up @@ -234,15 +233,15 @@ class GeneratorVisitor extends RecursiveVisitor {
path: context
.sameTypeWithNoPath(
alias: FragmentName(name: node.name.value),
ofUnion: Nullable<TypeDefinitionNode?>(null),
ofUnion: Nullable<Name?>(null),
)
.fullPathName());

final visitor = GeneratorVisitor(
context: context.sameTypeWithNextPath(
alias: fragmentName,
generatedClasses: [],
ofUnion: Nullable<TypeDefinitionNode?>(null),
ofUnion: Nullable<Name?>(null),
log: false,
),
);
Expand All @@ -260,7 +259,7 @@ class GeneratorVisitor extends RecursiveVisitor {
final partName = FragmentName(name: node.name.value);
final nextContext = context.sameTypeWithNoPath(
alias: partName,
ofUnion: Nullable<TypeDefinitionNode?>(null),
ofUnion: Nullable<Name?>(null),
);

logFn(context, nextContext.align, '-> Fragment');
Expand All @@ -270,6 +269,8 @@ class GeneratorVisitor extends RecursiveVisitor {

final nextType =
gql.getTypeByName(nextContext.schema, node.typeCondition.on);
final fragmentName =
FragmentName.fromPath(path: nextContext.fullPathName());

final visitorContext = Context(
schema: context.schema,
Expand All @@ -285,6 +286,7 @@ class GeneratorVisitor extends RecursiveVisitor {
fragments: [],
usedEnums: nextContext.usedEnums,
usedInputObjects: nextContext.usedInputObjects,
ofUnion: fragmentName,
);

final visitor = GeneratorVisitor(context: visitorContext);
Expand All @@ -298,8 +300,6 @@ class GeneratorVisitor extends RecursiveVisitor {
.expand((a) => a)
.mergeDuplicatesBy((a) => a.name, (a, b) => a);

final fragmentName =
FragmentName.fromPath(path: nextContext.fullPathName());
logFn(context, nextContext.align,
'└ ${nextContext.path}[${node.name.value}]');
logFn(context, nextContext.align,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: artemis
version: 7.0.0-beta.11
version: 7.0.0-beta.12

description: Build dart types from GraphQL schemas and queries (using Introspection Query).
homepage: https://github.com/comigor/artemis
Expand Down
Loading