Skip to content

Commit

Permalink
feat(examples): reorg graphql example so pub displays code
Browse files Browse the repository at this point in the history
  • Loading branch information
micimize committed Jun 1, 2020
1 parent 7e1edee commit bc32bdd
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 110 deletions.
34 changes: 15 additions & 19 deletions packages/graphql/example/README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
# `graphql` Example Application
# `graphql/client.dart` Example Application

This is a simple command line application to showcase how you can use the Dart GraphQL Client, without flutter.
This is a simple command line application to showcase how you can use the Dart GraphQL Client, without flutter.

To run this application:

## Setup:

1. First clone this repository and navigate to this directory
2. Install all dart dependencies
4. create a file `bin/local.dart` with the content:
3. create a file `bin/local.dart` with the content:
```dart
const String YOUR_PERSONAL_ACCESS_TOKEN =
'<YOUR_PERSONAL_ACCESS_TOKEN>';
```
3. Then run the Application using the commands below:

1. **List Your Repositories**

```
pub run main.dart
```

2. **Star Repository**
## Usage:

```
pub run main.dart -a star --id <REPOSITORY_ID_HERE>
```
```sh
# List repositories
pub run example

3. **Unstar Repository**
# Star Repository (you can get repository ids from `pub run example`)
pub run example -a star --id $REPOSITORY_ID

```
pub run main.dart -a unstar --id <REPOSITORY_ID_HERE>
```
# Unstar Repository
pub run example -a unstar --id $REPOSITORY_ID
```

**NB:** Replace repository id in the last two commands with a real Github Repository ID. You can get by running the first command, IDs are printed on the console.
**NB:** Replace repository id in the last two commands with a real Github Repository ID. You can get by running the first command, IDs are printed on the console.
40 changes: 40 additions & 0 deletions packages/graphql/example/bin/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:args/args.dart';
import 'package:example/main.dart';

ArgResults argResults;

/// CLI fro executing github actions
///
/// Usage:
/// ```sh
/// # List repositories
/// pub run example
///
/// # Star Repository
/// pub run example -a star --id $REPOSITORY_ID_HERE
///
/// # Unstar Repository
/// pub run example -a unstar --id $REPOSITORY_ID_HERE
/// ```
void main(List<String> arguments) {
final ArgParser parser = ArgParser()
..addOption('action', abbr: 'a', defaultsTo: 'fetch')
..addOption('id', defaultsTo: '');

argResults = parser.parse(arguments);

final String action = argResults['action'] as String;
final String id = argResults['id'] as String;

switch (action) {
case 'star':
starRepository(id);
break;
case 'unstar':
removeStarFromRepository(id);
break;
default:
readRepositories();
break;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
/// Example functions for calling the Github GraphQL API
///
/// ### Queries
/// * [readRepositories()]
///
/// ### Mutations:
/// * [starRepository(id)]
/// * [removeStarFromRepository(id)]
///
/// To run the example, create a file `lib/local.dart` with the content:
/// ```dart
/// const String YOUR_PERSONAL_ACCESS_TOKEN =
/// '<YOUR_PERSONAL_ACCESS_TOKEN>';
/// ```
import 'dart:io' show stdout, stderr, exit;

import 'package:args/args.dart';
import 'package:graphql/client.dart';

import './graphql_operation/mutations/mutations.dart';
import './graphql_operation/queries/readRepositories.dart';

// to run the example, create a file ../local.dart with the content:
// const String YOUR_PERSONAL_ACCESS_TOKEN =
// '<YOUR_PERSONAL_ACCESS_TOKEN>';
// ignore: uri_does_not_exist
import './local.dart';

ArgResults argResults;

// client - create a graphql client
GraphQLClient client() {
/// `graphql/client.dart` leverages the [gql_link][1] interface,
/// re-exporting `HttpLink`, `WebsocketLink`, `ErrorLink`, and `DedupeLink`,
/// in addition to the links we define ourselves (`AuthLink`)
///
/// [1]: https://pub.dev/packages/gql_link
/// Get an authenticated [GraphQLClient] for the github api
///
/// `graphql/client.dart` leverages the [gql_link][1] interface,
/// re-exporting [HttpLink], [WebsocketLink], [ErrorLink], and [DedupeLink],
/// in addition to the links we define ourselves (`AuthLink`)
///
/// [1]: https://pub.dev/packages/gql_link
GraphQLClient getGithubGraphQLClient() {
final Link _link = HttpLink(
'https://api.github.com/graphql',
defaultHeaders: {
Expand All @@ -34,14 +39,29 @@ GraphQLClient client() {
);
}

// query example - fetch all your github repositories
void query() async {
final GraphQLClient _client = client();
/// query example - fetch all your github repositories
void readRepositories() async {
final GraphQLClient _client = getGithubGraphQLClient();

const int nRepositories = 50;

final QueryOptions options = QueryOptions(
document: gql(readRepositories),
document: gql(
r'''
query ReadRepositories($nRepositories: Int!) {
viewer {
repositories(last: $nRepositories) {
nodes {
__typename
id
name
viewerHasStarred
}
}
}
}
''',
),
variables: {
'nRepositories': nRepositories,
},
Expand Down Expand Up @@ -71,10 +91,20 @@ void starRepository(String repositoryID) async {
exit(2);
}

final GraphQLClient _client = client();
final GraphQLClient _client = getGithubGraphQLClient();

final MutationOptions options = MutationOptions(
document: gql(addStar),
document: gql(
r'''
mutation AddStar($starrableId: ID!) {
action: addStar(input: {starrableId: $starrableId}) {
starrable {
viewerHasStarred
}
}
}
''',
),
variables: <String, dynamic>{
'starrableId': repositoryID,
},
Expand Down Expand Up @@ -104,10 +134,20 @@ void removeStarFromRepository(String repositoryID) async {
exit(2);
}

final GraphQLClient _client = client();
final GraphQLClient _client = getGithubGraphQLClient();

final MutationOptions options = MutationOptions(
document: gql(removeStar),
document: gql(
r'''
mutation RemoveStar($starrableId: ID!) {
action: removeStar(input: {starrableId: $starrableId}) {
starrable {
viewerHasStarred
}
}
}
''',
),
variables: <String, dynamic>{
'starrableId': repositoryID,
},
Expand All @@ -129,26 +169,3 @@ void removeStarFromRepository(String repositoryID) async {

exit(0);
}

void main(List<String> arguments) {
final ArgParser parser = ArgParser()
..addOption('action', abbr: 'a', defaultsTo: 'fetch')
..addOption('id', defaultsTo: '');

argResults = parser.parse(arguments);

final String action = argResults['action'] as String;
final String id = argResults['id'] as String;

switch (action) {
case 'star':
starRepository(id);
break;
case 'unstar':
removeStarFromRepository(id);
break;
default:
query();
break;
}
}
3 changes: 3 additions & 0 deletions packages/graphql/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ dependencies:
args:
graphql:
path: ..

executables:
example: example

0 comments on commit bc32bdd

Please sign in to comment.