Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
fix: json serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
Arenukvern committed May 19, 2021
1 parent e693f21 commit efda93f
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 45 deletions.
50 changes: 22 additions & 28 deletions dart_packages/cactus_sync_client/lib/src/abstract/cactus_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../graphql/graphql_result.dart';
import '../utils/utils.dart';
import 'cactus_sync.dart';
import 'graphql_runner.dart';
import 'recorded_model.dart';

/// [stringQueryGql] is a gql which replaces the whole gql
/// TODO: add an example
Expand All @@ -24,7 +25,6 @@ typedef CactusModelBuilder<
TUpdateResult,
TDeleteInput extends JsonSerializable,
TDeleteResult,
TGetInput extends JsonSerializable,
TGetResult,
TFindInput extends JsonSerializable,
TFindResult>
Expand All @@ -36,7 +36,6 @@ typedef CactusModelBuilder<
TUpdateResult,
TDeleteInput,
TDeleteResult,
TGetInput,
TGetResult,
TFindInput,
TFindResult>
Expand All @@ -50,7 +49,6 @@ abstract class AbstractCactusModel<
TUpdateResult,
TRemoveInput,
TRemoveResult,
TGetInput,
TGetResult,
TFindInput,
TFindResult> {
Expand All @@ -70,7 +68,7 @@ abstract class AbstractCactusModel<
bool notifyListeners = true,
});
Future<GraphqlResult<TGetResult>> get({
required TGetInput variableValues,
required RecordedModel variableValues,
QueryGql? queryGql,
bool notifyListeners = true,
});
Expand All @@ -89,7 +87,6 @@ class CactusModel<
TUpdateResult,
TRemoveInput extends JsonSerializable,
TRemoveResult,
TGetInput extends JsonSerializable,
TGetResult,
TFindInput extends JsonSerializable,
TFindResult>
Expand All @@ -101,7 +98,6 @@ class CactusModel<
TUpdateResult,
TRemoveInput,
TRemoveResult,
TGetInput,
TGetResult,
TFindInput,
TFindResult> {
Expand Down Expand Up @@ -142,7 +138,6 @@ class CactusModel<
TUpdateResult,
TDeleteInput,
TDeleteResult,
TGetInput,
TGetResult,
TFindInput,
TFindResult> init<
Expand All @@ -153,7 +148,6 @@ class CactusModel<
TUpdateResult,
TDeleteInput extends JsonSerializable,
TDeleteResult,
TGetInput extends JsonSerializable,
TGetResult,
TFindInput extends JsonSerializable,
TFindResult>({
Expand Down Expand Up @@ -302,10 +296,10 @@ class CactusModel<
}

@override
create({
required variableValues,
queryGql,
notifyListeners = true,
Future<GraphqlResult<TCreateResult>> create({
required TCreateInput variableValues,
QueryGql? queryGql,
bool notifyListeners = true,
}) async {
CactusSync.l.info('create');
return _executeMiddleware(
Expand All @@ -317,10 +311,10 @@ class CactusModel<
}

@override
update({
required variableValues,
queryGql,
notifyListeners = true,
Future<GraphqlResult<TUpdateResult>> update({
required TUpdateInput variableValues,
QueryGql? queryGql,
bool notifyListeners = true,
}) async {
CactusSync.l.info('update');
return _executeMiddleware(
Expand All @@ -332,10 +326,10 @@ class CactusModel<
}

@override
remove({
required variableValues,
queryGql,
notifyListeners = true,
Future<GraphqlResult<TRemoveResult>> remove({
required TRemoveInput variableValues,
QueryGql? queryGql,
bool notifyListeners = true,
}) async {
CactusSync.l.info('remove');
return _executeMiddleware(
Expand All @@ -347,10 +341,10 @@ class CactusModel<
}

@override
find({
required variableValues,
queryGql,
notifyListeners = true,
Future<GraphqlResult<TFindResult>> find({
required TFindInput variableValues,
QueryGql? queryGql,
bool notifyListeners = true,
}) async {
CactusSync.l.info('find');
return _executeMiddleware(
Expand All @@ -362,10 +356,10 @@ class CactusModel<
}

@override
get({
required variableValues,
queryGql,
notifyListeners = true,
Future<GraphqlResult<TGetResult>> get({
required RecordedModel variableValues,
QueryGql? queryGql,
bool notifyListeners = true,
}) async {
CactusSync.l.info('get');
return _executeMiddleware(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '../graphql/graphql_result.dart';
import 'cactus_model.dart';
import 'cactus_sync.dart';
import 'graphback_result_list.dart';
import 'recorded_model.dart';

enum StateModelEvents { addUpdateStateModel, removeStateModel }

Expand All @@ -31,7 +32,6 @@ class CactusModelState<
TUpdateResult,
TDeleteInput extends JsonSerializable,
TDeleteResult,
TGetInput extends JsonSerializable,
TGetResult,
TFindInput extends JsonSerializable,
TFindResult> extends StateNotifier<Set<TModel?>>
Expand All @@ -43,7 +43,6 @@ class CactusModelState<
TUpdateResult,
TDeleteInput,
TDeleteResult,
TGetInput,
TGetResult,
TFindInput,
TFindResult> {
Expand Down Expand Up @@ -78,7 +77,6 @@ class CactusModelState<
TUpdateResult,
TDeleteInput,
TDeleteResult,
TGetInput,
TGetResult,
TFindInput,
TFindResult> cactusModel;
Expand Down Expand Up @@ -236,7 +234,7 @@ class CactusModelState<

@override
Future<GraphqlResult<TGetResult>> get({
required TGetInput variableValues,
required RecordedModel variableValues,
QueryGql? queryGql,
bool notifyListeners = true,
}) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class CactusSync {
TUpdateResult,
TDeleteInput,
TDeleteResult,
TGetInput,
TGetResult,
TFindInput,
TFindResult> attachModel<
Expand All @@ -47,7 +46,6 @@ class CactusSync {
TUpdateResult,
TDeleteInput extends JsonSerializable,
TDeleteResult,
TGetInput extends JsonSerializable,
TGetResult,
TFindInput extends JsonSerializable,
TFindResult>(
Expand All @@ -59,7 +57,6 @@ class CactusSync {
TUpdateResult,
TDeleteInput,
TDeleteResult,
TGetInput,
TGetResult,
TFindInput,
TFindResult>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import 'package:graphql/client.dart';

/// Simple class with only one property - ID
class RecordedModel {
class RecordedModel extends JsonSerializable {
final String id;
const RecordedModel({
RecordedModel({
required this.id,
});
@override
Map<String, dynamic> toJson() => {'id': id};
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class GqlModelBuilder extends GqlObjectTypeDefinition {
(json){
final verifiedJson = ArgumentError.checkNotNull(json,'json');
return ${jsonSerializableName ?? properModelType}.fromJson(
verifiedJson["$operationName${isPlural ? pluralModelName : properModelType}"],
verifiedJson["$operationName${isPlural ? pluralModelName : properModelType}"] as Map<String, dynamic>,
);
}"""
.unindent();
Expand All @@ -202,7 +202,6 @@ class GqlModelBuilder extends GqlObjectTypeDefinition {
final mutationDeleteArgs = 'Mutate${properModelType}Input';
final mutationDeleteCallback = getCallbackStr(operationName: 'delete');

const queryGetArgs = 'RecordedModel';
final queryGetCallback = getCallbackStr(operationName: 'get');

final queryFindArgs = '${properModelType}Filter';
Expand All @@ -226,7 +225,6 @@ class GqlModelBuilder extends GqlObjectTypeDefinition {
$properModelType,
$mutationDeleteArgs,
$properModelType,
$queryGetArgs,
$properModelType,
$queryFindArgs,
$queryFindResult
Expand All @@ -241,7 +239,6 @@ class GqlModelBuilder extends GqlObjectTypeDefinition {
$properModelType,
$mutationDeleteArgs,
$properModelType,
$queryGetArgs,
$properModelType,
$queryFindArgs,
$queryFindResult
Expand All @@ -263,7 +260,6 @@ class GqlModelBuilder extends GqlObjectTypeDefinition {
$properModelType,
$mutationDeleteArgs,
$properModelType,
$queryGetArgs,
$properModelType,
$queryFindArgs,
$queryFindResult
Expand All @@ -276,7 +272,6 @@ class GqlModelBuilder extends GqlObjectTypeDefinition {
$properModelType,
$mutationDeleteArgs,
$properModelType,
$queryGetArgs,
$properModelType,
$queryFindArgs,
$queryFindResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class GqlObjectTypeDefinition {
);
} else {
b.extend = refer(
'JsonSerializable',
UsedDirectives.jsonAnnotation,
'graphql.JsonSerializable',
UsedDirectives.graphqlClient,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class ModelBuilder implements Builder {
/// Updated: ${DateTime.now()}
/// !---------- END CAUTION ----------!
import 'package:graphql/client.dart' as graphql;
$directivesStr
"""
.unindent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class UsedDirectives {
static const jsonAnnotation = 'package:json_annotation/json_annotation.dart';
static const equatable = 'package:equatable/equatable.dart';
static const builtValue = 'package:built_value/built_value.dart';
static const graphqlClient = 'package:graphql/client.dart';
}

final usedDirectives = [
Expand Down

0 comments on commit efda93f

Please sign in to comment.