addAllModels method
void
addAllModels(
- CodeBuilder code, {
- ClassSnippetGenerator? extraGenerator,
- ClassSnippetGenerator? inputGenerator,
Implementation
void addAllModels(
CodeBuilder code, {
ClassSnippetGenerator? extraGenerator,
ClassSnippetGenerator? inputGenerator,
}) {
final fragmentNames = <String>[];
log.info('Writing Objects');
declaredObjectTypes.forEach((def) {
// If there are union types, add them now
final unionInterfaces = this
.unionTypes
.values
.where((union) => union.types.any((t) {
return t.name.value == def.name.value;
}))
.map((e) => e.name.value)
.toSet();
code += classDefinition(
this,
def.name,
fromJson: true,
toJson: true,
toMap: true,
isInput: false,
isData: true,
directives: def.directives,
isJoinRecord: def.directives.hasDirective('joinRecord'),
interfaces: [
'${def.name.value}Data',
...def.interfaces.map(
(e) => e.toDartType(withNullability: false),
)
],
fields: def.fields.map(
(d) => FieldDefinition.ofField(this, d, entityName: def.name.value),
),
extraGenerator: extraGenerator,
);
});
log.info('Writing Unions');
unionTypes.values.forEach((union) {
final unionInterface = union.directives
.getDirectiveValue('union', 'interface')
.stringValueOrNull;
late List<FieldDefinition> fields;
if (unionInterface != null) {
final unionInterfaceDef = interfaceTypes[unionInterface]!;
fields = unionInterfaceDef.fields.map((field) {
return FieldDefinition.ofField(this, field,
entityName: union.name.value);
}).toList();
} else {
fields = [];
}
code += classDefinition(
this,
union.name,
fromJson: false,
toJson: true,
toMap: true,
isInput: false,
subTypes: union.types.map((t) => t.name.value),
directives: union.directives,
isJoinRecord: union.directives.hasDirective('joinRecord'),
interfaces: [],
fields: fields,
extraGenerator: extraGenerator,
);
//Input
code += classDefinition(
this,
'${union.name.value}CreateInput'.toNameNode(),
fromJson: false,
toJson: true,
toMap: false,
isInput: true,
isAbstract: true,
subTypes: union.types.map((t) => t.name.value),
directives: union.directives,
isJoinRecord: union.directives.hasDirective('joinRecord'),
interfaces: [],
fields: [],
extraGenerator: extraGenerator,
);
//Input
code += classDefinition(
this,
'${union.name.value}UpdateInput'.toNameNode(),
fromJson: false,
toJson: true,
toMap: false,
isInput: true,
isAbstract: true,
subTypes: union.types.map((t) => t.name.value),
directives: union.directives,
isJoinRecord: union.directives.hasDirective('joinRecord'),
interfaces: [],
fields: [],
extraGenerator: extraGenerator,
);
});
log.info('Writing Inputs');
this.declaredInputTypes.forEach((def) {
code += classDefinition(
this,
def.name,
fromJson: true,
toJson: true,
toMap: true,
isInput: true,
directives: def.directives,
isJoinRecord: false,
fields: def.fields.map((d) =>
FieldDefinition.ofInput(this, d, entityName: def.name.value)),
extraGenerator: inputGenerator,
);
});
log.info('Writing Fragments');
this.fragments.values.forEach((def) {
fragmentNames.add(def.name.value);
code += [
'final ${def.name.value} = gql("""',
'fragment ${def.name.value} on ${def.typeCondition.on.toGQLType(withNullability: false)} {',
def.selectionSet.serialize(1),
'}',
'""").definitions.whereType<FragmentDefinitionNode>().first;',
'',
];
});
}