From 95652440f6d688991700673022e690d8cd04eb54 Mon Sep 17 00:00:00 2001 From: Tan Jay Jun Date: Sat, 26 Oct 2019 17:07:01 +0800 Subject: [PATCH] feat(client): support joining multiple links at once --- packages/graphql/README.md | 6 ++++ packages/graphql/lib/src/link/link.dart | 5 +++ packages/graphql/test/link/link_test.dart | 42 +++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 packages/graphql/test/link/link_test.dart diff --git a/packages/graphql/README.md b/packages/graphql/README.md index 44e38aab9..2f11b7e2e 100644 --- a/packages/graphql/README.md +++ b/packages/graphql/README.md @@ -55,6 +55,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 diff --git a/packages/graphql/lib/src/link/link.dart b/packages/graphql/lib/src/link/link.dart index 0c0130e6d..d51d5a82b 100644 --- a/packages/graphql/lib/src/link/link.dart +++ b/packages/graphql/lib/src/link/link.dart @@ -31,6 +31,11 @@ class Link { RequestHandler request; + static Link from(List links) { + assert(links.isNotEmpty); + return links.reduce((first, second) => first.concat(second)); + } + Link concat(Link next) => _concat(this, next); } diff --git a/packages/graphql/test/link/link_test.dart b/packages/graphql/test/link/link_test.dart new file mode 100644 index 000000000..3d78836b9 --- /dev/null +++ b/packages/graphql/test/link/link_test.dart @@ -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'); + }); + }); +}