Created
January 13, 2020 21:06
-
-
Save vasilich6107/b1b9a24fb4d3ab69b32ebcfb757960a5 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:graphql_flutter/graphql_flutter.dart'; | |
import 'package:flutter/material.dart'; | |
String uuidFromObject(Object object) { | |
if (object is Map<String, Object>) { | |
final String typeName = object['__typename'] as String; | |
final String id = object['id'].toString(); | |
if (typeName != null && id != null) { | |
return <String>[typeName, id].join('/'); | |
} | |
} | |
return null; | |
} | |
final OptimisticCache cache = OptimisticCache( | |
dataIdFromObject: uuidFromObject, | |
); | |
ValueNotifier<GraphQLClient> clientFor({ | |
@required String uri, | |
String subscriptionUri, | |
}) { | |
Link link = HttpLink(uri: uri); | |
if (subscriptionUri != null) { | |
final WebSocketLink websocketLink = WebSocketLink( | |
url: subscriptionUri, | |
config: SocketClientConfig( | |
autoReconnect: true, | |
inactivityTimeout: Duration(seconds: 30), | |
), | |
); | |
link = link.concat(websocketLink); | |
} | |
return ValueNotifier<GraphQLClient>( | |
GraphQLClient( | |
cache: cache, | |
link: link, | |
), | |
); | |
} | |
/// Wraps the root application with the `graphql_flutter` client. | |
/// We use the cache for all state management. | |
class GraphqlProvider extends StatelessWidget { | |
GraphqlProvider({ | |
@required this.child, | |
@required String uri, | |
String subscriptionUri, | |
}) : client = clientFor( | |
uri: uri, | |
subscriptionUri: subscriptionUri, | |
); | |
final Widget child; | |
final ValueNotifier<GraphQLClient> client; | |
@override | |
Widget build(BuildContext context) { | |
return GraphQLProvider( | |
client: client, | |
child: child, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment