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.
Fixes zino-hofmann#419 and zino-hofmann#440.
- Loading branch information
Showing
4 changed files
with
194 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,63 @@ | ||
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'; | ||
|
||
typedef ErrorHandler = void Function(ErrorResponse); | ||
|
||
class ErrorResponse { | ||
ErrorResponse({ | ||
this.graphQLErrors, | ||
this.networkError, | ||
this.result, | ||
this.operation, | ||
this.forward, | ||
}); | ||
|
||
List<dynamic> graphQLErrors; | ||
Exception networkError; | ||
FetchResult result; | ||
Operation operation; | ||
NextLink forward; | ||
} | ||
|
||
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 result) { | ||
if (result.errors != null) { | ||
errorHandler(ErrorResponse( | ||
graphQLErrors: result.errors, | ||
result: result, | ||
operation: operation, | ||
forward: forward, | ||
)); | ||
} | ||
return result; | ||
}).handleError((error) { | ||
errorHandler(ErrorResponse( | ||
networkError: error, | ||
operation: operation, | ||
forward: forward, | ||
)); | ||
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.networkError != 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.graphQLErrors != null) { | ||
called = true; | ||
} | ||
}); | ||
|
||
await execute( | ||
link: errorLink.concat(httpLink), | ||
operation: query, | ||
).first; | ||
|
||
expect( | ||
called, | ||
true, | ||
); | ||
}); | ||
}); | ||
} |