Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmannZ committed Jul 24, 2018
2 parents 1f2e289 + 6ebba8e commit 94206d4
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 56 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ To use the client it first needs to be initialized with an endpoint and cache. I
import 'package:graphql_flutter/graphql_flutter.dart';
void main() {
ValueNotifier<Client> client = new ValueNotifier(
new Client(
ValueNotifier<Client> client = ValueNotifier(
Client(
endPoint: 'https://api.github.com/graphql',
cache: new InMemoryCache(),
cache: InMemoryCache(),
apiToken: '<YOUR_GITHUB_PERSONAL_ACCESS_TOKEN>',
),
);
Expand All @@ -81,9 +81,9 @@ In order to use the client, you app needs to be wrapped with the `GraphqlProvide
```dart
...
return new GraphqlProvider(
return GraphqlProvider(
client: client,
child: new MaterialApp(
child: MaterialApp(
title: 'Flutter Demo',
...
),
Expand Down Expand Up @@ -118,7 +118,7 @@ In your widget:
```dart
...
new Query(
Query(
readRepositories, // this is the query you just created
variables: {
'nRepositories': 50,
Expand All @@ -130,22 +130,22 @@ new Query(
String error,
}) {
if (error != '') {
return new Text(error);
return Text(error);
}
if (loading) {
return new Text('Loading');
return Text('Loading');
}
// it can be either Map or List
List repositories = data['viewer']['repositories']['nodes'];
return new ListView.builder(
return ListView.builder(
itemCount: repositories.length,
itemBuilder: (context, index) {
final repository = repositories[index];
return new Text(repository['name']);
return Text(repository['name']);
});
},
);
Expand Down Expand Up @@ -175,20 +175,20 @@ The syntax for mutations is fairly similar to that of a query. The only diffence
```dart
...
new Mutation(
Mutation(
addStar,
builder: (
runMutation, { // you can name it whatever you like
bool loading,
var data,
String error,
}) {
return new FloatingActionButton(
return FloatingActionButton(
onPressed: () => runMutation({
'starrableId': <A_STARTABLE_REPOSITORY_ID>,
}),
tooltip: 'Star',
child: new Icon(Icons.star),
child: Icon(Icons.star),
);
},
onCompleted: (Map<String, dynamic> data) {
Expand Down Expand Up @@ -278,12 +278,12 @@ You can always access the client direcly from the `GraphqlProvider` but to make
```dart
...
return new GraphqlConsumer(
return GraphqlConsumer(
builder: (Client client) {
// do something with the client
return new Container(
child: new Text('Hello world'),
return Container(
child: Text('Hello world'),
);
},
);
Expand All @@ -303,10 +303,10 @@ The in-memory cache can automatically be saved to and restored from offline stor
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new GraphqlProvider(
return GraphqlProvider(
client: client,
child: new CacheProvider(
child: new MaterialApp(
child: CacheProvider(
child: MaterialApp(
title: 'Flutter Demo',
...
),
Expand Down
40 changes: 20 additions & 20 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ import 'package:graphql_flutter/graphql_flutter.dart';
import './queries/readRepositories.dart' as queries;
import './mutations/addStar.dart' as mutations;

void main() => runApp(new MyApp());
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
ValueNotifier<Client> client = new ValueNotifier(
new Client(
ValueNotifier<Client> client = ValueNotifier(
Client(
endPoint: 'https://api.github.com/graphql',
cache: new InMemoryCache(),
cache: InMemoryCache(),
apiToken: '<YOUR_GITHUB_PERSONAL_ACCESS_TOKEN>',
),
);

return new GraphqlProvider(
return GraphqlProvider(
client: client,
child: new CacheProvider(
child: new MaterialApp(
child: CacheProvider(
child: MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
home: MyHomePage(title: 'Flutter Demo Home Page'),
),
),
);
Expand All @@ -41,17 +41,17 @@ class MyHomePage extends StatefulWidget {
final String title;

@override
_MyHomePageState createState() => new _MyHomePageState();
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: new Query(
body: Query(
queries.readRepositories,
pollInterval: 1,
builder: ({
Expand All @@ -60,22 +60,22 @@ class _MyHomePageState extends State<MyHomePage> {
String error,
}) {
if (error != '') {
return new Text(error);
return Text(error);
}

if (loading) {
return new Text('Loading');
return Text('Loading');
}

// it can be either Map or List
List repositories = data['viewer']['repositories']['nodes'];

return new ListView.builder(
return ListView.builder(
itemCount: repositories.length,
itemBuilder: (context, index) {
final repository = repositories[index];

return new Mutation(
return Mutation(
mutations.addStar,
builder: (
addStar, {
Expand All @@ -88,11 +88,11 @@ class _MyHomePageState extends State<MyHomePage> {
data['addStar']['starrable']['viewerHasStarred'];
}

return new ListTile(
return ListTile(
leading: repository['viewerHasStarred']
? const Icon(Icons.star, color: Colors.amber)
: const Icon(Icons.star_border),
title: new Text(repository['name']),
title: Text(repository['name']),
// NOTE: optimistic ui updates are not implemented yet, therefore changes may take upto 1 second to show.
onTap: () {
addStar({
Expand Down
10 changes: 5 additions & 5 deletions lib/src/cache/in_memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'dart:io';
import 'package:path_provider/path_provider.dart';

class InMemoryCache {
HashMap<String, dynamic> _inMemoryCache = new HashMap<String, dynamic>();
HashMap<String, dynamic> _inMemoryCache = HashMap<String, dynamic>();

Future<String> get _localStoragePath async {
final Directory directory = await getApplicationDocumentsDirectory();
Expand Down Expand Up @@ -37,15 +37,15 @@ class InMemoryCache {
try {
final File file = await _localStorageFile;
final HashMap<String, dynamic> storedHashMap =
new HashMap<String, dynamic>();
HashMap<String, dynamic>();

if (file.existsSync()) {
Stream<dynamic> inputStream = file.openRead();

inputStream
.transform(utf8.decoder) // Decode bytes to UTF8.
.transform(
new LineSplitter()) // Convert stream to individual lines.
LineSplitter()) // Convert stream to individual lines.
.listen((String line) {
final List keyAndValue = json.decode(line);

Expand All @@ -57,12 +57,12 @@ class InMemoryCache {
} on FileSystemException {
// TODO: handle No such file

return new HashMap<String, dynamic>();
return HashMap<String, dynamic>();
} catch (error) {
// TODO: handle error
print(error);

return new HashMap<String, dynamic>();
return HashMap<String, dynamic>();
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Client {
this.endPoint = endPoint;
this.cache = cache;
this.apiToken = apiToken;
this.client = new http.Client();
this.client = http.Client();
}

String _endpoint;
Expand Down Expand Up @@ -67,15 +67,15 @@ class Client {
final String reasonPhrase = response.reasonPhrase;

if (statusCode < 200 || statusCode >= 400) {
throw new http.ClientException(
throw http.ClientException(
'Network Error: $statusCode $reasonPhrase',
);
}

final Map<String, dynamic> jsonResponse = json.decode(response.body);

if (jsonResponse['errors'] != null && jsonResponse['errors'].length > 0) {
throw new Exception(
throw Exception(
'Error returned by the server in the query' +
jsonResponse['errors'].toString(),
);
Expand Down Expand Up @@ -124,7 +124,7 @@ class Client {
if (cache.hasEntity(body)) {
return cache.read(body);
} else {
throw new Exception('Can\'t find field in cache.');
throw Exception('Can\'t find field in cache.');
}
}
}
2 changes: 1 addition & 1 deletion lib/src/widgets/cache_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CacheProvider extends StatefulWidget {
final Widget child;

@override
_CacheProviderState createState() => new _CacheProviderState();
_CacheProviderState createState() => _CacheProviderState();
}

class _CacheProviderState extends State<CacheProvider>
Expand Down
4 changes: 2 additions & 2 deletions lib/src/widgets/graphql_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GraphqlProvider extends StatefulWidget {
}

@override
State<StatefulWidget> createState() => new _GraphqlProviderState();
State<StatefulWidget> createState() => _GraphqlProviderState();
}

class _GraphqlProviderState extends State<GraphqlProvider> {
Expand All @@ -42,7 +42,7 @@ class _GraphqlProviderState extends State<GraphqlProvider> {

@override
Widget build(BuildContext context) {
return new _InheritedGraphqlProvider(
return _InheritedGraphqlProvider(
client: widget.client,
child: widget.child,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/widgets/mutation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Mutation extends StatefulWidget {
final OnMutationCompleted onCompleted;

@override
MutationState createState() => new MutationState();
MutationState createState() => MutationState();
}

class MutationState extends State<Mutation> {
Expand Down
8 changes: 4 additions & 4 deletions lib/src/widgets/query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Query extends StatefulWidget {
final int pollInterval;

@override
QueryState createState() => new QueryState();
QueryState createState() => QueryState();
}

class QueryState extends State<Query> {
Expand All @@ -36,14 +36,14 @@ class QueryState extends State<Query> {
bool initialFetch = true;
Duration pollInterval;
Timer pollTimer;
Map currentVariables = new Map();
Map currentVariables = Map();

@override
void initState() {
super.initState();

if (widget.pollInterval is int) {
pollInterval = new Duration(seconds: widget.pollInterval);
pollInterval = Duration(seconds: widget.pollInterval);
}

getQueryResult();
Expand Down Expand Up @@ -107,7 +107,7 @@ class QueryState extends State<Query> {
}

if (pollInterval is Duration && !(pollTimer is Timer)) {
pollTimer = new Timer.periodic(
pollTimer = Timer.periodic(
pollInterval,
(Timer t) => getQueryResult(),
);
Expand Down

0 comments on commit 94206d4

Please # to comment.