Skip to content

Commit be6d4ee

Browse files
authored
Merge pull request #5 from zino-app/improve-documentation-1
Improve documentation
2 parents 296d064 + 70b7b75 commit be6d4ee

File tree

3 files changed

+78
-30
lines changed

3 files changed

+78
-30
lines changed

README.md

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,70 @@
88
[![Watch on GitHub][github-watch-badge]][github-watch]
99
[![Star on GitHub][github-star-badge]][github-star]
1010

11-
https://pub.dartlang.org/packages/graphql_flutter
11+
## Table of Contents
12+
13+
- [Installation](#installation)
14+
- [Usage](#usage)
15+
- [Queries](#queries)
16+
- [Mutations](#mutations)
17+
- [Contributing](#contributing)
18+
- [Contributors](#contributors)
19+
20+
## Installation
21+
22+
First depend on the library by adding this to your packages `pubspec.yaml`:
23+
24+
```yaml
25+
dependencies:
26+
graphql_flutter: ^0.3.0
27+
```
28+
29+
Now inside your Dart code you can import it.
30+
31+
```dart
32+
import 'package:graphql_flutter/graphql_flutter.dart';
33+
```
1234

1335
## Usage
1436

15-
In `main.dart`:
37+
To use the client it first needs to be initialzed with an endpoint. If your endpoint requires authentication you can provide it to the client by calling the setter `apiToken` on the `Client` class.
38+
39+
> For this example we will use the public GitHub API.
1640
1741
```dart
1842
...
1943
2044
import 'package:graphql_flutter/graphql_flutter.dart';
2145
2246
void main() async {
23-
client = new Client('<YOUR_ENDPOINT>');
24-
client.apiToken = '<YOUR_API_KEY>';
47+
client = new Client('https://api.github.com/graphql');
48+
client.apiToken = '<YOUR_GITHUB_PERSONAL_ACCESS_TOKEN>';
2549
2650
...
2751
}
2852
2953
...
3054
```
3155

32-
Now create a quiry:
56+
### Queries
57+
58+
Creating a query, is as simple as creating a multiline string:
3359

3460
```dart
35-
String readAllPeople = """
36-
query ReadAllPeople {
37-
allPeople(first: 4) {
38-
people {
39-
id
40-
name
61+
String readRepositories = """
62+
query ReadRepositories(\$nRepositories) {
63+
viewer {
64+
repositories(last: \$nRepositories) {
65+
nodes {
66+
id
67+
name
68+
viewerHasStarred
69+
}
4170
}
4271
}
4372
}
4473
"""
45-
.replaceAll('\n', ' ');
74+
.replaceAll('\n', ' ');
4675
```
4776

4877
In your widget:
@@ -51,9 +80,11 @@ In your widget:
5180
...
5281
5382
new Query(
54-
queries.readAllPeople,
55-
variables: {},
56-
pollInterval: 10,
83+
readRepositories, // this is the query you just created
84+
variables: {
85+
'nRepositories': 50,
86+
},
87+
pollInterval: 10, // optional
5788
builder: ({
5889
bool loading,
5990
var data,
@@ -67,38 +98,55 @@ new Query(
6798
return new Text('Loading');
6899
}
69100
70-
// It can be either Map or List or Map
71-
List people = data['allPeople']['people'];
101+
// it can be either Map or List
102+
List repositories = data['viewer']['repositories']['nodes'];
72103
73104
return new ListView.builder(
74-
itemCount: people.length,
105+
itemCount: repositories.length,
75106
itemBuilder: (context, index) {
76-
final item = people[index];
107+
final repository = repositories[index];
77108
78-
return new Text(item['name']);
109+
return new Text(repository['name']);
79110
});
80111
},
81-
)
112+
);
82113
83114
...
84115
```
85116

86-
The StarWars API does not have mutations, but has the same syntax as a query where the first argument of the builder function is the mutation function. Just call it to trigger the mutations (Yeah we deliberetly stole this from react-apollo.)
117+
### Mutations
118+
119+
Again first create the mutation string string:
120+
121+
```dart
122+
String addStar = """
123+
mutation AddStar(\$starrableId: ID!) {
124+
addStar(input: {starrableId: \$starrableId}) {
125+
starrable {
126+
viewerHasStarred
127+
}
128+
}
129+
}
130+
"""
131+
.replaceAll('\n', ' ');
132+
```
133+
134+
The syntax for mutations fairly similar to those of a query. The only diffence is that the first argument of the builder function is the mutation function. Just call it to trigger the mutations (Yeah we deliberetly stole this from react-apollo.)
87135

88136
```dart
89137
...
90138
91139
new Mutation(
92-
'<YOUR_MUTATION_STRING>',
140+
addStar,
93141
builder: (
94-
runMutation, {
142+
runMutation, { // you can name it whatever you like
95143
bool loading,
96144
var data,
97145
String error,
98146
}) {
99147
return new FloatingActionButton(
100148
onPressed: () => runMutation({
101-
<YOUR_PARAMETERS>
149+
'starrableId': <A_STARTABLE_REPOSITORY_ID>,
102150
}),
103151
tooltip: 'Increment',
104152
child: new Icon(Icons.edit),
@@ -123,7 +171,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
123171

124172
<!-- ALL-CONTRIBUTORS-LIST:END -->
125173

126-
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
174+
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind are welcome!
127175

128176
[version-badge]: https://img.shields.io/pub/v/graphql_flutter.svg?style=flat-square
129177
[package]: https://pub.dartlang.org/packages/graphql_flutter

example/app/test/widget_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// find child widgets in the widget tree, read text, and verify that the values of widget properties
55
// are correct.
66

7-
import 'package:flutter/material.dart';
8-
import 'package:flutter_test/flutter_test.dart';
7+
// import 'package:flutter/material.dart';
8+
// import 'package:flutter_test/flutter_test.dart';
99

1010
void main() {
1111
// TODO: write tests

test/graphql_flutter_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import 'package:test/test.dart';
1+
// import 'package:test/test.dart';
22

3-
import 'package:graphql_flutter/graphql_flutter.dart';
3+
// import 'package:graphql_flutter/graphql_flutter.dart';
44

55
void main() {
66
// TODO: write tests

0 commit comments

Comments
 (0)