Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Begging for support] Can the url be transformed? #419

Open
danielRi opened this issue Sep 19, 2023 · 2 comments
Open

[Begging for support] Can the url be transformed? #419

danielRi opened this issue Sep 19, 2023 · 2 comments

Comments

@danielRi
Copy link

Hi,

I use want to use a WebSocketLink, but for authentication I need to append the current auth token to the url (required by server).

I was thinking about something llike this, but I dont know how to update the url of the request. Can TransformLink achieve this? Im stuck, so any suggestions/help is much appreciated. Thank you!

class WebSocketAuthLink extends Link {
  final Future<String?> Function() getToken;
  final String graphQLEndpoint;

  String? _token;

  late Link _link;

  WebSocketAuthLink({
    required this.getToken,
    required this.graphQLEndpoint,
  }) {
    _link = Link.from([
      WebSocketLink(graphQLEndpoint),
      TransformLink(requestTransformer: transformRequest),
    ]);
  }

  Future<void> updateToken() async {
    _token = await getToken();
    debugPrint('token: ${_token.toString()}');
  }

  Request transformRequest(Request request) {
    // How to update url?
    // final url = oldUrl + _token?
  }

  @override
  Stream<Response> request(Request request, [forward]) async* {
    if (_token == null) {
      await updateToken();
    }

    yield* _link.request(request, forward);
  }
}
@knaeckeKami
Copy link
Collaborator

knaeckeKami commented Sep 19, 2023 via email

@danielRi
Copy link
Author

Cool Ill drop by.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants