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,
},
);
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.
Most helpful comment
@liu-dongyu You can use an
AuthLink:The function you provide to the
AuthLinkcan be a future and can call any of you own auth logic.