platform/packages/graphql/graphql_schema/lib/src/scalar.dart

159 lines
4.3 KiB
Dart
Raw Normal View History

2018-08-02 13:31:54 +00:00
part of graphql_schema.src.schema;
/// `true` or `false`.
final GraphQLScalarType<bool, bool> graphQLBoolean = new _GraphQLBoolType();
/// A UTF8 character sequence.
final GraphQLScalarType<String, String> graphQLString =
new _GraphQLStringType._();
/// The ID scalar type represents a unique identifier, often used to re-fetch an object or as the key for a cache.
2018-08-02 13:31:54 +00:00
///
/// The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be humanreadable.
2018-08-04 19:18:53 +00:00
final GraphQLScalarType<String, String> graphQLId =
new _GraphQLStringType._('ID');
2018-08-02 13:31:54 +00:00
/// A [DateTime], serialized as an ISO-8601 string..
2018-08-02 13:31:54 +00:00
final GraphQLScalarType<DateTime, String> graphQLDate =
new _GraphQLDateType._();
/// A signed 32bit integer.
2018-08-02 19:22:16 +00:00
final GraphQLScalarType<int, int> graphQLInt = new _GraphQLNumType<int>(
'Int', 'A signed 64-bit integer.', (x) => x is int, 'an integer');
2018-08-02 13:31:54 +00:00
/// A signed double-precision floating-point value.
final GraphQLScalarType<double, double> graphQLFloat =
2018-08-02 19:22:16 +00:00
new _GraphQLNumType<double>(
'Float',
'A signed double-precision floating-point value.',
(x) => x is double,
'a float');
2018-08-02 13:31:54 +00:00
abstract class GraphQLScalarType<Value, Serialized>
2018-08-02 19:22:16 +00:00
extends GraphQLType<Value, Serialized>
2018-08-03 17:45:40 +00:00
with _NonNullableMixin<Value, Serialized> {
Type get valueType => Value;
}
2018-08-02 13:31:54 +00:00
typedef bool _NumVerifier(x);
class _GraphQLBoolType extends GraphQLScalarType<bool, bool> {
@override
bool serialize(bool value) {
return value;
}
2018-08-02 19:22:16 +00:00
@override
String get name => 'Boolean';
@override
String get description => 'A boolean value; can be either true or false.';
2018-08-02 13:31:54 +00:00
@override
ValidationResult<bool> validate(String key, input) {
if (input != null && input is! bool)
return new ValidationResult._failure(
['Expected "$key" to be a boolean.']);
return new ValidationResult._ok(input);
}
@override
bool deserialize(bool serialized) {
return serialized;
}
2018-08-04 19:18:53 +00:00
@override
GraphQLType<bool, bool> coerceToInputObject() => this;
2018-08-02 13:31:54 +00:00
}
class _GraphQLNumType<T extends num> extends GraphQLScalarType<T, T> {
2018-08-02 19:22:16 +00:00
final String name;
final String description;
2018-08-02 13:31:54 +00:00
final _NumVerifier verifier;
final String expected;
2018-08-02 19:22:16 +00:00
_GraphQLNumType(this.name, this.description, this.verifier, this.expected);
2018-08-02 13:31:54 +00:00
@override
ValidationResult<T> validate(String key, input) {
if (input != null && !verifier(input))
return new ValidationResult._failure(
['Expected "$key" to be $expected.']);
return new ValidationResult._ok(input);
}
@override
T deserialize(T serialized) {
return serialized;
}
@override
T serialize(T value) {
return value;
}
2018-08-04 19:18:53 +00:00
@override
GraphQLType<T, T> coerceToInputObject() => this;
2018-08-02 13:31:54 +00:00
}
class _GraphQLStringType extends GraphQLScalarType<String, String> {
final String name;
2018-08-02 13:31:54 +00:00
_GraphQLStringType._([this.name = 'String']);
2018-08-02 19:22:16 +00:00
@override
String get description => 'A character sequence.';
2018-08-02 13:31:54 +00:00
@override
String serialize(String value) => value;
@override
String deserialize(String serialized) => serialized;
@override
ValidationResult<String> validate(String key, input) =>
input == null || input is String
? new ValidationResult<String>._ok(input)
: new ValidationResult._failure(['Expected "$key" to be a string.']);
2018-08-04 19:18:53 +00:00
@override
GraphQLType<String, String> coerceToInputObject() => this;
2018-08-02 13:31:54 +00:00
}
class _GraphQLDateType extends GraphQLScalarType<DateTime, String>
with _NonNullableMixin<DateTime, String> {
_GraphQLDateType._();
2018-08-02 19:22:16 +00:00
@override
String get name => 'Date';
@override
2018-08-02 19:44:44 +00:00
String get description => 'An ISO-8601 Date.';
2018-08-02 19:22:16 +00:00
2018-08-02 13:31:54 +00:00
@override
String serialize(DateTime value) => value.toIso8601String();
@override
DateTime deserialize(String serialized) => DateTime.parse(serialized);
@override
ValidationResult<String> validate(String key, input) {
if (input != null && input is! String)
return new ValidationResult<String>._failure(
['$key must be an ISO 8601-formatted date string.']);
else if (input == null) return new ValidationResult<String>._ok(input);
try {
DateTime.parse(input);
return new ValidationResult<String>._ok(input);
} on FormatException {
return new ValidationResult<String>._failure(
['$key must be an ISO 8601-formatted date string.']);
}
}
2018-08-04 19:18:53 +00:00
@override
GraphQLType<DateTime, String> coerceToInputObject() => this;
2018-08-02 13:31:54 +00:00
}