-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
…e gql_exec for users who don't use tristate null
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# https://dart.dev/guides/libraries/private-files | ||
# Created by `dart pub` | ||
.dart_tool/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 1.0.0 | ||
|
||
- Initial version. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
A `Value` class used to represent the three possible states: | ||
- absence of a value | ||
- presence of a value that is `null` | ||
- presence of a non-null value | ||
|
||
This class is used by the generated code for GraphQL variables and input types that are nullable | ||
in order to distinguish between the absence of a value and the presence of a `null` value, typically | ||
used for "update" Mutations. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include: package:lints/recommended.yaml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'src/value.dart'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/// A nullable value that may or may not be present. | ||
/// This is used to represent three possible states: | ||
/// - The value is absent. It will not be serialized. | ||
/// - The value is present and null. It will be serialized as null. | ||
/// - The value is present and non-null. It will be serialized as the value. | ||
sealed class Value<T extends Object> { | ||
const Value._(); | ||
|
||
/// The value is absent. It will not be serialized. | ||
const factory Value.absent() = AbsentValue; | ||
|
||
/// The value is present. It may still be be null. | ||
/// If the value is null, it will be serialized as null. | ||
/// If the value is non-null, it will be serialized as the value. | ||
const factory Value.present(T? value) = PresentValue<T>; | ||
|
||
/// Returns the value if present (no matter if null or non-null), otherwise throws a [StateError]. | ||
T? get requireValue => switch (this) { | ||
PresentValue(:final value) => value, | ||
AbsentValue() => throw StateError("Value is absent"), | ||
}; | ||
|
||
/// return the value if present and non-null, otherwise null. | ||
/// this is useful for cases where you don't care between the value being absent or null. | ||
T? get valueOrNull => switch (this) { | ||
PresentValue(:final value) => value, | ||
AbsentValue() => null, | ||
}; | ||
|
||
bool get isPresent => switch (this) { | ||
PresentValue() => true, | ||
AbsentValue() => false, | ||
}; | ||
} | ||
|
||
class AbsentValue extends Value<Never> { | ||
const AbsentValue() : super._(); | ||
} | ||
|
||
class PresentValue<T extends Object> extends Value<T> { | ||
final T? value; | ||
|
||
const PresentValue(this.value) : super._(); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
other is PresentValue<T> && | ||
runtimeType == other.runtimeType && | ||
value == other.value; | ||
|
||
@override | ||
int get hashCode => value.hashCode; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: gql_tristate_value | ||
version: 1.0.0 | ||
description: A wrapper class for representing a value that can be absent, present and null, or present and non-null. | ||
repository: https://github.com/gql-dart/gql | ||
environment: | ||
sdk: '>=3.0.0 <4.0.0' | ||
|
||
|
||
dev_dependencies: | ||
test: ^1.16.8 | ||
dependency_overrides: | ||
gql_code_builder: | ||
path: ../gql_code_builder | ||
gql_build: | ||
path: ../gql_build | ||
end_to_end_test_tristate: | ||
path: ../end_to_end_test_tristate | ||
end_to_end_test: | ||
path: ../end_to_end_test | ||
topics: | ||
- graphql | ||
- gql | ||
- codegen |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'package:gql_tristate_value/gql_tristate_value.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test("requireValue on absent throws", () { | ||
final absent = const AbsentValue(); | ||
|
||
expect(() => absent.requireValue, throwsA(isA<StateError>())); | ||
}); | ||
|
||
test("requireValue on present returns value", () { | ||
final present = Value.present(42); | ||
|
||
expect(present.requireValue, equals(42)); | ||
}); | ||
|
||
test("requireValue on null returns null", () { | ||
final nullValue = Value<String>.present(null); | ||
|
||
expect(nullValue.requireValue, isNull); | ||
}); | ||
|
||
test("valueOrNull on absent returns null", () { | ||
final absent = const AbsentValue(); | ||
|
||
expect(absent.valueOrNull, isNull); | ||
}); | ||
|
||
test("valueOrNull on present returns value", () { | ||
final present = Value.present(42); | ||
|
||
expect(present.valueOrNull, equals(42)); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1 @@ | ||
/// A nullable value that may or may not be present. | ||
/// This is used to represent three possible states: | ||
/// - The value is absent. It will not be serialized. | ||
/// - The value is present and null. It will be serialized as null. | ||
/// - The value is present and non-null. It will be serialized as the value. | ||
sealed class Value<T extends Object> { | ||
const Value._(); | ||
|
||
/// The value is absent. It will not be serialized. | ||
const factory Value.absent() = AbsentValue; | ||
|
||
/// The value is present. It may still be be null. | ||
/// If the value is null, it will be serialized as null. | ||
/// If the value is non-null, it will be serialized as the value. | ||
const factory Value.present(T value) = PresentValue<T>; | ||
|
||
/// Returns the value if present (no matter if null or non-null), otherwise throws a [StateError]. | ||
T? get requireValue => switch (this) { | ||
PresentValue(:final value) => value, | ||
AbsentValue() => throw StateError("Value is absent"), | ||
}; | ||
|
||
/// return the value if present and non-null, otherwise null. | ||
/// this is useful for cases where you don't care between the value being absent or null. | ||
T? get valueOrNull => switch (this) { | ||
PresentValue(:final value) => value, | ||
AbsentValue() => null, | ||
}; | ||
|
||
bool get isPresent => switch (this) { | ||
PresentValue() => true, | ||
AbsentValue() => false, | ||
}; | ||
} | ||
|
||
class AbsentValue extends Value<Never> { | ||
const AbsentValue() : super._(); | ||
} | ||
|
||
class PresentValue<T extends Object> extends Value<T> { | ||
final T? value; | ||
|
||
const PresentValue(this.value) : super._(); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
other is PresentValue<T> && | ||
runtimeType == other.runtimeType && | ||
value == other.value; | ||
|
||
@override | ||
int get hashCode => value.hashCode; | ||
} |