forked from zino-hofmann/graphql-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zino-hofmann#445 from jayjun/errorlink
feat(client): add error link
- Loading branch information
Showing
4 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:graphql/src/link/link.dart'; | ||
import 'package:graphql/src/link/operation.dart'; | ||
import 'package:graphql/src/link/fetch_result.dart'; | ||
import 'package:graphql/src/exceptions/exceptions.dart'; | ||
import 'package:graphql/src/exceptions/graphql_error.dart'; | ||
import 'package:graphql/src/exceptions/operation_exception.dart'; | ||
|
||
typedef ErrorHandler = void Function(ErrorResponse); | ||
|
||
class ErrorResponse { | ||
ErrorResponse({ | ||
this.operation, | ||
this.fetchResult, | ||
this.exception, | ||
}); | ||
|
||
Operation operation; | ||
FetchResult fetchResult; | ||
OperationException exception; | ||
} | ||
|
||
class ErrorLink extends Link { | ||
ErrorLink({ | ||
this.errorHandler, | ||
}) : super( | ||
request: (Operation operation, [NextLink forward]) { | ||
StreamController<FetchResult> controller; | ||
|
||
Future<void> onListen() async { | ||
Stream stream = forward(operation).map((FetchResult fetchResult) { | ||
if (fetchResult.errors != null) { | ||
List<GraphQLError> errors = fetchResult.errors | ||
.map((json) => GraphQLError.fromJSON(json)) | ||
.toList(); | ||
|
||
ErrorResponse response = ErrorResponse( | ||
operation: operation, | ||
fetchResult: fetchResult, | ||
exception: OperationException(graphqlErrors: errors), | ||
); | ||
|
||
errorHandler(response); | ||
} | ||
return fetchResult; | ||
}).handleError((error) { | ||
ErrorResponse response = ErrorResponse( | ||
operation: operation, | ||
exception: OperationException( | ||
clientException: translateFailure(error), | ||
), | ||
); | ||
|
||
errorHandler(response); | ||
throw error; | ||
}); | ||
|
||
await controller.addStream(stream); | ||
await controller.close(); | ||
} | ||
|
||
controller = StreamController<FetchResult>(onListen: onListen); | ||
|
||
return controller.stream; | ||
}, | ||
); | ||
|
||
ErrorHandler errorHandler; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import "dart:async"; | ||
import "dart:convert"; | ||
|
||
import 'package:graphql/src/exceptions/exceptions.dart'; | ||
import 'package:graphql/src/link/error/link_error.dart'; | ||
import 'package:graphql/src/link/http/link_http.dart'; | ||
import 'package:graphql/src/link/link.dart'; | ||
import 'package:graphql/src/link/operation.dart'; | ||
import "package:http/http.dart" as http; | ||
import "package:mockito/mockito.dart"; | ||
import "package:test/test.dart"; | ||
|
||
class MockClient extends Mock implements http.Client {} | ||
|
||
void main() { | ||
group('error link', () { | ||
MockClient client; | ||
Operation query; | ||
HttpLink httpLink; | ||
|
||
setUp(() { | ||
client = MockClient(); | ||
query = Operation( | ||
document: 'query Operation {}', | ||
operationName: 'Operation', | ||
); | ||
httpLink = HttpLink( | ||
uri: '/graphql-test', | ||
httpClient: client, | ||
); | ||
}); | ||
|
||
test('network error', () async { | ||
bool called = false; | ||
|
||
when( | ||
client.send(any), | ||
).thenAnswer( | ||
(_) => Future.value( | ||
http.StreamedResponse( | ||
Stream.fromIterable( | ||
[utf8.encode('{}')], | ||
), | ||
400, | ||
), | ||
), | ||
); | ||
|
||
final errorLink = ErrorLink(errorHandler: (response) { | ||
if (response.exception.clientException != null) { | ||
called = true; | ||
} | ||
}); | ||
|
||
Exception exception; | ||
|
||
try { | ||
await execute( | ||
link: errorLink.concat(httpLink), | ||
operation: query, | ||
).first; | ||
} on Exception catch (e) { | ||
exception = e; | ||
} | ||
|
||
expect( | ||
exception, | ||
const TypeMatcher<ClientException>(), | ||
); | ||
expect( | ||
called, | ||
true, | ||
); | ||
}); | ||
|
||
test('graphql error', () async { | ||
bool called = false; | ||
|
||
when( | ||
client.send(any), | ||
).thenAnswer( | ||
(_) => Future.value( | ||
http.StreamedResponse( | ||
Stream.fromIterable( | ||
[utf8.encode('{"errors":[{"message":"error"}]}')], | ||
), | ||
200, | ||
), | ||
), | ||
); | ||
|
||
final errorLink = ErrorLink(errorHandler: (response) { | ||
if (response.exception.graphqlErrors != null) { | ||
called = true; | ||
} | ||
}); | ||
|
||
await execute( | ||
link: errorLink.concat(httpLink), | ||
operation: query, | ||
).first; | ||
|
||
expect( | ||
called, | ||
true, | ||
); | ||
}); | ||
}); | ||
} |