Graphql-flutter: upgraded to alpha-1.0.0

Created on 14 Sep 2018  路  7Comments  路  Source: zino-app/graphql-flutter

hello,

in previous version I was able to set apiToken

final Client client = GraphqlProvider.of(context).value;
final SharedPreferences preferences = await SharedPreferences.getInstance();
const token = preferences.getString('authToken');
client.apiToken = token;

from authentication.dart page

but now client.link says, its final, can't change it.
how can i set headers ???

Also main.dart file does not support async function so that i can't use something like:-

final SharedPreferences preferences = await SharedPreferences.getInstance();
const token = preferences.getString('authToken');

HttpLink link = HttpLink(
      uri: 'http://192.168.2.200:4000',
      headers: <String, String>{
        'Authorization': token,
      },
    );
question

Most helpful comment

@liu-dongyu You can use an AuthLink:

final AuthLink authLink = AuthLink(
  getToken: () async => 'Bearer $YOUR_PERSONAL_ACCESS_TOKEN',
);

The function you provide to the AuthLink can be a future and can call any of you own auth logic.

All 7 comments

You can override the headers trough the context operation context, allowing dynamically calculated headers. We added an example of it in the example project, but I'll add it to the migration guide as well.

The code snippet comes form the example project:

Query(
  options: QueryOptions(
    document: queries.readRepositories,
    pollInterval: 4,
    // you can optionally override some http options through the contexts
    context: <String, dynamic>{
      'headers': <String, String>{
        'Authorization': 'Bearer <YOUR_PERSONAL_ACCESS_TOKEN>',
      },
    },
  ),
  builder: (QueryResult result) {
    ...
  },
);

That is cool.. But now we have to specify headers in every request manually, can u make it one time process???

@kunaldodiya Were you able to make it one time process ? I'am solving same issue as you. Thank you for your Help ! 馃憤

@kunaldodiya @filippofilip95 did you guys figure out any solutions to this?

@nayanbhana in my project i use redux. So i usually wrap Query with StoreConnector which is providing user token.

  Widget _buildQuery() {
    return StoreConnector<AppState, String>(
      converter: (store) => store.state.user.token,
      builder: (context, token) {
        return Query(
          options: QueryOptions(
            document: lookForMusicQuery,
            variables: {'keyword': _keyword},
            context: {
              'headers': {'Authorization': 'Bearer $token'}
            },
          ),
          builder: (QueryResult result) {
            return QueryManager(
              result: result,
              builder: (data) {
                return _buildListView(data['lookForMusic']);
              },
            );
          },
        );
      },
    );
  }

This approach works but i will refactor it to something like AuthorizedQuery widget.
There will be almost same logic but then i can use it anywhere as a common Query or Mutation widget with all parameters and without wrapping it by StoreConnector.

Why close ? Is there any solution to make it one time process ?

@liu-dongyu You can use an AuthLink:

final AuthLink authLink = AuthLink(
  getToken: () async => 'Bearer $YOUR_PERSONAL_ACCESS_TOKEN',
);

The function you provide to the AuthLink can be a future and can call any of you own auth logic.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

juicycleff picture juicycleff  路  4Comments

javi11 picture javi11  路  3Comments

sjmcdowall picture sjmcdowall  路  4Comments

campanagerald picture campanagerald  路  3Comments

jomag picture jomag  路  3Comments