platform/graphql_parser/lib/src/language/ast/misc_value.dart

106 lines
2.4 KiB
Dart
Raw Normal View History

2018-08-04 19:18:53 +00:00
import 'package:source_span/source_span.dart';
import '../token.dart';
2019-08-08 01:48:12 +00:00
import 'input_value.dart';
2018-08-04 19:18:53 +00:00
import 'node.dart';
2019-08-08 03:01:54 +00:00
/// A GraphQL `null` literal.
2019-08-08 01:48:12 +00:00
class NullValueContext extends InputValueContext<Null> {
2019-08-08 03:01:54 +00:00
/// The source token.
final Token nullToken;
2018-08-04 19:18:53 +00:00
2019-08-08 03:01:54 +00:00
NullValueContext(this.nullToken);
/// Use [nullToken] instead.
@deprecated
Token get NULL => nullToken;
2018-08-04 19:18:53 +00:00
@override
2019-08-08 03:01:54 +00:00
FileSpan get span => nullToken.span;
2018-08-04 19:18:53 +00:00
@override
2019-08-08 01:48:12 +00:00
Null computeValue(Map<String, dynamic> variables) => null;
2018-08-04 19:18:53 +00:00
}
2019-08-08 03:01:54 +00:00
/// A GraphQL enumeration literal.
2019-08-08 01:48:12 +00:00
class EnumValueContext extends InputValueContext<String> {
2019-08-08 03:01:54 +00:00
/// The source token.
final Token nameToken;
EnumValueContext(this.nameToken);
2018-08-04 19:18:53 +00:00
2019-08-08 03:01:54 +00:00
/// Use [nameToken] instead.
@deprecated
Token get NAME => nameToken;
2018-08-04 19:18:53 +00:00
@override
2019-08-08 03:01:54 +00:00
FileSpan get span => nameToken.span;
2018-08-04 19:18:53 +00:00
@override
2019-08-08 03:01:54 +00:00
String computeValue(Map<String, dynamic> variables) => nameToken.span.text;
2018-08-04 19:18:53 +00:00
}
2019-08-08 03:01:54 +00:00
/// A GraphQL object literal.
2019-08-08 01:48:12 +00:00
class ObjectValueContext extends InputValueContext<Map<String, dynamic>> {
2019-08-08 03:01:54 +00:00
/// The source tokens.
final Token lBraceToken, rBraceToken;
/// The fields in the object.
2018-08-04 19:18:53 +00:00
final List<ObjectFieldContext> fields;
2019-08-08 03:01:54 +00:00
ObjectValueContext(this.lBraceToken, this.fields, this.rBraceToken);
/// Use [lBraceToken] instead.
Token get LBRACE => lBraceToken;
/// Use [rBraceToken] instead.
@deprecated
Token get RBRACE => rBraceToken;
2018-08-04 19:18:53 +00:00
@override
FileSpan get span {
2019-08-08 03:01:54 +00:00
var left = lBraceToken.span;
2018-08-04 19:18:53 +00:00
for (var field in fields) {
left = left.expand(field.span);
}
2019-08-08 03:01:54 +00:00
return left.expand(rBraceToken.span);
2018-08-04 19:18:53 +00:00
}
@override
2019-08-08 01:48:12 +00:00
Map<String, dynamic> computeValue(Map<String, dynamic> variables) {
2018-08-04 19:18:53 +00:00
if (fields.isEmpty) {
return <String, dynamic>{};
} else {
return fields.fold<Map<String, dynamic>>(<String, dynamic>{},
(map, field) {
2019-08-08 03:01:54 +00:00
return map
..[field.nameToken.text] = field.value.computeValue(variables);
2018-08-04 19:18:53 +00:00
});
}
}
}
2019-08-08 03:01:54 +00:00
/// A field within an [ObjectValueContext].
2018-08-04 19:18:53 +00:00
class ObjectFieldContext extends Node {
2019-08-08 03:01:54 +00:00
/// The source tokens.
final Token nameToken, colonToken;
/// The associated value.
2019-08-08 01:48:12 +00:00
final InputValueContext value;
2018-08-04 19:18:53 +00:00
2019-08-08 03:01:54 +00:00
ObjectFieldContext(this.nameToken, this.colonToken, this.value);
/// Use [nameToken] instead.
@deprecated
Token get NAME => nameToken;
/// Use [colonToken] instead.
@deprecated
Token get COLON => colonToken;
2018-08-04 19:18:53 +00:00
@override
2019-08-08 03:01:54 +00:00
FileSpan get span =>
nameToken.span.expand(colonToken.span).expand(value.span);
2018-08-04 19:18:53 +00:00
}