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_devtools_extension): Add DevTools extension to display GraphQLClient cache #1449

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
3 changes: 2 additions & 1 deletion docs/dev/MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ The version in our package.json gets copied to the one we publish, and users nee

- **graphql**: Changes related to the graphql client
- **graphql_flutter**: Changes related to the graphql_flutter package
- **graphql_devtools_extension**: Changes related to the devtools extension


### Subject
Expand Down Expand Up @@ -155,4 +156,4 @@ if we make a release with the tag `v{version_number}` this will release all the

Cheers!

[Vincent](https://github.com/vincenzopalazzo)
[Vincent](https://github.com/vincenzopalazzo)
1 change: 1 addition & 0 deletions packages/graphql/extension/devtools/.pubignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!build
6 changes: 6 additions & 0 deletions packages/graphql/extension/devtools/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: graphql
issueTracker: https://github.com/zino-hofmann/graphql-flutter/pulls
version: 0.0.1
materialIconCodePoint: '0xe4f9'
requiresConnection: true

22 changes: 21 additions & 1 deletion packages/graphql/lib/src/graphql_client.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:convert';
import 'dart:developer';

import 'package:meta/meta.dart';
import 'dart:async';

Expand All @@ -6,6 +9,9 @@ import 'package:graphql/src/cache/cache.dart';

import 'package:graphql/src/core/fetch_more.dart';

/// Flag to register extension only once
bool _isExtensionRegistered = false;

/// Universal GraphQL Client with configurable caching and [link][] system.
/// modelled after the [`apollo-client`][ac].
///
Expand Down Expand Up @@ -35,7 +41,21 @@ class GraphQLClient implements GraphQLDataProxy {
alwaysRebroadcast: alwaysRebroadcast,
deepEquals: deepEquals,
deduplicatePollers: deduplicatePollers,
);
) {
const releaseMode = bool.fromEnvironment('dart.vm.product');
// Register extension for not in release mode and not already registered
if (!releaseMode && !_isExtensionRegistered) {
// Register the extension to expose the cache to the devtools
registerExtension(
'ext.graphql.getCache',
(method, parameters) async {
return ServiceExtensionResponse.result(
jsonEncode({'value': this.cache.store.toMap()}));
},
);
_isExtensionRegistered = true;
}
}

/// The default [Policies] to set for each client action
late final DefaultPolicies defaultPolicies;
Expand Down
2 changes: 2 additions & 0 deletions packages/graphql_devtools_extension/CAHNGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# v0.0.1
Initial release
7 changes: 7 additions & 0 deletions packages/graphql_devtools_extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This is the source of the graphql's devtool.

You can locally run it with:

```
flutter run -d chrome --dart-define=use_simulated_environment=true
```
5 changes: 5 additions & 0 deletions packages/graphql_devtools_extension/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
analyzer:
language:
strict-casts: true
strict-inference: true
strict-raw-types: true
12 changes: 12 additions & 0 deletions packages/graphql_devtools_extension/lib/api/app_connection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:devtools_extensions/devtools_extensions.dart';

class AppConnection {
static const _cacheApiKey = 'ext.graphql.getCache';
static const _cacheApiValueKey = 'value';

static Future<Map<String, dynamic>?> fetchCache() async {
final result =
await serviceManager.callServiceExtensionOnMainIsolate(_cacheApiKey);
return result.json?[_cacheApiValueKey] as Map<String, dynamic>?;
}
}
39 changes: 39 additions & 0 deletions packages/graphql_devtools_extension/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:devtools_extensions/devtools_extensions.dart';
import 'package:flutter/material.dart';
import 'package:graphql_devtools_extension/ui/cache_inspector/cache_inspector.dart';

void main() {
runApp(DevToolsExtension(child: GraphQLDevToolsExtension()));
}

class GraphQLDevToolsExtension extends StatelessWidget {
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return DefaultTabController(
length: 1,
child: Column(
children: [
Align(
alignment: Alignment.centerLeft,
child: TabBar(
isScrollable: true,
tabs: [
Tab(
child: Text(
'Cache Inspector',
style: theme.textTheme.titleMedium,
),
)
],
),
),
const SizedBox(height: 24),
Expanded(
child: TabBarView(children: [const CacheInspector()]),
),
],
),
);
}
}
Loading
Loading