Skip to content

Commit

Permalink
extract serializers to seperate package
Browse files Browse the repository at this point in the history
  • Loading branch information
knaeckeKami committed May 12, 2024
1 parent 2dad214 commit ffecca3
Show file tree
Hide file tree
Showing 41 changed files with 657 additions and 446 deletions.

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.

11 changes: 6 additions & 5 deletions codegen/end_to_end_test/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ version: 0.0.1
publish_to: none
description: Tests, not for publishing. Uses the original version of codegen without tristate optionals.
repository: https://github.com/gql-dart/gql
environment:
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
dependencies:
built_collection: ^5.0.0
built_value: ^8.0.6
gql_exec: ^1.0.0
gql_build: ^0.8.0
gql_code_builder: ^0.7.1
dev_dependencies:
gql_code_builder_serializers: ^0.1.0
dev_dependencies:
build: ^2.0.0
build_runner: ^2.0.0
test: ^1.16.8
gql_code_builder: ^0.7.1
gql_build: ^0.8.0

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.

11 changes: 6 additions & 5 deletions codegen/end_to_end_test_tristate/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ version: 0.0.1
publish_to: none
description: Tests, not for publishing. Uses the tristate optionals version of codegen.
repository: https://github.com/gql-dart/gql
environment:
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
dependencies:
built_collection: ^5.0.0
built_value: ^8.0.6
gql_exec: ^1.0.0
gql_build: ^0.8.0
gql_code_builder: ^0.7.1
dev_dependencies:
gql_code_builder_serializers: ^0.1.0
dev_dependencies:
build: ^2.0.0
build_runner: ^2.0.0
test: ^1.16.8
gql_tristate_value: ^1.0.0
gql_build: ^0.8.0
gql_code_builder: ^0.7.1
2 changes: 1 addition & 1 deletion codegen/gql_build/lib/src/serializer_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class SerializerBuilder implements Builder {
// GraphQL Operation serializer
refer(
"OperationSerializer",
"package:gql_code_builder/src/serializers/operation_serializer.dart",
"package:gql_code_builder_serializers/gql_code_builder_serializers.dart",
).call([]),
// User-defined custom serializers
...customSerializers.map((ref) => ref.call([])),
Expand Down
11 changes: 6 additions & 5 deletions codegen/gql_build/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: gql_build
version: 0.11.0+1
description: Useful builders for your GraphQL SDL and documents. Based on package:gql_code_builder and package:build
repository: https://github.com/gql-dart/gql
environment:
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
dependencies:
analyzer: '>=4.6.0 <7.0.0'
build: ^2.1.0
built_collection: ^5.0.0
Expand All @@ -14,14 +14,15 @@ dependencies:
dart_style: ^2.2.4
glob: ^2.0.0
gql: ^1.0.0
gql_code_builder: ^0.10.0
gql_code_builder: ^0.11.0
gql_code_builder_serializers: ^0.1.0
path: ^1.8.0
yaml: ^3.1.0
gql_tristate_value: ^1.0.0
dev_dependencies:
dev_dependencies:
build_test: ^2.0.0
gql_pedantic: ^1.0.2
topics:
topics:
- graphql
- gql
- codegen
6 changes: 6 additions & 0 deletions codegen/gql_code_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.12.0

- Moving built-in Serializers to gql_code_builder_serializers package, so
so don't have to depend on gql_code_builder in dependencies, and can keep
it as dev-dependency (#456)

## 0.11.0

- Adding option to create factory initializers with required parameters also for inputs (#585)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ List<Method> _inlineFragmentRootSerializationMethods({
..body = TypeReference((b) => b
..symbol = "InlineFragmentSerializer"
..url =
"package:gql_code_builder/src/serializers/inline_fragment_serializer.dart"
"package:gql_code_builder_serializers/gql_code_builder_serializers.dart"
..types.add(refer(name))).call([
literalString(name),
refer("${name}__base"),
Expand Down
2 changes: 1 addition & 1 deletion codegen/gql_code_builder/lib/src/schema/scalar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ListBuilder<Method> _buildMethods(
..body = TypeReference((b) => b
..symbol = "DefaultScalarSerializer"
..url =
"package:gql_code_builder/src/serializers/default_scalar_serializer.dart"
"package:gql_code_builder_serializers/gql_code_builder_serializers.dart"
..types.add(refer(scalarName))).call([
Method(
(b) => b
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
import "package:built_value/serializer.dart";

class DefaultScalarSerializer<T> implements PrimitiveSerializer<T> {
final T Function(Object serialized) _deserialize;

DefaultScalarSerializer(this._deserialize);

@override
final types = <Type>[T];
@override
final wireName = T.toString();

@override
Object serialize(Serializers serializers, T scalar,
{FullType specifiedType = FullType.unspecified}) =>
(scalar as dynamic).value as Object;

@override
T deserialize(Serializers serializers, Object serialized,
{FullType specifiedType = FullType.unspecified}) =>
_deserialize(serialized);
}
// serializers have been implemented in this files originally,
// but moved to a separate package later to avoid
// direct dependencies on gql_code_builder in application code.
// This file is kept for backwards compatibility and should be removed in
// the next major version of gql_code_builder.
export "package:gql_code_builder_serializers/gql_code_builder_serializers.dart"
show DefaultScalarSerializer;
Original file line number Diff line number Diff line change
@@ -1,66 +1,7 @@
import "package:built_value/serializer.dart";
import "package:built_value/standard_json_plugin.dart";

/// Deserializes a GraphQL selection with inline fragments into it's
/// appropriate concrete data class based on its `__typename` field.
///
/// If no `__typename` is found, it will simply return a base data class that
/// only includes the common fields.
class InlineFragmentSerializer<T> implements StructuredSerializer<T> {
final String rootName;
final Type baseClass;
final Map<String, Type> asTypeClasses;

InlineFragmentSerializer(
this.rootName,
this.baseClass,
this.asTypeClasses,
);

Type _typeForTypename(String name) => asTypeClasses[name] ?? baseClass;

@override
T deserialize(
Serializers serializers,
Iterable serialized, {
FullType specifiedType = FullType.unspecified,
}) {
try {
// Get JSON representation of object
final json = StandardJsonPlugin()
.afterSerialize(serialized, specifiedType) as Map<String, dynamic>;
final typeName = (json["__typename"] ?? "") as String;
final type = _typeForTypename(typeName);
final serializer =
serializers.serializerForType(type) as StructuredSerializer;
return serializer.deserialize(serializers, serialized,
specifiedType: specifiedType) as T;
} catch (e) {
rethrow;
}
}

@override
Iterable<Object?> serialize(
Serializers serializers,
T object, {
FullType specifiedType = FullType.unspecified,
}) {
try {
final typeName = (object as dynamic).G__typename as String;
final type = _typeForTypename(typeName);
final serializer =
serializers.serializerForType(type) as StructuredSerializer;
return serializer.serialize(serializers, object,
specifiedType: specifiedType);
} catch (e) {
rethrow;
}
}

@override
Iterable<Type> get types => [T];

@override
String get wireName => rootName;
}
// serializers have been implemented in this files originally,
// but moved to a separate package later to avoid
// direct dependencies on gql_code_builder in application code.
// This file is kept for backwards compatibility and should be removed in
// the next major version of gql_code_builder.
export "package:gql_code_builder_serializers/gql_code_builder_serializers.dart"
show InlineFragmentSerializer;
46 changes: 7 additions & 39 deletions codegen/gql_code_builder/lib/src/serializers/json_serializer.dart
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
import "package:built_value/serializer.dart";
import "package:built_value/standard_json_plugin.dart";

/// A serializer which can be implemented to easily build a Json Serializer
abstract class JsonSerializer<T> implements StructuredSerializer<T> {
final _jsonPlugin = StandardJsonPlugin();

T fromJson(Map<String, dynamic> json);

Map<String, dynamic> toJson(T object);

@override
T deserialize(
Serializers serializers,
Iterable serialized, {
FullType specifiedType = FullType.unspecified,
}) {
final json = _jsonPlugin.afterSerialize(serialized, specifiedType)
as Map<String, dynamic>;
return fromJson(json);
}

@override
Iterable<Object?> serialize(
Serializers serializers,
T object, {
FullType specifiedType = FullType.unspecified,
}) {
final json = toJson(object);
final serialized = _jsonPlugin.beforeDeserialize(json, FullType(T));
return serialized is Iterable ? serialized : [serialized];
}

@override
Iterable<Type> get types => [T];

@override
String get wireName => T.toString();
}
// serializers have been implemented in this files originally,
// but moved to a separate package later to avoid
// direct dependencies on gql_code_builder in application code.
// This file is kept for backwards compatibility and should be removed in
// the next major version of gql_code_builder.
export "package:gql_code_builder_serializers/gql_code_builder_serializers.dart"
show JsonSerializer;
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import "package:gql/language.dart";
import "package:gql_exec/gql_exec.dart";

import "./json_serializer.dart";

class OperationSerializer extends JsonSerializer<Operation> {
@override
Operation fromJson(Map<String, dynamic> json) => Operation(
document: parseString(json["document"] as String),
operationName: json["operationName"] as String?,
);

@override
Map<String, dynamic> toJson(Operation operation) => <String, dynamic>{
"document": printNode(operation.document),
if (operation.operationName != null)
"operationName": operation.operationName,
};
}
// serializers have been implemented in this files originally,
// but moved to a separate package later to avoid
// direct dependencies on gql_code_builder in application code.
// This file is kept for backwards compatibility and should be removed in
// the next major version of gql_code_builder.
export "package:gql_code_builder_serializers/gql_code_builder_serializers.dart"
show OperationSerializer;
11 changes: 6 additions & 5 deletions codegen/gql_code_builder/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: gql_code_builder
version: 0.11.0
version: 0.12.0
description: Dart code builders taking *.graphql documents and SDL to build useful classes.
repository: https://github.com/gql-dart/gql
environment:
environment:
sdk: '>=3.0.0 <4.0.0'
dependencies:
dependencies:
analyzer: '>=4.6.0 <7.0.0'
built_collection: ^5.0.0
built_value: ^8.0.6
Expand All @@ -14,11 +14,12 @@ dependencies:
gql_exec: ^1.0.0
path: ^1.8.0
gql_tristate_value: ^1.0.0
dev_dependencies:
gql_code_builder_serializers: ^0.1.0
dev_dependencies:
build_runner: ^2.1.0
gql_pedantic: ^1.0.2
test: ^1.16.8
topics:
topics:
- graphql
- gql
- codegen
3 changes: 3 additions & 0 deletions codegen/gql_code_builder_serializers/.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/
Loading

0 comments on commit ffecca3

Please sign in to comment.