generateArgumentClassSpec function
Generates a Spec
of a mutation argument class.
Implementation
Spec generateArgumentClassSpec(QueryDefinition definition) {
return Class(
(b) => b
..annotations
.add(CodeExpression(Code('JsonSerializable(explicitToJson: true)')))
..name = '${definition.className}Arguments'
..extend = refer('JsonSerializable')
..mixins.add(refer('EquatableMixin'))
..methods.add(_propsMethod(
definition.inputs.map((input) => input.name.namePrintable)))
..constructors.add(Constructor(
(b) => b
..optionalParameters.addAll(definition.inputs.map(
(input) => Parameter(
(p) {
p
..name = input.name.namePrintable
..named = true
..toThis = true
..required = input.type.isNonNull;
},
),
)),
))
..constructors.add(Constructor(
(b) => b
..factory = true
..name = 'fromJson'
..lambda = true
..requiredParameters.add(Parameter(
(p) => p
..type = refer('Map<String, dynamic>')
..name = 'json',
))
..annotations.add(CodeExpression(Code('override')))
..body = Code('_\$${definition.className}ArgumentsFromJson(json)'),
))
..methods.add(Method(
(m) => m
..name = 'toJson'
..lambda = true
..returns = refer('Map<String, dynamic>')
..annotations.add(CodeExpression(Code('override')))
..body = Code('_\$${definition.className}ArgumentsToJson(this)'),
))
..fields.addAll(definition.inputs.map(
(p) => Field(
(f) {
f
..name = p.name.namePrintable
// TODO: remove this workaround when code_builder includes late field modifier:
// https://github.com/dart-lang/code_builder/pull/310
..type = refer(
'${p.type.isNonNull ? 'late ' : ''} ${p.type.namePrintable}')
..annotations
.addAll(p.annotations.map((e) => CodeExpression(Code(e))));
if (!p.type.isNonNull) {
f.modifier = FieldModifier.final$;
}
},
),
)),
);
}