2018-08-03 22:01:36 +00:00
|
|
|
part of graphql_schema.src.schema;
|
|
|
|
|
2018-08-05 01:56:49 +00:00
|
|
|
/// Shorthand for building a [GraphQLEnumType].
|
2018-08-03 22:41:13 +00:00
|
|
|
GraphQLEnumType enumType<Value>(String name, Map<String, Value> values,
|
2018-08-03 22:10:29 +00:00
|
|
|
{String description}) {
|
2018-08-03 22:41:13 +00:00
|
|
|
return new GraphQLEnumType<Value>(
|
|
|
|
name, values.keys.map((k) => new GraphQLEnumValue(k, values[k])).toList(),
|
2018-08-03 22:10:29 +00:00
|
|
|
description: description);
|
|
|
|
}
|
|
|
|
|
2018-08-05 01:56:49 +00:00
|
|
|
/// Shorthand for building a [GraphQLEnumType] where all the possible values
|
|
|
|
/// are mapped to Dart strings.
|
2019-03-29 00:30:40 +00:00
|
|
|
GraphQLEnumType<String> enumTypeFromStrings(String name, List<String> values,
|
2018-08-03 22:41:13 +00:00
|
|
|
{String description}) {
|
|
|
|
return new GraphQLEnumType<String>(
|
|
|
|
name, values.map((s) => new GraphQLEnumValue(s, s)).toList(),
|
|
|
|
description: description);
|
|
|
|
}
|
|
|
|
|
2018-08-05 01:56:49 +00:00
|
|
|
/// A [GraphQLType] with only a predetermined number of possible values.
|
|
|
|
///
|
|
|
|
/// Though these are serialized as strings, they carry special meaning with a type system.
|
2018-08-03 22:41:13 +00:00
|
|
|
class GraphQLEnumType<Value> extends GraphQLScalarType<Value, String>
|
|
|
|
with _NonNullableMixin<Value, String> {
|
2018-08-05 01:56:49 +00:00
|
|
|
/// The name of this enum type.
|
2018-08-03 22:01:36 +00:00
|
|
|
final String name;
|
2018-08-05 01:56:49 +00:00
|
|
|
|
|
|
|
/// The defined set of possible values for this type.
|
|
|
|
///
|
|
|
|
/// No other values will be accepted than the ones you define.
|
2018-08-03 22:41:13 +00:00
|
|
|
final List<GraphQLEnumValue<Value>> values;
|
2018-08-05 01:56:49 +00:00
|
|
|
|
|
|
|
/// A description of this enum type, for tools like GraphiQL.
|
2018-08-03 22:01:36 +00:00
|
|
|
final String description;
|
|
|
|
|
2018-08-03 22:41:13 +00:00
|
|
|
GraphQLEnumType(this.name, this.values, {this.description});
|
2018-08-03 22:01:36 +00:00
|
|
|
|
|
|
|
@override
|
2018-08-03 22:41:13 +00:00
|
|
|
String serialize(Value value) {
|
2018-08-03 22:42:35 +00:00
|
|
|
if (value == null) return null;
|
2018-08-03 22:41:13 +00:00
|
|
|
return values.firstWhere((v) => v.value == value).name;
|
|
|
|
}
|
2018-08-03 22:01:36 +00:00
|
|
|
|
2018-08-03 22:41:13 +00:00
|
|
|
@override
|
|
|
|
Value deserialize(String serialized) {
|
|
|
|
return values.firstWhere((v) => v.name == serialized).value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
ValidationResult<String> validate(String key, String input) {
|
|
|
|
if (!values.any((v) => v.name == input)) {
|
2018-08-04 19:18:53 +00:00
|
|
|
if (input == null) {
|
|
|
|
return new ValidationResult<String>._failure(
|
|
|
|
['The enum "$name" does not accept null values.']);
|
|
|
|
}
|
|
|
|
|
2018-08-03 22:41:13 +00:00
|
|
|
return new ValidationResult<String>._failure(
|
|
|
|
['"$input" is not a valid value for the enum "$name".']);
|
2018-08-03 22:01:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 22:41:13 +00:00
|
|
|
return new ValidationResult<String>._ok(input);
|
2018-08-03 22:01:36 +00:00
|
|
|
}
|
2018-08-04 19:18:53 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(other) =>
|
|
|
|
other is GraphQLEnumType &&
|
|
|
|
other.name == name &&
|
|
|
|
other.description == description &&
|
|
|
|
const ListEquality<GraphQLEnumValue>().equals(other.values, values);
|
|
|
|
|
|
|
|
@override
|
|
|
|
GraphQLType<Value, String> coerceToInputObject() => this;
|
2018-08-03 22:01:36 +00:00
|
|
|
}
|
2018-08-03 22:10:29 +00:00
|
|
|
|
2018-08-05 01:56:49 +00:00
|
|
|
/// A known value of a [GraphQLEnumType].
|
|
|
|
///
|
|
|
|
/// In practice, you might not directly call this constructor very often.
|
2018-08-03 22:41:13 +00:00
|
|
|
class GraphQLEnumValue<Value> {
|
2018-08-05 01:56:49 +00:00
|
|
|
/// The name of this value.
|
2018-08-03 22:10:29 +00:00
|
|
|
final String name;
|
2018-08-05 01:56:49 +00:00
|
|
|
|
|
|
|
/// The Dart value associated with enum values bearing the given [name].
|
2018-08-03 22:41:13 +00:00
|
|
|
final Value value;
|
2018-08-05 01:56:49 +00:00
|
|
|
|
|
|
|
/// An optional description of this value; useful for tools like GraphiQL.
|
2018-08-03 22:54:12 +00:00
|
|
|
final String description;
|
2018-08-05 01:56:49 +00:00
|
|
|
|
|
|
|
/// The reason, if any, that this value was deprecated, if it indeed is deprecated.
|
2018-08-03 22:10:29 +00:00
|
|
|
final String deprecationReason;
|
|
|
|
|
2018-08-04 19:18:53 +00:00
|
|
|
GraphQLEnumValue(this.name, this.value,
|
|
|
|
{this.description, this.deprecationReason});
|
2018-08-03 22:10:29 +00:00
|
|
|
|
2018-08-05 01:56:49 +00:00
|
|
|
/// Returns `true` if this value has a [deprecationReason].
|
2018-08-03 22:10:29 +00:00
|
|
|
bool get isDeprecated => deprecationReason != null;
|
|
|
|
|
|
|
|
@override
|
2018-08-04 19:18:53 +00:00
|
|
|
bool operator ==(other) =>
|
|
|
|
other is GraphQLEnumValue &&
|
|
|
|
other.name == name &&
|
|
|
|
other.value == value &&
|
|
|
|
other.description == description &&
|
|
|
|
other.deprecationReason == deprecationReason;
|
2018-08-03 22:10:29 +00:00
|
|
|
}
|