graphqlClient function

  1. @riverpod
GraphQLClient graphqlClient(
  1. Ref ref
)

Implementation

@riverpod
GraphQLClient graphqlClient(Ref ref) {
  // Default settings for development/testing. Remove or override in production.
  const defaultServerUrl = 'http://localhost:9999/graphql';
  const defaultApiKey = '';

  final prefs = ref.watch(sharedPreferencesProvider);
  final storedServerUrl = prefs.getString('server_base_url')?.trim() ?? '';
  final storedApiKey = prefs.getString('server_api_key')?.trim() ?? '';
  final normalizedServerUrl = normalizeGraphqlServerUrl(storedServerUrl);

  final serverUrl = normalizedServerUrl.isEmpty
      ? defaultServerUrl
      : normalizedServerUrl;
  final apiKey = storedApiKey.isEmpty ? defaultApiKey : storedApiKey;

  final HttpLink httpLink = HttpLink(
    serverUrl,
    defaultHeaders: {'ApiKey': apiKey},
  );

  return GraphQLClient(
    link: httpLink,
    cache: GraphQLCache(store: InMemoryStore()),
  );
}