|
1 |
| -import "package:built_value/serializer.dart"; |
2 |
| -import "package:built_value/standard_json_plugin.dart"; |
3 |
| - |
4 |
| -/// Deserializes a GraphQL selection with inline fragments into it's |
5 |
| -/// appropriate concrete data class based on its `__typename` field. |
6 |
| -/// |
7 |
| -/// If no `__typename` is found, it will simply return a base data class that |
8 |
| -/// only includes the common fields. |
9 |
| -class InlineFragmentSerializer<T> implements StructuredSerializer<T> { |
10 |
| - final String rootName; |
11 |
| - final Type baseClass; |
12 |
| - final Map<String, Type> asTypeClasses; |
13 |
| - |
14 |
| - InlineFragmentSerializer( |
15 |
| - this.rootName, |
16 |
| - this.baseClass, |
17 |
| - this.asTypeClasses, |
18 |
| - ); |
19 |
| - |
20 |
| - Type _typeForTypename(String name) => asTypeClasses[name] ?? baseClass; |
21 |
| - |
22 |
| - @override |
23 |
| - T deserialize( |
24 |
| - Serializers serializers, |
25 |
| - Iterable serialized, { |
26 |
| - FullType specifiedType = FullType.unspecified, |
27 |
| - }) { |
28 |
| - try { |
29 |
| - // Get JSON representation of object |
30 |
| - final json = StandardJsonPlugin() |
31 |
| - .afterSerialize(serialized, specifiedType) as Map<String, dynamic>; |
32 |
| - final typeName = (json["__typename"] ?? "") as String; |
33 |
| - final type = _typeForTypename(typeName); |
34 |
| - final serializer = |
35 |
| - serializers.serializerForType(type) as StructuredSerializer; |
36 |
| - return serializer.deserialize(serializers, serialized, |
37 |
| - specifiedType: specifiedType) as T; |
38 |
| - } catch (e) { |
39 |
| - rethrow; |
40 |
| - } |
41 |
| - } |
42 |
| - |
43 |
| - @override |
44 |
| - Iterable<Object?> serialize( |
45 |
| - Serializers serializers, |
46 |
| - T object, { |
47 |
| - FullType specifiedType = FullType.unspecified, |
48 |
| - }) { |
49 |
| - try { |
50 |
| - final typeName = (object as dynamic).G__typename as String; |
51 |
| - final type = _typeForTypename(typeName); |
52 |
| - final serializer = |
53 |
| - serializers.serializerForType(type) as StructuredSerializer; |
54 |
| - return serializer.serialize(serializers, object, |
55 |
| - specifiedType: specifiedType); |
56 |
| - } catch (e) { |
57 |
| - rethrow; |
58 |
| - } |
59 |
| - } |
60 |
| - |
61 |
| - @override |
62 |
| - Iterable<Type> get types => [T]; |
63 |
| - |
64 |
| - @override |
65 |
| - String get wireName => rootName; |
66 |
| -} |
| 1 | +// serializers have been implemented in this files originally, |
| 2 | +// but moved to a separate package later to avoid |
| 3 | +// direct dependencies on gql_code_builder in application code. |
| 4 | +// This file is kept for backwards compatibility and should be removed in |
| 5 | +// the next major version of gql_code_builder. |
| 6 | +export "package:gql_code_builder_serializers/gql_code_builder_serializers.dart" |
| 7 | + show InlineFragmentSerializer; |
0 commit comments