【发布时间】:2021-08-13 22:47:32
【问题描述】:
这是我第一次使用 Ferry 发出 GraphQL 请求。 我的 GraphQL Server 有一些查询需要 HTTP 标头进行授权。
我需要能够在初始化客户端后添加标头。
client.dart:
Future<Client> initClient() async {
await Hive.initFlutter();
final box = await Hive.openBox<Map<String, dynamic>>("graphql");
await box.clear();
final store = HiveStore(box);
final cache = Cache(store: store);
final link = HttpLink("example.com/");
final client = Client(
link: link,
cache: cache,
);
return client;
}
main.dart:
void main() async{
final client = await initClient();
GetIt.I.registerLazySingleton<Client>(() => client);
runApp(MyApp());
}
请求文件:
client.request(Req).listen((response) {
print(response.graphqlErrors); // It will return an error because theres no header with the token
print(response.data);
});
【问题讨论】:
-
在他们的 Github 上查看这篇文章,其中解释了如何添加标题。 github.com/gql-dart/ferry/issues/95 也是一个例子github.com/gql-dart/gql/blob/…
-
@ChiragBargoojar 谢谢,我不知道我是否正确理解了代码,但该示例不会强制拥有令牌?
-
我不认为它会强迫你传递令牌只是尝试它是找出的唯一方法。
-
谢谢,我会试试的。