platform/graphql_schema/lib/src/field.dart

29 lines
953 B
Dart
Raw Normal View History

2018-08-02 13:31:54 +00:00
part of graphql_schema.src.schema;
typedef FutureOr<Value> GraphQLFieldResolver<Value, Serialized>(
2018-08-02 13:56:00 +00:00
Serialized serialized, Map<String, dynamic> argumentValues);
2018-08-02 13:31:54 +00:00
class GraphQLField<Value, Serialized> {
2018-08-02 13:50:31 +00:00
final List<GraphQLFieldArgument> arguments = <GraphQLFieldArgument>[];
2018-08-02 13:31:54 +00:00
final String name;
final GraphQLFieldResolver<Value, Serialized> resolve;
final GraphQLType<Value, Serialized> type;
2018-08-02 13:50:31 +00:00
GraphQLField(this.name,
{Iterable<GraphQLFieldArgument> arguments: const <GraphQLFieldArgument>[],
this.resolve,
this.type}) {
this.arguments.addAll(arguments ?? <GraphQLFieldArgument>[]);
}
2018-08-02 13:31:54 +00:00
FutureOr<Serialized> serialize(Value value) {
return type.serialize(value);
}
2018-08-02 13:56:00 +00:00
FutureOr<Value> deserialize(Serialized serialized,
[Map<String, dynamic> argumentValues = const <String, dynamic>{}]) {
if (resolve != null) return resolve(serialized, argumentValues);
2018-08-02 13:31:54 +00:00
return type.deserialize(serialized);
}
}