setConfig static method

void setConfig({
  1. required String storefrontAccessToken,
  2. required String storeUrl,
  3. String? adminAccessToken,
  4. String storefrontApiVersion = "2023-07",
  5. String language = 'en',
  6. CachePolicy? cachePolicy,
})

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.

Implementation

static void setConfig({
  required String storefrontAccessToken,
  required String storeUrl,
  String? adminAccessToken,
  String storefrontApiVersion = "2023-07",
  String language = 'en',
  CachePolicy? cachePolicy,
}) {
  _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, // Default to English
      },
    ),
    cache: GraphQLCache(),
  );

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