Created
June 21, 2023 21:46
-
-
Save wackyapps/ed70e6489382bb576bd72bd187d2bb70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:checksheett/config/AppConstants.dart'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:graphql_flutter/graphql_flutter.dart'; | |
class GraphQLClientConfig { | |
// JWT Token based GraphQL client | |
static Future<GraphQLClient> gqlClient(String token) async { | |
final HttpLink httpLink = HttpLink(AppConstants.GQL_HTTPS_URL); | |
final AuthLink authLink = AuthLink( | |
getToken: () async => 'Bearer $token', | |
); | |
final Link link = authLink.concat(httpLink); | |
GraphQLClient client = GraphQLClient( | |
cache: GraphQLCache(), | |
link: link, | |
defaultPolicies: DefaultPolicies( | |
query: Policies( | |
fetch: FetchPolicy.networkOnly, | |
error: ErrorPolicy.none, | |
), | |
), | |
); | |
return client; | |
} | |
// anonymous authorization GraphQL client | |
static Future<GraphQLClient> gqlClientAnonymous() async { | |
final HttpLink httpLink = HttpLink( | |
AppConstants.GQL_HTTPS_URL, | |
defaultHeaders: {'HASURA_GRAPHQL_UNAUTHORIZED_ROLE': 'anonymous'}, | |
); | |
// httpLink.defaultHeaders['HASURA_GRAPHQL_UNAUTHORIZED_ROLE'] = 'anonymous'; | |
final Link link = httpLink; | |
GraphQLClient client = GraphQLClient( | |
cache: GraphQLCache(), | |
link: link, | |
defaultPolicies: DefaultPolicies( | |
query: Policies( | |
fetch: FetchPolicy.networkOnly, | |
error: ErrorPolicy.none, | |
), | |
), | |
); | |
return client; | |
} | |
// JWT Token based GraphQL client WebSocket based connection | |
static ValueNotifier<GraphQLClient> gqlClientWS(String token) { | |
// print("Subscription Client Initializing"); | |
WebSocketLink webSocketLink = WebSocketLink( | |
AppConstants.GQL_WS_URL, | |
config: SocketClientConfig( | |
autoReconnect: true, | |
inactivityTimeout: const Duration(seconds: 30), | |
delayBetweenReconnectionAttempts: const Duration(seconds: 2), | |
initialPayload: () async { | |
return { | |
'headers': {'Authorization': 'Bearer $token'} | |
// 'headers': {'HASURA_GRAPHQL_UNAUTHORIZED_ROLE': 'anonymous'} | |
// 'headers': {'HASURA_GRAPHQL_ADMIN_SECRET': 'w94Fi7hPvbw5X*p'} | |
}; | |
}, | |
), | |
); | |
ValueNotifier<GraphQLClient> client = ValueNotifier( | |
GraphQLClient( | |
cache: GraphQLCache(), | |
link: webSocketLink, | |
defaultPolicies: DefaultPolicies( | |
query: Policies( | |
fetch: FetchPolicy.networkOnly, | |
error: ErrorPolicy.none, | |
), | |
), | |
), | |
); | |
return client; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment