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

retry request when token invalid or expired occured #1373

Open
J-Rigondo opened this issue Aug 25, 2023 · 1 comment
Open

retry request when token invalid or expired occured #1373

J-Rigondo opened this issue Aug 25, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@J-Rigondo
Copy link

J-Rigondo commented Aug 25, 2023

``Hello !
when user send request, access token expired, server will response error,
my GraphqlClient's ErrorLink catch onGraphQLError, on this handler do that get new access token and change only request header,
and retry request.
but forward method not working.
I use graphql_codgen, so I don't want use dio.
I think get refresh token in AuthLink bad.
server have token validation role.
How to implement this use your library?

class ApiService {
  late final GraphQLClient _client;

  ApiService() {
    final authLink = AuthLink(getToken: _getToken);
    final httpLink = HttpLink(Platform.isAndroid
        ? 'http://10.0.2.2:4000/graphql'
        : 'http://localhost:4000/graphql');
    final errorLink = ErrorLink(
      onGraphQLError: (request, forward, response) {
        print('=====================in Error link===========================');
        print(request.context);

        if (counter < 1) {
          final newReq = request.updateContextEntry<HttpLinkHeaders>((headers) {
            print(headers?.headers);
            return HttpLinkHeaders(headers: <String, String>{
              ...headers?.headers ?? <String, String>{},
              'Authorization': 'Bearer refresh new',
            });
          });
          counter++;
          print('forward req');
          forward(newReq);
        }

        print(counter);

        print('=====================in Error link===========================');

        // final oldHeaders = response.context.entry();
        //
        // print(oldHeaders);
        //
      },
    );

    final link = Link.from([authLink, errorLink, httpLink]);

    _client = GraphQLClient(
      link: link,
      cache: GraphQLCache(),
      defaultPolicies: DefaultPolicies(
        query: Policies(fetch: FetchPolicy.networkOnly),
      ),
    );
  }

  Future<String?> _getToken() async {
    return 'Bearer raewfawefawfwalfekjf';
  }

  Future<void> gqlRequest() async {
    final result = await _client.query$Relays_cursor_list(
        Options$Query$Relays_cursor_list(
            variables: Variables$Query$Relays_cursor_list(
                orderBy: Enum$order_by.POPULARITY, take: 5)));

    if (result.hasException) {
      // print(result.exception.toString());
    }
    // print(result.parsedData?.relays_cursor_list.toString());
    return;
  }
}
@J-Rigondo J-Rigondo added the enhancement New feature or request label Aug 25, 2023
@J-Rigondo J-Rigondo changed the title retry request when graphql occured retry request when token invalid or expired occured Aug 25, 2023
@Sh1d0w
Copy link

Sh1d0w commented Aug 31, 2023

The order of the links matters change your code to:

 final Link link = errorLink.concat(authLink.concat(httpLink));

and it will work.

PS. Also onGraphQLError expects stream of response type so you should change the function to

onGraphQLError: (request, forward, response) async* {
...
}

And return the forward like this:

yield* forward(newReq);

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

No branches or pull requests

2 participants