Graphql-flutter: Need more headers in AuthLink

Created on 17 Feb 2019  路  4Comments  路  Source: zino-app/graphql-flutter

Our API service uses access-token and refresh token.

Here just one header: 'Authorization'
https://github.com/zino-app/graphql-flutter/blob/next/lib/src/link/auth/link_auth.dart#L21

We need 3 custom named headers.
for example:

HttpLink(uri: global.graphqlEndpoint, headers: {
  'x-token': state.user.accessToken,
  'x-refresh-token': state.user.refreshToken,
  'accept-language': locale.toString()
}),

Thanks.

next question

Most helpful comment

You can write your own Link:

typedef GetHeaders = Future<Map<String, String>> Function();

class CustomAuthLink extends Link {
  CustomAuthLink({
    this.getHeaders,
  }) : super(
          request: (Operation operation, [NextLink forward]) {
            StreamController<FetchResult> controller;

            Future<void> onListen() async {
              try {
                final Map<String, String> headers = await getHeaders();

                operation.setContext(<String, Map<String, String>>{
                  'headers': headers
                });
              } catch (error) {
                controller.addError(error);
              }

              await controller.addStream(forward(operation));
              await controller.close();
            }

            controller = StreamController<FetchResult>(onListen: onListen);

            return controller.stream;
          },
        );

  GetHeaders getHeaders;
}

And then you can implement it just as you would with the AuthLink.

Hope that helps!

All 4 comments

You can write your own Link:

typedef GetHeaders = Future<Map<String, String>> Function();

class CustomAuthLink extends Link {
  CustomAuthLink({
    this.getHeaders,
  }) : super(
          request: (Operation operation, [NextLink forward]) {
            StreamController<FetchResult> controller;

            Future<void> onListen() async {
              try {
                final Map<String, String> headers = await getHeaders();

                operation.setContext(<String, Map<String, String>>{
                  'headers': headers
                });
              } catch (error) {
                controller.addError(error);
              }

              await controller.addStream(forward(operation));
              await controller.close();
            }

            controller = StreamController<FetchResult>(onListen: onListen);

            return controller.stream;
          },
        );

  GetHeaders getHeaders;
}

And then you can implement it just as you would with the AuthLink.

Hope that helps!

Closing, seems revolved.

@HofmannZ need to reopen this as the above CustomAuthLink's parameter signatures fails for 4.0.0

@5hanth we moved to a new link system, see https://github.com/zino-app/graphql-flutter/blob/beta/changelog-v3-v4.md#we-now-use-the-gql_link-system

Was this page helpful?
0 / 5 - 0 ratings