Skip to content

Commit

Permalink
extract gql_tristate_value to separate package in order to not pollut…
Browse files Browse the repository at this point in the history
…e gql_exec for users who don't use tristate null
  • Loading branch information
knaeckeKami committed Nov 12, 2023
1 parent f6a5b74 commit 931bb04
Show file tree
Hide file tree
Showing 27 changed files with 157 additions and 67 deletions.
1 change: 1 addition & 0 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
- gql_http_link
- gql_websocket_link
- gql_transform_link
- gql_tristate_value
runs-on: ubuntu-latest
container:
image: dart:latest
Expand Down

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.

1 change: 1 addition & 0 deletions codegen/end_to_end_test_tristate/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ dev_dependencies:
build: ^2.0.0
build_runner: ^2.0.0
test: ^1.16.8
gql_tristate_value: ^1.0.0
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:gql_exec/value.dart';
import 'package:gql_tristate_value/gql_tristate_value.dart';
import "package:test/test.dart";

import 'package:end_to_end_test_tristate/graphql/__generated__/schema.schema.gql.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:gql_exec/value.dart';
import 'package:gql_tristate_value/gql_tristate_value.dart';
import "package:test/test.dart";

import 'package:end_to_end_test_tristate/graphql/__generated__/schema.schema.gql.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:built_collection/built_collection.dart';
import 'package:gql_exec/value.dart';
import 'package:gql_tristate_value/gql_tristate_value.dart';
import "package:test/test.dart";

import 'package:end_to_end_test_tristate/graphql/__generated__/schema.schema.gql.dart';
Expand Down
2 changes: 1 addition & 1 deletion codegen/gql_build/lib/src/utils/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Map<String, String> enumFallbackMap(final dynamic enumFallbacks) {
}

TriStateValueConfig triStateOptionalsConfig(Map<String, dynamic> config) {
final configValue = config["tristate_optionals"];
final Object? configValue = config["tristate_optionals"];

if (configValue is bool) {
return configValue
Expand Down
1 change: 1 addition & 0 deletions codegen/gql_build/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies:
gql_code_builder: ^0.8.0
path: ^1.8.0
yaml: ^3.1.0
gql_tristate_value: ^1.0.0
dev_dependencies:
build_test: ^2.0.0
gql_pedantic: ^1.0.2
Expand Down
5 changes: 3 additions & 2 deletions codegen/gql_code_builder/lib/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "package:code_builder/code_builder.dart";
import "package:collection/collection.dart";
import "package:gql/ast.dart";
import "package:gql_code_builder/src/config/tristate_optionals_config.dart";
import "package:gql_code_builder/src/tristate_optionals.dart";

import "../source.dart";

Expand Down Expand Up @@ -213,8 +214,8 @@ Method buildOptionalGetter({
final optionalGetter = baseGetter.rebuild((b) => b
..returns = TypeReference((b2) => b2
..isNullable = true
..url = "package:gql_exec/value.dart"
..symbol = "Value"
..url = valueTypeUrl
..symbol = valueTypeSymbol
..types.add((baseGetter.returns as TypeReference)
.rebuild((b3) => b3..isNullable = false))));
return optionalGetter;
Expand Down
2 changes: 1 addition & 1 deletion codegen/gql_code_builder/lib/src/tristate_optionals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ String _generateTypeCast(TypeReference ref, Allocator allocator) {
}

const valueTypeSymbol = "Value";
const valueTypeUrl = "package:gql_exec/value.dart";
const valueTypeUrl = "package:gql_tristate_value/gql_tristate_value.dart";

final valueTypeRef = TypeReference(
(b) => b
Expand Down
1 change: 1 addition & 0 deletions codegen/gql_code_builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
gql: ^1.0.0
gql_exec: ^1.0.0
path: ^1.8.0
gql_tristate_value: ^1.0.0
dev_dependencies:
build_runner: ^2.1.0
gql_pedantic: ^1.0.2
Expand Down
3 changes: 3 additions & 0 deletions codegen/gql_tristate_value/.gitignore
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/
3 changes: 3 additions & 0 deletions codegen/gql_tristate_value/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
21 changes: 21 additions & 0 deletions codegen/gql_tristate_value/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-present, GQL Dart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions codegen/gql_tristate_value/README.md
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.
1 change: 1 addition & 0 deletions codegen/gql_tristate_value/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:lints/recommended.yaml
1 change: 1 addition & 0 deletions codegen/gql_tristate_value/lib/gql_tristate_value.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'src/value.dart';
54 changes: 54 additions & 0 deletions codegen/gql_tristate_value/lib/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;
}
12 changes: 12 additions & 0 deletions codegen/gql_tristate_value/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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
topics:
- graphql
- gql
- codegen
34 changes: 34 additions & 0 deletions codegen/gql_tristate_value/test/value_test.dart
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));
});
}
53 changes: 0 additions & 53 deletions links/gql_exec/lib/src/value.dart
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;
}
2 changes: 1 addition & 1 deletion links/gql_exec/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: gql_exec
version: 1.0.0
version: 1.1.0
description: Basis for GraphQL execution layer to support Link and Client.
repository: https://github.com/gql-dart/gql
environment:
Expand Down

0 comments on commit 931bb04

Please sign in to comment.