Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/counter_graphql/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/

# Web related
lib/generated_plugin_registrant.dart

# Exceptions to above rules.
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
10 changes: 10 additions & 0 deletions examples/counter_graphql/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: f139b11009aeb8ed2a3a3aa8b0066e482709dde3
channel: stable

project_type: app
16 changes: 16 additions & 0 deletions examples/counter_graphql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# counter_graphql

A new Flutter project.

## Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
65 changes: 65 additions & 0 deletions examples/counter_graphql/lib/counter/clients/counter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'package:graphql_flutter/graphql_flutter.dart';

class CounterClient {
final GraphQLClient client;

CounterClient(this.client);

Future<int> getCount() async {
var options = QueryOptions(
documentNode: gql(_getCount),
fetchPolicy: FetchPolicy.networkOnly,
);
QueryResult result = await client.query(options);

return result.data['count'] as int;
}

Future<int> increment(int count) async {
var options = QueryOptions(
documentNode: gql(_increment),
fetchPolicy: FetchPolicy.networkOnly,
variables: {
'nCount': count,
},
);

QueryResult result = await client.query(options);

print(result.data);

return result.data['increment'] as int;
}

Future<int> decrement(int count) async {
var options = QueryOptions(
documentNode: gql(_decrement),
fetchPolicy: FetchPolicy.networkOnly,
variables: {
'nCount': count,
},
);

QueryResult result = await client.query(options);

return result.data['decrement'] as int;
}
}

String _getCount = r'''
query GetCount {
count
}
''';

String _increment = r'''
mutation Increment($nCount: Int) {
increment(count: $nCount)
}
''';

String _decrement = r'''
mutation Decrement($nCount: Int) {
decrement(count: $nCount)
}
''';
57 changes: 57 additions & 0 deletions examples/counter_graphql/lib/counter/notifier/counter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:remote_state/remote_state.dart';
import 'package:state_notifier/state_notifier.dart';

import '../clients/counter.dart';
import 'states/counter.dart';

class CounterNotifier extends StateNotifier<CounterState> with LocatorMixin {
CounterClient get _counterClient => read<CounterClient>();

CounterNotifier()
: super(
CounterState(count: RemoteState<int>.initial(), isUpdating: false));

getCount() async {
try {
state = state.copyWith(count: RemoteState.loading());

var count = await _counterClient.getCount();

state = state.copyWith(count: RemoteState.success(count));
} catch (e) {
state = state.copyWith(count: RemoteState.error(e));
}
}

increment() async {
var count =
state.count.maybeWhen(success: (state) => state, orElse: () => 0);

state = state.copyWith(isUpdating: true);

try {
var result = await _counterClient.increment(count);

state =
state.copyWith(count: RemoteState.success(result), isUpdating: false);
} catch (e) {
state = state.copyWith(count: RemoteState.error(e), isUpdating: false);
}
}

decrement() async {
var count =
state.count.maybeWhen(success: (state) => state, orElse: () => 0);

state = state.copyWith(isUpdating: true);

try {
var result = await _counterClient.decrement(count);

state =
state.copyWith(count: RemoteState.success(result), isUpdating: false);
} catch (e) {
state = state.copyWith(count: RemoteState.error(e), isUpdating: false);
}
}
}
11 changes: 11 additions & 0 deletions examples/counter_graphql/lib/counter/notifier/states/counter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:remote_state/remote_state.dart';

part 'counter.freezed.dart';

@freezed
abstract class CounterState with _$CounterState {
/// Currently won't generate correctly due to https://github.com/rrousselGit/freezed/issues/128
factory CounterState({RemoteState<int> count, bool isUpdating}) =
_CounterState;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading