From 1f0f5d49ed5fd8614ab7edf4f53d4946cdd69b06 Mon Sep 17 00:00:00 2001 From: Koki Yoshida Date: Thu, 1 Aug 2024 09:21:03 +0900 Subject: [PATCH] fix(graphql): register extension only for not release mode and not already registered --- packages/graphql/lib/src/graphql_client.dart | 24 +++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/graphql/lib/src/graphql_client.dart b/packages/graphql/lib/src/graphql_client.dart index 04098097..37a5fb3f 100644 --- a/packages/graphql/lib/src/graphql_client.dart +++ b/packages/graphql/lib/src/graphql_client.dart @@ -9,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]. /// @@ -35,14 +38,19 @@ class GraphQLClient implements GraphQLDataProxy { cache: cache, alwaysRebroadcast: alwaysRebroadcast, ) { - // 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()})); - }, - ); + 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