platform/graphql_schema/lib/src/field.dart

63 lines
2.3 KiB
Dart
Raw Normal View History

2018-08-02 13:31:54 +00:00
part of graphql_schema.src.schema;
2018-08-05 01:59:31 +00:00
/// Typedef for a function that resolves the value of a [GraphQLObjectField], whether asynchronously or not.
2018-08-02 13:31:54 +00:00
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
2018-08-05 01:59:31 +00:00
/// A field on a [GraphQLObjectType].
///
/// It can have input values and additional documentation, and explicitly declares it shape
/// within the schema.
2018-08-04 19:18:53 +00:00
class GraphQLObjectField<Value, Serialized> {
2018-08-05 01:59:31 +00:00
/// The list of input values this field accepts, if any.
2018-08-04 19:18:53 +00:00
final List<GraphQLFieldInput> inputs = <GraphQLFieldInput>[];
2018-08-05 01:59:31 +00:00
/// The name of this field in serialized input.
2018-08-02 13:31:54 +00:00
final String name;
2018-08-05 01:59:31 +00:00
/// A function used to evaluate the value of this field, with respect to an arbitrary Dart value.
2018-08-02 13:31:54 +00:00
final GraphQLFieldResolver<Value, Serialized> resolve;
2018-08-05 01:59:31 +00:00
/// The [GraphQLType] associated with values that this field's [resolve] callback returns.
2018-08-02 13:31:54 +00:00
final GraphQLType<Value, Serialized> type;
2018-08-05 01:59:31 +00:00
/// An optional description of this field; useful for tools like GraphiQL.
2018-08-04 19:18:53 +00:00
final String description;
2018-08-05 01:59:31 +00:00
/// The reason that this field, if it is deprecated, was deprecated.
2018-08-03 17:36:34 +00:00
final String deprecationReason;
2018-08-02 13:31:54 +00:00
2018-08-04 19:18:53 +00:00
GraphQLObjectField(this.name, this.type,
{Iterable<GraphQLFieldInput> arguments: const <GraphQLFieldInput>[],
2018-08-02 15:17:14 +00:00
@required this.resolve,
2018-08-04 19:18:53 +00:00
this.deprecationReason,
this.description}) {
assert(type != null, 'GraphQL fields must specify a `type`.');
2018-08-05 01:32:20 +00:00
// assert(
// resolve != null, 'GraphQL fields must specify a `resolve` callback.');
2018-08-04 19:18:53 +00:00
this.inputs.addAll(arguments ?? <GraphQLFieldInput>[]);
2018-08-02 13:50:31 +00:00
}
2018-08-02 13:31:54 +00:00
2018-08-05 01:59:31 +00:00
/// Returns `true` if this field has a [deprecationReason].
2018-08-03 17:36:34 +00:00
bool get isDeprecated => deprecationReason?.isNotEmpty == true;
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);
}
2018-08-04 19:18:53 +00:00
@override
bool operator ==(other) =>
other is GraphQLObjectField &&
other.name == name &&
other.deprecationReason == deprecationReason &&
other.type == type &&
other.resolve == resolve &&
const ListEquality<GraphQLFieldInput>().equals(other.inputs, inputs);
2018-08-02 13:31:54 +00:00
}