From 4ecfa64fce56b9b4e232aba33d0feacb3d2a7a65 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Mon, 22 Aug 2022 01:06:01 +0100 Subject: [PATCH 1/3] feat(graphql_common): add common package that contains utils functions Signed-off-by: Vincenzo Palazzo --- packages/graphql_common/.gitignore | 10 +++++ packages/graphql_common/CHANGELOG.md | 3 ++ packages/graphql_common/README.md | 39 +++++++++++++++++++ packages/graphql_common/analysis_options.yaml | 30 ++++++++++++++ .../example/graphql_common_example.dart | 1 + .../graphql_common/lib/graphql_common.dart | 7 ++++ .../lib/src/tracing/logger_tracer.dart | 20 ++++++++++ .../lib/src/tracing/tracer.dart | 10 +++++ packages/graphql_common/pubspec.yaml | 17 ++++++++ .../test/graphql_common_test.dart | 11 ++++++ 10 files changed, 148 insertions(+) create mode 100644 packages/graphql_common/.gitignore create mode 100644 packages/graphql_common/CHANGELOG.md create mode 100644 packages/graphql_common/README.md create mode 100644 packages/graphql_common/analysis_options.yaml create mode 100644 packages/graphql_common/example/graphql_common_example.dart create mode 100644 packages/graphql_common/lib/graphql_common.dart create mode 100644 packages/graphql_common/lib/src/tracing/logger_tracer.dart create mode 100644 packages/graphql_common/lib/src/tracing/tracer.dart create mode 100644 packages/graphql_common/pubspec.yaml create mode 100644 packages/graphql_common/test/graphql_common_test.dart diff --git a/packages/graphql_common/.gitignore b/packages/graphql_common/.gitignore new file mode 100644 index 000000000..65c34dc86 --- /dev/null +++ b/packages/graphql_common/.gitignore @@ -0,0 +1,10 @@ +# Files and directories created by pub. +.dart_tool/ +.packages + +# Conventional directory for build outputs. +build/ + +# Omit committing pubspec.lock for library packages; see +# https://dart.dev/guides/libraries/private-files#pubspeclock. +pubspec.lock diff --git a/packages/graphql_common/CHANGELOG.md b/packages/graphql_common/CHANGELOG.md new file mode 100644 index 000000000..effe43c82 --- /dev/null +++ b/packages/graphql_common/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/packages/graphql_common/README.md b/packages/graphql_common/README.md new file mode 100644 index 000000000..8b55e735b --- /dev/null +++ b/packages/graphql_common/README.md @@ -0,0 +1,39 @@ + + +TODO: Put a short description of the package here that helps potential users +know whether this package might be useful for them. + +## Features + +TODO: List what your package can do. Maybe include images, gifs, or videos. + +## Getting started + +TODO: List prerequisites and provide or point to information on how to +start using the package. + +## Usage + +TODO: Include short and useful examples for package users. Add longer examples +to `/example` folder. + +```dart +const like = 'sample'; +``` + +## Additional information + +TODO: Tell users more about the package: where to find more information, how to +contribute to the package, how to file issues, what response they can expect +from the package authors, and more. diff --git a/packages/graphql_common/analysis_options.yaml b/packages/graphql_common/analysis_options.yaml new file mode 100644 index 000000000..dee8927aa --- /dev/null +++ b/packages/graphql_common/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/packages/graphql_common/example/graphql_common_example.dart b/packages/graphql_common/example/graphql_common_example.dart new file mode 100644 index 000000000..ab73b3a23 --- /dev/null +++ b/packages/graphql_common/example/graphql_common_example.dart @@ -0,0 +1 @@ +void main() {} diff --git a/packages/graphql_common/lib/graphql_common.dart b/packages/graphql_common/lib/graphql_common.dart new file mode 100644 index 000000000..cc0c46c0d --- /dev/null +++ b/packages/graphql_common/lib/graphql_common.dart @@ -0,0 +1,7 @@ +/// Support for doing something awesome. +/// +/// More dartdocs go here. +library graphql_common; + +export 'src/tracing/tracer.dart'; +export 'src/tracing/logger_tracer.dart'; diff --git a/packages/graphql_common/lib/src/tracing/logger_tracer.dart b/packages/graphql_common/lib/src/tracing/logger_tracer.dart new file mode 100644 index 000000000..0c414e17c --- /dev/null +++ b/packages/graphql_common/lib/src/tracing/logger_tracer.dart @@ -0,0 +1,20 @@ +import 'package:graphql_common/src/tracing/tracer.dart'; +import 'package:logger/logger.dart'; + +class LoggerTracer extends Tracer { + late Logger _logger; + + LoggerTracer( + {LogFilter? filter, + LogPrinter? printer, + LogOutput? output, + Level? level}) { + _logger = + Logger(filter: filter, printer: printer, output: output, level: level); + } + + @override + Future asyncTrace(String msg, {Map? opts}) { + return Future(() => _logger.d(msg)); + } +} diff --git a/packages/graphql_common/lib/src/tracing/tracer.dart b/packages/graphql_common/lib/src/tracing/tracer.dart new file mode 100644 index 000000000..681dc2f5c --- /dev/null +++ b/packages/graphql_common/lib/src/tracing/tracer.dart @@ -0,0 +1,10 @@ +/// Tracer is an abstract class that provide the basic blocs to +/// build an application tracer (aka Logger). +/// +/// author: Vincenzo Palazzo +abstract class Tracer { + /// Async function to start to tracing the library at runtime + /// useful when the user want log the information in an external + /// source, and it is required an async trace! + Future asyncTrace(String msg, {Map? opts}); +} diff --git a/packages/graphql_common/pubspec.yaml b/packages/graphql_common/pubspec.yaml new file mode 100644 index 000000000..d5584e7b4 --- /dev/null +++ b/packages/graphql_common/pubspec.yaml @@ -0,0 +1,17 @@ +name: graphql_common +description: A starting point for Dart libraries or applications. +version: 0.0.1 +# homepage: https://www.example.com + +environment: + sdk: '>=2.12.6 <3.0.0' + +dependencies: + logger: ^1.1.0 + + +dev_dependencies: + lints: ^2.0.0 + test: ^1.16.0 + graphql: + path: ../graphql diff --git a/packages/graphql_common/test/graphql_common_test.dart b/packages/graphql_common/test/graphql_common_test.dart new file mode 100644 index 000000000..7de0162a4 --- /dev/null +++ b/packages/graphql_common/test/graphql_common_test.dart @@ -0,0 +1,11 @@ +import 'package:test/test.dart'; + +void main() { + group('A group of tests', () { + setUp(() { + // Additional setup goes here. + }); + + test('First Test', () {}); + }); +} From 19cfbe4e5ccfe3acd2f1c5e6b637a7d0f6d37669 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Mon, 22 Aug 2022 01:07:28 +0100 Subject: [PATCH 2/3] feat(graphql): add tracer to the core library to be able to trace the lib Signed-off-by: Vincenzo Palazzo --- packages/graphql/lib/src/core/query_manager.dart | 6 ++++++ packages/graphql/lib/src/graphql_client.dart | 10 ++++++++-- .../lib/src/links/websocket_link/websocket_client.dart | 6 ++++++ .../lib/src/links/websocket_link/websocket_link.dart | 6 ++++++ packages/graphql/pubspec.yaml | 2 ++ 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/graphql/lib/src/core/query_manager.dart b/packages/graphql/lib/src/core/query_manager.dart index a53ff8342..ed99762ff 100644 --- a/packages/graphql/lib/src/core/query_manager.dart +++ b/packages/graphql/lib/src/core/query_manager.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:graphql/src/utilities/response.dart'; +import 'package:graphql_common/graphql_common.dart'; import 'package:meta/meta.dart'; import 'package:collection/collection.dart'; @@ -26,6 +27,7 @@ class QueryManager { QueryManager({ required this.link, required this.cache, + this.tracer, this.alwaysRebroadcast = false, }) { scheduler = QueryScheduler( @@ -50,6 +52,10 @@ class QueryManager { /// prevents rebroadcasting for some intensive bulk operation like [refetchSafeQueries] bool rebroadcastLocked = false; + /// Custom Tracer specified by the user to trace the library + /// if the user need it. + final Tracer? tracer; + ObservableQuery watchQuery( WatchQueryOptions options) { final ObservableQuery observableQuery = ObservableQuery( diff --git a/packages/graphql/lib/src/graphql_client.dart b/packages/graphql/lib/src/graphql_client.dart index 84cd3d598..9f26650d3 100644 --- a/packages/graphql/lib/src/graphql_client.dart +++ b/packages/graphql/lib/src/graphql_client.dart @@ -1,10 +1,9 @@ import 'package:meta/meta.dart'; import 'dart:async'; - import 'package:graphql/src/core/core.dart'; import 'package:graphql/src/cache/cache.dart'; - import 'package:graphql/src/core/fetch_more.dart'; +import 'package:graphql_common/graphql_common.dart'; /// Universal GraphQL Client with configurable caching and [link][] system. /// modelled after the [`apollo-client`][ac]. @@ -24,12 +23,14 @@ class GraphQLClient implements GraphQLDataProxy { GraphQLClient({ required this.link, required this.cache, + this.tracer, DefaultPolicies? defaultPolicies, bool alwaysRebroadcast = false, }) : defaultPolicies = defaultPolicies ?? DefaultPolicies(), queryManager = QueryManager( link: link, cache: cache, + tracer: tracer, alwaysRebroadcast: alwaysRebroadcast, ); @@ -44,11 +45,16 @@ class GraphQLClient implements GraphQLDataProxy { late final QueryManager queryManager; + /// Custom Tracer specified by the user to trace the library + /// if the user need it. + final Tracer? tracer; + /// Create a copy of the client with the provided information. GraphQLClient copyWith( {Link? link, GraphQLCache? cache, DefaultPolicies? defaultPolicies, + Tracer? tracer, bool? alwaysRebroadcast}) { return GraphQLClient( link: link ?? this.link, diff --git a/packages/graphql/lib/src/links/websocket_link/websocket_client.dart b/packages/graphql/lib/src/links/websocket_link/websocket_client.dart index a6a6aacd1..1e1979e58 100644 --- a/packages/graphql/lib/src/links/websocket_link/websocket_client.dart +++ b/packages/graphql/lib/src/links/websocket_link/websocket_client.dart @@ -7,6 +7,7 @@ import 'package:gql_exec/gql_exec.dart'; import 'package:graphql/src/core/query_options.dart' show WithType; import 'package:graphql/src/links/gql_links.dart'; import 'package:graphql/src/utilities/platform.dart'; +import 'package:graphql_common/graphql_common.dart'; import 'package:meta/meta.dart'; import 'package:rxdart/rxdart.dart'; import 'package:stream_channel/stream_channel.dart'; @@ -48,8 +49,13 @@ class SocketClientConfig { this.initialPayload, this.headers, this.connectFn, + this.tracer, }); + /// Custom Tracer specified by the user to trace the library + /// if the user need it. + final Tracer? tracer; + /// Serializer used to serialize request final RequestSerializer serializer; diff --git a/packages/graphql/lib/src/links/websocket_link/websocket_link.dart b/packages/graphql/lib/src/links/websocket_link/websocket_link.dart index 018d1d248..e8e4d4783 100644 --- a/packages/graphql/lib/src/links/websocket_link/websocket_link.dart +++ b/packages/graphql/lib/src/links/websocket_link/websocket_link.dart @@ -1,5 +1,6 @@ import 'package:gql_exec/gql_exec.dart'; import 'package:gql_link/gql_link.dart'; +import 'package:graphql_common/graphql_common.dart'; import './websocket_client.dart'; @@ -17,6 +18,7 @@ class WebSocketLink extends Link { this.url, { this.config = const SocketClientConfig(), this.subProtocol = GraphQLProtocol.graphqlWs, + this.tracer, }); final String url; @@ -26,6 +28,10 @@ class WebSocketLink extends Link { // cannot be final because we're changing the instance upon a header change. SocketClient? _socketClient; + /// Custom Tracer specified by the user to trace the library + /// if the user need it. + final Tracer? tracer; + @override Stream request(Request request, [forward]) async* { if (_socketClient == null) { diff --git a/packages/graphql/pubspec.yaml b/packages/graphql/pubspec.yaml index 32472c068..7123870af 100644 --- a/packages/graphql/pubspec.yaml +++ b/packages/graphql/pubspec.yaml @@ -22,6 +22,8 @@ dependencies: stream_channel: ^2.1.0 rxdart: ^0.27.1 uuid: ^3.0.1 + graphql_common: + path: ../graphql_common dev_dependencies: async: ^2.5.0 From bc7bdc39fc527defc7c1e1b0c14a259e76b2bcce Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sun, 27 Nov 2022 21:58:23 +0100 Subject: [PATCH 3/3] ci: include the common package inside our CI architecture Signed-off-by: Vincenzo Palazzo --- Makefile | 14 +++++++++++++- melos.yaml | 26 ++++++++++++++++++++++++++ packages/graphql/pubspec.yaml | 3 +++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 72a8eba83..f3c043a42 100644 --- a/Makefile +++ b/Makefile @@ -28,12 +28,19 @@ ci_check_flutter: ci_check_client: $(CC) run client_test --no-select +ci_check_comm: + $(CC) run comm_test --no-select + ci_fmt_client: $(CC) run client_analyze --no-select ci_fmt_flutter: + # FIXME: use client_analyze $(CC) run client_analyze --no-select +ci_fmt_comm: + $(CC) run comm_analyze --no-select + ci_coverage_client: $(CC) run client_test_coverage --no-select @@ -44,15 +51,20 @@ check_client: ci_fmt_client ci_check_client check_flutter: ci_fmt_flutter ci_check_flutter +check_comm: ci_fmt_comm ci_check_comm + changelog_client: cd packages/graphql && $(CC_CHANGELOG) changelog_flutter: cd packages/graphql_flutter && $(CC_CHANGELOG) +changelog_comm: + cd packages/graphql_common && $(CC_CHANGELOG) + changelog: changelog_client changelog_flutter -ci: dep check_client check_flutter +ci: dep check_comm check_client check_flutter clean: $(CC) clean diff --git a/melos.yaml b/melos.yaml index a13ecc20f..8a3b215ab 100644 --- a/melos.yaml +++ b/melos.yaml @@ -19,6 +19,22 @@ scripts: select-package: flutter: false + comm_analyze: + run: melos exec -c 1 -- "dart format --set-exit-if-changed . && dart analyze . --fatal-infos" + description: Run dart analyzer in a specific package. + select-package: + flutter: false + + test: + description: Run tests in a specific package. + run: melos exec -- "dart pub get && dart pub run test" + select-package: + flutter: false + dir-exists: + - "test/" + env: + MELOS_TEST: true + flutter_test: description: Run tests in a specific package. run: melos exec --depends-on="graphql" -- "flutter test" @@ -39,6 +55,16 @@ scripts: env: MELOS_TEST: true + comm_test: + description: Run tests in a specific package. + run: melos exec -- "dart pub get && dart pub run test" + select-package: + flutter: false + dir-exists: + - "test/" + env: + MELOS_TEST: true + flutter_test_coverage: description: Run tests in a specific package. run: melos exec --depends-on="graphql" -- "flutter test --coverage" diff --git a/packages/graphql/pubspec.yaml b/packages/graphql/pubspec.yaml index 7123870af..0ded433e2 100644 --- a/packages/graphql/pubspec.yaml +++ b/packages/graphql/pubspec.yaml @@ -4,6 +4,9 @@ version: 5.1.3 repository: https://github.com/zino-app/graphql-flutter/tree/main/packages/graphql issue_tracker: https://github.com/zino-hofmann/graphql-flutter/issues +# just for dev work +publish_to: 'none' + dependencies: meta: ^1.3.0 path: ^1.8.0