setConfig static method

void setConfig({
  1. required String storefrontAccessToken,
  2. required String storeUrl,
  3. String? adminAccessToken,
  4. String storefrontApiVersion = "2024-07",
  5. CachePolicy? cachePolicy,
  6. String? language,
  7. GraphQLCache? storefrontCache,
  8. GraphQLCache? adminCache,
  9. Duration queryRequestTimeout = const Duration(seconds: 30),
})

Sets the config.

IMPORTANT: preferably call this inside the main function or at least before instantiating other Shopify classes.

adminAccessToken is optional, but required for some admin API calls like deleteCustomer.

storefrontApiVersion is optional, but defaults to "2024-07".

cachePolicy is optional, but defaults to CachePolicy.networkOnly.

language is optional, but defaults to "en". Used to change language. eg: "en", "np", "fr" etc. Only takes effect if the store supports provided language.

countryCode is optional, but defaults to null. Used to change currency units. eg: "US", "NP", "JP" etc. Only takes effect if the store supports provided currency.

storefrontCache is optional. Inject a custom GraphQLCache (e.g. backed by HiveStore for disk persistence) for the Storefront API client. Defaults to a new in-memory GraphQLCache when omitted.

adminCache is optional. Inject a custom GraphQLCache for the Admin API client. Defaults to a new in-memory GraphQLCache when omitted.

queryRequestTimeout overrides the per-request timeout on the underlying graphql package, which otherwise applies a 5 s default that frequently trips on mobile networks and can surface as a "Future already completed" crash when a late HTTP response arrives after the timeout has fired. Pass null to disable the timeout.

Implementation

static void setConfig({
  required String storefrontAccessToken,
  required String storeUrl,
  String? adminAccessToken,
  String storefrontApiVersion = "2024-07",
  CachePolicy? cachePolicy,
  String? language,
  GraphQLCache? storefrontCache,
  GraphQLCache? adminCache,
  Duration queryRequestTimeout = const Duration(seconds: 30),
}) {
  _storefrontAccessToken = storefrontAccessToken;
  _adminAccessToken = adminAccessToken;
  _storeUrl = !storeUrl.contains('http') ? 'https://$storeUrl' : storeUrl;
  _storefrontApiVersion = storefrontApiVersion;
  _fetchPolicy = cachePolicy;
  _graphQLClient = GraphQLClient(
    link: HttpLink(
      '$_storeUrl/api/$_storefrontApiVersion/graphql.json',
      defaultHeaders: {
        'X-Shopify-Storefront-Access-Token': _storefrontAccessToken!,
        'Accept-Language': language ?? 'en',
      },
    ),
    cache: storefrontCache ?? GraphQLCache(),
    queryRequestTimeout: queryRequestTimeout,
  );

  _graphQLClientAdmin = _adminAccessToken == null
      ? null
      : GraphQLClient(
          link: HttpLink(
            '$_storeUrl/admin/api/$_storefrontApiVersion/graphql.json',
            defaultHeaders: {
              'X-Shopify-Access-Token': _adminAccessToken!,
              'Accept-Language': language ?? 'en',
            },
          ),
          cache: adminCache ?? GraphQLCache(),
          queryRequestTimeout: queryRequestTimeout,
        );
}