Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(graphql_common): add common package that contains utils functions #1207

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
26 changes: 26 additions & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions packages/graphql/lib/src/core/query_manager.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -26,6 +27,7 @@ class QueryManager {
QueryManager({
required this.link,
required this.cache,
this.tracer,
this.alwaysRebroadcast = false,
}) {
scheduler = QueryScheduler(
Expand All @@ -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<TParsed> watchQuery<TParsed>(
WatchQueryOptions<TParsed> options) {
final ObservableQuery<TParsed> observableQuery = ObservableQuery<TParsed>(
Expand Down
10 changes: 8 additions & 2 deletions packages/graphql/lib/src/graphql_client.dart
Original file line number Diff line number Diff line change
@@ -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].
Expand All @@ -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,
);

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -17,6 +18,7 @@ class WebSocketLink extends Link {
this.url, {
this.config = const SocketClientConfig(),
this.subProtocol = GraphQLProtocol.graphqlWs,
this.tracer,
});

final String url;
Expand All @@ -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<Response> request(Request request, [forward]) async* {
if (_socketClient == null) {
Expand Down
5 changes: 5 additions & 0 deletions packages/graphql/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +25,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
Expand Down
10 changes: 10 additions & 0 deletions packages/graphql_common/.gitignore
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions packages/graphql_common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
39 changes: 39 additions & 0 deletions packages/graphql_common/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.

For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).

For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

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.
30 changes: 30 additions & 0 deletions packages/graphql_common/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void main() {}
7 changes: 7 additions & 0 deletions packages/graphql_common/lib/graphql_common.dart
Original file line number Diff line number Diff line change
@@ -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';
20 changes: 20 additions & 0 deletions packages/graphql_common/lib/src/tracing/logger_tracer.dart
Original file line number Diff line number Diff line change
@@ -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<void> asyncTrace(String msg, {Map<String, dynamic>? opts}) {
return Future(() => _logger.d(msg));
}
}
10 changes: 10 additions & 0 deletions packages/graphql_common/lib/src/tracing/tracer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// Tracer is an abstract class that provide the basic blocs to
/// build an application tracer (aka Logger).
///
/// author: Vincenzo Palazzo <[email protected]>
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<void> asyncTrace(String msg, {Map<String, dynamic>? opts});
}
17 changes: 17 additions & 0 deletions packages/graphql_common/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions packages/graphql_common/test/graphql_common_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:test/test.dart';

void main() {
group('A group of tests', () {
setUp(() {
// Additional setup goes here.
});

test('First Test', () {});
});
}