Created
April 11, 2020 23:19
-
-
Save micimize/62781aac9840a7b11405c3fda51a7e62 to your computer and use it in GitHub Desktop.
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
// version of auth link with overrideable header key | |
// https://github.com/zino-app/graphql-flutter/blob/master/packages/graphql/lib/src/link/auth/link_auth.dart | |
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 GetToken = FutureOr<String> Function(); | |
class AuthLink extends Link { | |
AuthLink({ | |
this.getToken, | |
this.headerKey = 'Authorization', | |
}) : super( | |
request: (Operation operation, [NextLink forward]) { | |
StreamController<FetchResult> controller; | |
Future<void> onListen() async { | |
try { | |
final String token = await getToken(); | |
operation.setContext(<String, Map<String, String>>{ | |
'headers': <String, String>{headerKey: token} | |
}); | |
} catch (error) { | |
controller.addError(error); | |
} | |
await controller.addStream(forward(operation)); | |
await controller.close(); | |
} | |
controller = StreamController<FetchResult>(onListen: onListen); | |
return controller.stream; | |
}, | |
); | |
GetToken getToken; | |
String headerKey; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment