Skip to content

Commit

Permalink
Merge pull request #443 from jayjun/from
Browse files Browse the repository at this point in the history
feat(client): support joining multiple links at once
  • Loading branch information
mainawycliffe authored Nov 5, 2019
2 parents 8485f79 + 9565244 commit e9f9527
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ final GraphQLClient _client = GraphQLClient(
```

`Link.from` joins multiple links into a single link at once.

```dart
final Link _link = Link.from([_authLink, _httpLink]);
```

Once you have initialized a client, you can run queries and mutations.

### Query
Expand Down
5 changes: 5 additions & 0 deletions packages/graphql/lib/src/link/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class Link {

RequestHandler request;

static Link from(List<Link> links) {
assert(links.isNotEmpty);
return links.reduce((first, second) => first.concat(second));
}

Link concat(Link next) => _concat(this, next);
}

Expand Down
42 changes: 42 additions & 0 deletions packages/graphql/test/link/link_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:graphql/src/link/link.dart';
import 'package:graphql/src/link/operation.dart';
import 'package:test/test.dart';

void main() {
group('link', () {
test('multiple', () {
String result = '';

final link1 = Link(
request: (Operation op, [NextLink forward]) {
result += '1';
return null;
},
);

final link2 = Link(
request: (Operation op, [NextLink forward]) {
result += '2';
return null;
},
);

final link3 = Link(
request: (Operation op, [NextLink forward]) {
result += '3';
return null;
},
);

expect(
execute(
link: Link.from([link1, link2, link3]),
operation: null,
),
null,
);

expect(result, '123');
});
});
}

0 comments on commit e9f9527

Please # to comment.